qiniu_direct_uploader 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 30834557e1e4f698f93ed5dd779c56c579ad388a
4
+ data.tar.gz: aa58f2e3cf18b6e3f285b8b09474f105e68824b8
5
+ SHA512:
6
+ metadata.gz: a9fcbbd31594dfec7c2c1eb55d95f995e73f0d141f2951c2766c9740bf1d9429825f18833eee56acc545aa9bdb38c139af6973b7b28a9be04d97eea3f196e875
7
+ data.tar.gz: e23985d0e1c3d433c2dde1e75dc3e5b155a6db9e0b0e9298f4f308bf8c2555b27fa77cc0749d2c819b8b0925bdaf3fa7c04e579a9c30f8fa5393de28c4ef335e
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.DS_Store
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in qiniu_direct_uploader.gemspec
4
+ gemspec
5
+
6
+ gem 'jquery-fileupload-rails'
7
+ gem 'activesupport'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Marble Wu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # QiniuDirectUploader
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'qiniu_direct_uploader'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install qiniu_direct_uploader
18
+
19
+ ## Usage
20
+
21
+
22
+ ## Contributing
23
+
24
+ 1. Fork it
25
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
26
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
27
+ 4. Push to the branch (`git push origin my-new-feature`)
28
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,165 @@
1
+ # Place all the behaviors and hooks related to the matching controller here.
2
+ # All this logic will automatically be available in application.js.
3
+ # You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
4
+
5
+ #= require jquery-fileupload/basic
6
+ #= require jquery-fileupload/vendor/tmpl
7
+
8
+ $ = jQuery
9
+
10
+ $.fn.QiniuUploader = (options) ->
11
+
12
+ # support multiple elements
13
+ if @length > 1
14
+ @each ->
15
+ $(this).QiniuUploader options
16
+
17
+ return this
18
+
19
+ $uploadForm = this
20
+
21
+ settings =
22
+ customCallbackData: undefined
23
+ onFilesAdd: undefined
24
+ removeProgressBarWhenCompleted: true
25
+ removeProgressBarWhenFailed: false
26
+ progressBarId: undefined
27
+ buttonId: undefined
28
+ dropPasteZoneId: undefined
29
+ allowMultipleFiles: true
30
+
31
+ $.extend settings, options
32
+
33
+ submitButtonId = $uploadForm.data('submit-button-id')
34
+ progressBarId = $uploadForm.data('progress-bar-id')
35
+ dropPasteZoneId= $uploadForm.data('drop-paste-zone-id')
36
+
37
+ submitButton = $('#' + submitButtonId) if submitButtonId
38
+ progressBar = $('#' + progressBarId) if progressBarId
39
+ dropPasteZone = if dropPasteZoneId then $('#' + dropPasteZoneId) else $(document)
40
+
41
+ currentFiles = []
42
+ formsForSubmit = []
43
+
44
+ if submitButton and submitButton.length > 0
45
+ submitButton.click ->
46
+ form.submit() for form in formsForSubmit
47
+ false
48
+
49
+ generateRandomString= (length) ->
50
+ chars = "abcdefghiklmno0123456789pqrstuvwxyz"
51
+ text = ""
52
+ i = 0
53
+ while i < length
54
+ randomPoz = Math.floor(Math.random() * chars.length)
55
+ text += chars.substring(randomPoz, randomPoz + 1)
56
+ i++
57
+ text
58
+
59
+ setUploadForm = ->
60
+ $uploadForm.fileupload
61
+ dropZone: dropPasteZone
62
+ pasteZone: dropPasteZone
63
+ add: (e, data) ->
64
+ file = data.files[0]
65
+ file.uniqueId = generateRandomString(10) + Math.random().toString(36).substr(2,12)
66
+
67
+ unless settings.onFilesAdd and not settings.onFilesAdd(file)
68
+ currentFiles.push data
69
+ if $('#template-upload').length > 0
70
+ data.context = $($.trim(tmpl("template-upload", file)))
71
+ $(data.context).appendTo(progressBar || $uploadForm)
72
+ else if !settings.allowMultipleFiles
73
+ data.context = progressBar
74
+ if submitButton and submitButton.length > 0
75
+ if settings.allowMultipleFiles
76
+ formsForSubmit.push data
77
+ else
78
+ formsForSubmit = [data]
79
+ else
80
+ data.submit()
81
+
82
+ start: (e) ->
83
+ $uploadForm.trigger("qiniu_upload_start", [e])
84
+
85
+ progress: (e, data) ->
86
+ if data.context
87
+ progress = parseInt(data.loaded / data.total * 100, 10)
88
+ data.context.find('.bar').css('width', progress + '%')
89
+
90
+ done: (e, data) ->
91
+ postData = buildCallbackData $uploadForm, data.files[0], data.result
92
+ callbackUrl = $uploadForm.data('callback-url')
93
+ if callbackUrl
94
+ $.ajax
95
+ type: $uploadForm.data('callback-method')
96
+ url: callbackUrl
97
+ data: postData
98
+ beforeSend: ( xhr, settings ) -> $uploadForm.trigger( 'ajax:beforeSend', [xhr, settings] )
99
+ complete: ( xhr, status ) -> $uploadForm.trigger( 'ajax:complete', [xhr, status] )
100
+ success: ( data, status, xhr ) -> $uploadForm.trigger( 'ajax:success', [data, status, xhr] )
101
+ error: ( xhr, status, error ) -> $uploadForm.trigger( 'ajax:error', [xhr, status, error] )
102
+
103
+ data.context.remove() if data.context && settings.removeProgressBarWhenCompleted # remove progress bar
104
+ $uploadForm.trigger("qiniu_upload_complete", [postData])
105
+
106
+ currentFiles.splice($.inArray(data, currentFiles), 1) # remove that element from the array
107
+ $uploadForm.trigger("qiniu_upload_complete", [postData]) unless currentFiles.length
108
+
109
+ fail: (e, data) ->
110
+ content = buildCallbackData $uploadForm, data.files[0], data.result
111
+ content.errorThrown = data.errorThrown
112
+
113
+ data.context.remove() if data.context && settings.removeProgressBarWhenFailed # remove progress bar
114
+ $uploadForm.trigger("qiniu_upload_failed", [postData])
115
+
116
+ formData: (form) ->
117
+ data = form.serializeArray()
118
+ #fileType = ""
119
+ #if "type" of @files[0]
120
+ #fileType = @files[0].type
121
+ #data.push
122
+ #name: "x:contentType"
123
+ #value: fileType
124
+
125
+ key = $uploadForm.data("key")
126
+ .replace('{timestamp}', new Date().getTime())
127
+ .replace('{unique-id}', @files[0].uniqueId)
128
+ .replace('{filename}', @files[0].name)
129
+
130
+ # substitute upload timestamp and uniqueId into key
131
+ keyField = $.grep data, (n) ->
132
+ n if n.name == "key"
133
+
134
+ if keyField.length > 0
135
+ keyField[0].value = key
136
+
137
+ # IE <= 9 doesn't have XHR2 hence it can't use formData
138
+ # replace 'key' field to submit form
139
+ unless 'FormData' of window
140
+ $uploadForm.find("input[name='key']").val(key)
141
+ data
142
+
143
+ buildCallbackData = ($uploadForm, file, result) ->
144
+ content = {}
145
+ content = $.extend content, result if result
146
+ content = $.extend content, settings.customCallbackData if settings.customCallbackData
147
+ content
148
+
149
+ #public methods
150
+ @initialize = ->
151
+ # Save key for IE9 Fix
152
+ $uploadForm.data("key", $uploadForm.find("input[name='key']").val())
153
+ setUploadForm()
154
+ this
155
+
156
+ @storePath = ->
157
+ newPath = $uploadForm.data('store-path')
158
+ newPath = '/' + newPath if newPath.slice(0, 1) != '/'
159
+ newPath = newPath + '/' if newPath.slice(-1) != '/'
160
+ newPath
161
+
162
+ @customCallbackData = (newData) ->
163
+ settings.customCallbackData = newData
164
+
165
+ @initialize()
@@ -0,0 +1,20 @@
1
+ // Place all the styles related to the items controller here.
2
+ // They will automatically be included in application.css.
3
+ // You can use Sass (SCSS) here: http://sass-lang.com/
4
+ .upload {
5
+ border-top: solid 1px #CCC;
6
+ width: 400px;
7
+ padding-top: 10px;
8
+ margin-top: 10px;
9
+
10
+ .progress {
11
+ margin-top: 8px;
12
+ border: solid 1px #555;
13
+ border-radius: 3px;
14
+ -moz-border-radius: 3px;
15
+ .bar {
16
+ height: 10px;
17
+ background: #3EC144;
18
+ }
19
+ }
20
+ }
@@ -0,0 +1,5 @@
1
+ # coding: utf-8
2
+ module QiniuDirectUploader
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ module QiniuDirectUploader
3
+ module FormHelper
4
+ def qiniu_uploader_form(options = {}, &block)
5
+ uploader = Uploader.new(options)
6
+ form_tag(uploader.action, uploader.form_options) do
7
+ all_hidden_fields = {}
8
+ all_hidden_fields = all_hidden_fields.merge uploader.fields
9
+
10
+ custom_hidden_fields = {}
11
+ uploader.custom_fields.each do |key,value|
12
+ custom_hidden_fields["x:#{key}"] = value
13
+ end
14
+
15
+ all_hidden_fields = all_hidden_fields.reverse_merge custom_hidden_fields
16
+ #all_hidden_fields = all_hidden_fields.reverse_merge({:ooo=>uploader.return_body})
17
+
18
+ all_hidden_fields.map do |name, value|
19
+ hidden_field_tag(name, value)
20
+ end.join.html_safe + capture(&block)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,95 @@
1
+ # coding: utf-8
2
+ module QiniuDirectUploader
3
+ class Uploader
4
+ def initialize(options)
5
+ @options = options.reverse_merge(
6
+ expires_in: 360,
7
+ ssl: false,
8
+ store_path: '/uploads/',
9
+ custom_fields: {},
10
+ submit_button_id: nil,
11
+ progress_bar_id: nil,
12
+ drop_paste_zone_id: nil,
13
+ callback_method: "POST"
14
+ )
15
+ end
16
+
17
+ def form_options
18
+ {
19
+ id: @options[:id],
20
+ class: @options[:class],
21
+ method: "post",
22
+ authenticity_token: false,
23
+ multipart: true,
24
+ data: {
25
+ store_path: @options[:store_path],
26
+ callback_url: @options[:callback_url],
27
+ callback_method: @options[:callback_method],
28
+ submit_button_id: @options[:submit_button_id],
29
+ drop_paste_zone_id: @options[:drop_paste_zone_id],
30
+ progress_bar_id: @options[:progress_bar_id]
31
+ }.reverse_merge(@options[:data] || {})
32
+ }
33
+ end
34
+
35
+ def fields
36
+ {
37
+ key: key,
38
+ token: @options[:token] || token
39
+ }
40
+ end
41
+
42
+ def custom_fields
43
+ @options[:custom_fields]
44
+ end
45
+
46
+ def default_key
47
+ "{timestamp}-{unique-id}-#{SecureRandom.hex}-{filename}"
48
+ end
49
+
50
+ def store_path
51
+ store_path = @options[:store_path]
52
+ store_path = '/' + store_path if store_path.slice(0, 1) != '/'
53
+ store_path = store_path+ '/' if store_path.slice(-1) != '/'
54
+ end
55
+
56
+ def key
57
+ return store_path + @options[:key] if @options[:key]
58
+ return store_path + default_key
59
+ end
60
+
61
+ def action
62
+ @options[:action] || "http#{@options[:ssl] ? 's' : ''}://up.qiniu.com/"
63
+ end
64
+
65
+ def return_body
66
+ fields_array = []
67
+ fields_array.push '"etag": $(etag)'
68
+ fields_array.push '"fname": $(fname)'
69
+ fields_array.push '"fsize": $(fsize)'
70
+ fields_array.push '"mimeType": $(mimeType)'
71
+ fields_array.push '"imageInfo": $(imageInfo)'
72
+ fields_array.push '"exif": $(exif)'
73
+ fields_array.push '"endUser": $(endUser)'
74
+ fields_array.push '"key": $(key)'
75
+
76
+ custom_fields_array = []
77
+ @options[:custom_fields].each do |k,v|
78
+ custom_fields_array.push '"' + k.to_s + '": $(x:'+ k.to_s + ')'
79
+ end
80
+ custom_fields_json = '"custom_fields": {' + custom_fields_array.join(',') + '}'
81
+
82
+ fields_array.push custom_fields_json
83
+
84
+ '{'+ fields_array.join(',') +'}'
85
+ end
86
+
87
+ def token
88
+ Qiniu::RS.generate_upload_token scope: @options[:bucket],
89
+ escape: 1,
90
+ expires_in: @options[:expires_in],
91
+ return_body: return_body,
92
+ customer: @options[:customer]
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,4 @@
1
+ # coding: utf-8
2
+ module QiniuDirectUploader
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,11 @@
1
+ # coding: utf-8
2
+
3
+ require 'jquery-fileupload-rails' if defined?(Rails)
4
+
5
+ require "qiniu_direct_uploader/version"
6
+ require "qiniu_direct_uploader/uploader"
7
+ require "qiniu_direct_uploader/form_helper"
8
+
9
+ require 'qiniu_direct_uploader/engine' if defined?(Rails)
10
+
11
+ ActionView::Base.send(:include, QiniuDirectUploader::FormHelper) if defined?(ActionView::Base)
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'qiniu_direct_uploader/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "qiniu_direct_uploader"
8
+ s.version = QiniuDirectUploader::VERSION
9
+ s.authors = ["Marble Wu"]
10
+ s.email = ["huobazi@gmail.com"]
11
+ s.description = %q{Direct upload to a Qiniu storage bucket.}
12
+ s.summary = %q{Direct upload to a Qiniu storage bucket.}
13
+ s.homepage = ""
14
+ s.license = "MIT"
15
+
16
+ s.files = `git ls-files`.split($/)
17
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency 'rails', '>= 3.2'
22
+ s.add_dependency 'coffee-rails', '>= 3.2.1'
23
+ s.add_dependency 'sass-rails', '>= 3.2.5'
24
+
25
+ s.add_dependency 'qiniu-rs', '>= 3.4.5'
26
+
27
+ s.add_dependency "jquery-fileupload-rails", "~> 0.4.1"
28
+
29
+ s.add_development_dependency "bundler", "~> 1.3"
30
+ s.add_development_dependency "rake"
31
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qiniu_direct_uploader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marble Wu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coffee-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: sass-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.5
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.5
55
+ - !ruby/object:Gem::Dependency
56
+ name: qiniu-rs
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.4.5
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.4.5
69
+ - !ruby/object:Gem::Dependency
70
+ name: jquery-fileupload-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 0.4.1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 0.4.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '1.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '1.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Direct upload to a Qiniu storage bucket.
112
+ email:
113
+ - huobazi@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - app/assets/javascripts/qiniu_direct_uploader.js.coffee
124
+ - app/assets/stylesheets/qiniu_direct_uploader.css.scss
125
+ - lib/qiniu_direct_uploader.rb
126
+ - lib/qiniu_direct_uploader/engine.rb
127
+ - lib/qiniu_direct_uploader/form_helper.rb
128
+ - lib/qiniu_direct_uploader/uploader.rb
129
+ - lib/qiniu_direct_uploader/version.rb
130
+ - qiniu_direct_uploader.gemspec
131
+ homepage: ''
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.0.3
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Direct upload to a Qiniu storage bucket.
155
+ test_files: []