Skip to content

Instantly share code, notes, and snippets.

@nickcheng
Last active August 29, 2015 14:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickcheng/08577ab44d142f6f4ab1 to your computer and use it in GitHub Desktop.
Save nickcheng/08577ab44d142f6f4ab1 to your computer and use it in GitHub Desktop.
QiNiu Action for Dropzone 3
# Dropzone Action Info
# Name: QiNiu
# Description: Upload file to QiNiu and get the link.
# Handles: Files
# Creator: nickcheng
# URL: http://nickcheng.com
# OptionsNIB: ExtendedLogin
# Events: Clicked, Dragged
# KeyModifiers: Command, Option, Control, Shift
# SkipConfig: No
# RunsSandboxed: Yes
# Version: 1.0
# MinDropzoneVersion: 3.0
# RubyPath: /usr/bin/ruby
require 'qiniu'
require 'pathname'
require 'securerandom'
Qiniu.establish_connection! :access_key => ENV['username'],
:secret_key => ENV['password']
ENV['http_proxy'] = ""
def dragged
puts $items.inspect
local_file = $items[0]
new_filename = SecureRandom.uuid + Pathname.new(local_file).extname
puts local_file
put_policy = Qiniu::Auth::PutPolicy.new(ENV['server'], new_filename)
uptoken = Qiniu::Auth.generate_uptoken(put_policy)
puts uptoken
$dz.begin("Starting some task...")
$dz.determinate(true)
$dz.percent(10)
code, result, response_headers = Qiniu::Storage.upload_with_put_policy(
put_policy, # 上传策略
local_file
)
$dz.percent(50)
puts code
puts result
puts response_headers
$dz.percent(100)
$dz.finish("Task Complete")
if code != 200
$dz.fail("Error uploading file")
else
url = "http://#{ENV['server']}.u.qiniudn.com/#{result["key"]}"
$dz.url(url)
end
end
def clicked
system("open http://qiniu.com")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment