anaconda 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.gitignore +51 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +20 -0
- data/README.markdown +56 -0
- data/Rakefile +53 -0
- data/anaconda.gemspec +26 -0
- data/app/assets/javascripts/anaconda.js +5 -0
- data/app/assets/javascripts/anaconda_uploader.js.coffee +165 -0
- data/app/views/anaconda/_uploader_form_for.html.haml +29 -0
- data/lib/anaconda.rb +9 -0
- data/lib/anaconda/anaconda.rb +3 -0
- data/lib/anaconda/anaconda_for.rb +61 -0
- data/lib/anaconda/engine.rb +8 -0
- data/lib/anaconda/railtie.rb +10 -0
- data/lib/anaconda/upload_helper.rb +96 -0
- data/lib/anaconda/version.rb +5 -0
- data/test/helper.rb +18 -0
- data/test/test_anaconda.rb +7 -0
- data/vendor/assets/javascripts/.gitkeep +0 -0
- data/vendor/assets/javascripts/md5-min.js +9 -0
- data/vendor/assets/stylesheets/.gitkeep +0 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1607eb100c23426c78fc3be127fcea5ff7062510
|
4
|
+
data.tar.gz: 1a50a5e1fea15bb4055e1ef3f8b16806d2526527
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 319a37f584909a3187783e83b064bcc6a3a624b497a23cbfc7e25340dd512223515509783414756d5c848ef143d9de283689a72f2796d8ffcb72dc4c376b5f13
|
7
|
+
data.tar.gz: b3398c2a3895086e72ea43acc9face25564403f39d5724ed6a1afd57c49b1dad16b46a2dfa7ab253ebaba8806cee0fa6ddd17d85bde19c032d0379e665db7cfe
|
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
Gemfile.lock
|
2
|
+
|
3
|
+
# rcov generated
|
4
|
+
coverage
|
5
|
+
coverage.data
|
6
|
+
|
7
|
+
# rdoc generated
|
8
|
+
rdoc
|
9
|
+
|
10
|
+
# yard generated
|
11
|
+
doc
|
12
|
+
.yardoc
|
13
|
+
|
14
|
+
# bundler
|
15
|
+
.bundle
|
16
|
+
|
17
|
+
# jeweler generated
|
18
|
+
pkg
|
19
|
+
|
20
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
21
|
+
#
|
22
|
+
# * Create a file at ~/.gitignore
|
23
|
+
# * Include files you want ignored
|
24
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
25
|
+
#
|
26
|
+
# After doing this, these files will be ignored in all your git projects,
|
27
|
+
# saving you from having to 'pollute' every project you touch with them
|
28
|
+
#
|
29
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
30
|
+
#
|
31
|
+
# For MacOS:
|
32
|
+
#
|
33
|
+
.DS_Store
|
34
|
+
|
35
|
+
# For TextMate
|
36
|
+
#*.tmproj
|
37
|
+
#tmtags
|
38
|
+
|
39
|
+
# For emacs:
|
40
|
+
#*~
|
41
|
+
#\#*
|
42
|
+
#.\#*
|
43
|
+
|
44
|
+
# For vim:
|
45
|
+
#*.swp
|
46
|
+
|
47
|
+
# For redcar:
|
48
|
+
#.redcar
|
49
|
+
|
50
|
+
# For rubinius:
|
51
|
+
#*.rbc
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Ben McFadden
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# anaconda
|
2
|
+
|
3
|
+
Dead simple direct-to-s3 file uploading for your rails app.
|
4
|
+
|
5
|
+
## Alpha Warning
|
6
|
+
|
7
|
+
We intend to follow semantic versioning as of 1.0. Before that time breaking changes may occur, as development is very active.
|
8
|
+
|
9
|
+
If you require stability before that time, you are strongly encouraged to specify an exact version in your `Gemfile` to avoid updating to a version that breaks things for you.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add to your `Gemfile`
|
14
|
+
|
15
|
+
gem 'anaconda'
|
16
|
+
|
17
|
+
Then `bundle install`
|
18
|
+
|
19
|
+
* Asset updates
|
20
|
+
* AWS setup
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
* Controller changes (if any)
|
25
|
+
* Form setup
|
26
|
+
* Options
|
27
|
+
|
28
|
+
## Contributing to anaconda
|
29
|
+
|
30
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
31
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
32
|
+
* Fork the project.
|
33
|
+
* Start a feature/bugfix branch.
|
34
|
+
* Commit and push until you are happy with your contribution.
|
35
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
36
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
37
|
+
|
38
|
+
## Copyright
|
39
|
+
|
40
|
+
Copyright (c) 2014 Forge Apps, LLC. See LICENSE.txt for
|
41
|
+
further details.
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
Columns we're expecting (in the case of `anaconda_for :image`)
|
47
|
+
image_file_path
|
48
|
+
image_size
|
49
|
+
image_original_filename
|
50
|
+
|
51
|
+
Magic Columns we'll make
|
52
|
+
image_url
|
53
|
+
|
54
|
+
|
55
|
+
You'll have to make and run a migration for the columns you want:
|
56
|
+
`rails g migration AddAnacondaToUsers image_file_path:text image_size:integer image_original_filename:text`
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "anaconda"
|
18
|
+
gem.homepage = "http://github.com/ForgeApps/anaconda"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Dead simple file uploading to S3}
|
21
|
+
gem.description = %Q{Dead simple file uploading to S3}
|
22
|
+
gem.email = "ben@forgeapps.com"
|
23
|
+
gem.authors = ["Ben McFadden"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
# require 'rcov/rcovtask'
|
36
|
+
# Rcov::RcovTask.new do |test|
|
37
|
+
# test.libs << 'test'
|
38
|
+
# test.pattern = 'test/**/test_*.rb'
|
39
|
+
# test.verbose = true
|
40
|
+
# test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
# end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rdoc/task'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "anaconda #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/anaconda.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/anaconda/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "anaconda"
|
6
|
+
s.version = Anaconda::Rails::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
|
9
|
+
s.authors = ["Ben McFadden", "Jeff McFadden"]
|
10
|
+
s.date = "2014-01-07"
|
11
|
+
s.description = "Dead simple file uploading to S3"
|
12
|
+
s.email = "ben@forgeapps.com"
|
13
|
+
s.extra_rdoc_files = [
|
14
|
+
"LICENSE.txt"
|
15
|
+
]
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
|
18
|
+
s.homepage = "http://github.com/ForgeApps/anaconda"
|
19
|
+
s.licenses = ["MIT"]
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.rubygems_version = "2.0.3"
|
22
|
+
s.summary = "Dead simple file uploading to S3"
|
23
|
+
|
24
|
+
s.add_dependency "jquery-fileupload-rails", "~> 0.4.1"
|
25
|
+
s.add_dependency "javascript_dlog-rails", "~> 1.0.1"
|
26
|
+
end
|
@@ -0,0 +1,165 @@
|
|
1
|
+
class @AnacondaUploader
|
2
|
+
@debug_enabled: false
|
3
|
+
@upload_started: false
|
4
|
+
@audio_types = /(\.|\/)(wav|mp3|m4a|aiff|ogg|flac)$/i
|
5
|
+
@video_types = /(\.|\/)(mp[e]?g|mov|avi|mp4|m4v)$/i
|
6
|
+
@image_types = /(\.|\/)(jp[e]?g|png|bmp)$/i
|
7
|
+
@resource_types = /(\.|\/)(pdf|ppt[x]?|doc[x]?)$/i
|
8
|
+
|
9
|
+
|
10
|
+
constructor: (options = {}) ->
|
11
|
+
@limits = options.limits ? {}
|
12
|
+
@allowed_types = options.allowed_types ? []
|
13
|
+
@upload_details_container = $("##{options.upload_details_container}") ? $("#files")
|
14
|
+
@upload_button = $("##{options.upload_button_id}") ? $("#upload")
|
15
|
+
@upload_complete_post_url = options.upload_complete_post_url ? null
|
16
|
+
@upload_complete_form_to_fill = options.upload_complete_form_to_fill ? null
|
17
|
+
@upload_automatically = options.upload_automatically ? false
|
18
|
+
@resource = options.resource ? null
|
19
|
+
@attribute = options.attribute ? null
|
20
|
+
|
21
|
+
@files_for_upload = []
|
22
|
+
@base_key = $("#fileupload").data("base-key")
|
23
|
+
@setup_fileupload()
|
24
|
+
@setup_upload_button_handler()
|
25
|
+
|
26
|
+
setup_fileupload: ->
|
27
|
+
self = this
|
28
|
+
$('#fileupload').fileupload
|
29
|
+
dropZone: $("#dropzone")
|
30
|
+
add: (e, data) ->
|
31
|
+
self.add_file data
|
32
|
+
progress: (e, data) ->
|
33
|
+
DLog data
|
34
|
+
progress = parseInt(data.loaded / data.total * 100, 10)
|
35
|
+
DLog( "Progress: " + progress )
|
36
|
+
data.context.update_progress_to(progress)
|
37
|
+
|
38
|
+
done: (e, data) ->
|
39
|
+
self.file_completed_upload data
|
40
|
+
|
41
|
+
fail: (e, data) ->
|
42
|
+
alert("#{data.files[0].name} failed to upload.")
|
43
|
+
DLog("Upload failed:")
|
44
|
+
DLog(e)
|
45
|
+
DLog(data)
|
46
|
+
DLog(data.errorThrown )
|
47
|
+
DLog(data.textStatus )
|
48
|
+
DLog(data.jqXHR )
|
49
|
+
|
50
|
+
$(document).bind 'drop dragover', (e) ->
|
51
|
+
e.preventDefault()
|
52
|
+
|
53
|
+
setup_upload_button_handler: ->
|
54
|
+
self = this
|
55
|
+
@upload_button.off("click").click (e) ->
|
56
|
+
e.preventDefault()
|
57
|
+
self.upload_files()
|
58
|
+
|
59
|
+
files_by_type: (type) ->
|
60
|
+
matches = []
|
61
|
+
for v,i in @files_for_upload
|
62
|
+
if v.media_type == type
|
63
|
+
matches.push v
|
64
|
+
return matches
|
65
|
+
|
66
|
+
upload_files: ->
|
67
|
+
media_type = null
|
68
|
+
for v,i in @files_for_upload
|
69
|
+
$("input#key").val "#{@base_key}/${filename}"
|
70
|
+
v.submit()
|
71
|
+
|
72
|
+
is_allowed_type: (upload_file) ->
|
73
|
+
if 0 == @allowed_types.length || 0 <= @allowed_types.indexOf upload_file.media_type
|
74
|
+
return true
|
75
|
+
return false
|
76
|
+
|
77
|
+
is_within_limits: (upload_file) ->
|
78
|
+
if !@limits[upload_file.media_type]? || @limits[upload_file.media_type] > @files_by_type(upload_file.media_type).length
|
79
|
+
return true
|
80
|
+
return false
|
81
|
+
|
82
|
+
reset: ->
|
83
|
+
@files_for_upload = []
|
84
|
+
@upload_details_container.html ''
|
85
|
+
|
86
|
+
add_file: (data) ->
|
87
|
+
upload_file = new AnacondaUploadFile data
|
88
|
+
if @is_allowed_type(upload_file)
|
89
|
+
if @is_within_limits(upload_file)
|
90
|
+
@files_for_upload.push upload_file
|
91
|
+
DLog(upload_file)
|
92
|
+
@upload_details_container.append "<div id='upload_file_#{upload_file.id}' class='upload-file #{upload_file.media_type}'><span class='file-name'>#{upload_file.file.name}</span><span class='size'>#{upload_file.file.size}</span><span class='progress-percent'></span><div class='progress'><span class='progress-bar'></span></div></div>"
|
93
|
+
|
94
|
+
if @upload_automatically
|
95
|
+
upload_file.submit()
|
96
|
+
|
97
|
+
else
|
98
|
+
alert "Only #{@limits[upload_file.media_type]} #{upload_file.media_type} files are allowed"
|
99
|
+
else
|
100
|
+
alert "#{upload_file.file.name} is a #{upload_file.media_type} file. Only #{@allowed_types.join(", ")} files are allowed."
|
101
|
+
file_completed_upload: (data) ->
|
102
|
+
upload_file = data.context
|
103
|
+
DLog "#{upload_file.file.name} completed uploading"
|
104
|
+
DLog upload_file
|
105
|
+
|
106
|
+
if @upload_complete_post_url? && @upload_complete_post_url != ""
|
107
|
+
DLog "will now post to #{@upload_complete_post_url}"
|
108
|
+
|
109
|
+
file_data = {}
|
110
|
+
file_data[@resource] = {}
|
111
|
+
file_data[@resource]["#{@attribute}_file_path"] = "#{@base_key}/#{upload_file.file.name}"
|
112
|
+
file_data[@resource]["#{@attribute}_filename"] = upload_file.file.name
|
113
|
+
file_data[@resource]["#{@attribute}_size"] = upload_file.file.size
|
114
|
+
file_data[@resource]["#{@attribute}_type"] = upload_file.file.media_type
|
115
|
+
upload_file = this
|
116
|
+
$.ajax({
|
117
|
+
type: 'PATCH',
|
118
|
+
url: @upload_complete_post_url,
|
119
|
+
data: $.param(file_data)
|
120
|
+
success: (data, textStatus, jqXHR) ->
|
121
|
+
DLog "file completed handler complete"
|
122
|
+
DLog data
|
123
|
+
#TODO: handle a failure on this POST
|
124
|
+
})
|
125
|
+
|
126
|
+
if @upload_complete_form_to_fill? && @upload_complete_form_to_fill != ""
|
127
|
+
DLog "will now fill form #{@upload_complete_form_to_fill}"
|
128
|
+
|
129
|
+
DLog "#{@resource}_#{@attribute}_file_path"
|
130
|
+
|
131
|
+
$( @upload_complete_form_to_fill + ' ' + '#' + "#{@resource}_#{@attribute}_file_path" ).val( "#{@base_key}/#{upload_file.file.name}" )
|
132
|
+
$( @upload_complete_form_to_fill + ' ' + '#' + "#{@resource}_#{@attribute}_filename" ).val( upload_file.file.name )
|
133
|
+
$( @upload_complete_form_to_fill + ' ' + '#' + "#{@resource}_#{@attribute}_size" ).val( upload_file.file.size )
|
134
|
+
$( @upload_complete_form_to_fill + ' ' + '#' + "#{@resource}_#{@attribute}_type" ).val( upload_file.file.type )
|
135
|
+
|
136
|
+
|
137
|
+
class @AnacondaUploadFile
|
138
|
+
|
139
|
+
constructor: (@data) ->
|
140
|
+
@file = @data.files[0]
|
141
|
+
@media_type = @get_media_type()
|
142
|
+
@id = @get_id()
|
143
|
+
|
144
|
+
@set_context()
|
145
|
+
get_id: ->
|
146
|
+
hex_md5( "#{@file.name} #{@file.size}" )
|
147
|
+
get_media_type: ->
|
148
|
+
if AnacondaUploader.audio_types.test(@file.type) || AnacondaUploader.audio_types.test(@file.name)
|
149
|
+
media_type = "audio"
|
150
|
+
else if AnacondaUploader.video_types.test(@file.type) || AnacondaUploader.video_types.test(@file.name)
|
151
|
+
media_type = "video"
|
152
|
+
else if AnacondaUploader.image_types.test(@file.type) || AnacondaUploader.image_types.test(@file.name)
|
153
|
+
media_type = "image"
|
154
|
+
else if AnacondaUploader.resource_types.test(@file.type) || AnacondaUploader.resource_types.test(@file.name)
|
155
|
+
media_type = "resource"
|
156
|
+
else
|
157
|
+
media_type = "unknown"
|
158
|
+
return media_type
|
159
|
+
set_context: ->
|
160
|
+
@data.context = this
|
161
|
+
submit: ->
|
162
|
+
@data.submit()
|
163
|
+
update_progress_to: (progress) ->
|
164
|
+
$("#upload_file_#{@id} .progress-percent").html progress
|
165
|
+
$("#upload_file_#{@id}").find('.progress-bar').css('width', progress + '%')
|
@@ -0,0 +1,29 @@
|
|
1
|
+
= anaconda_uploader_form options
|
2
|
+
|
3
|
+
%button#upload Upload
|
4
|
+
|
5
|
+
%strong Files:
|
6
|
+
#files
|
7
|
+
|
8
|
+
:javascript
|
9
|
+
(function() {
|
10
|
+
jQuery(function() {
|
11
|
+
return window.uploader = new AnacondaUploader({
|
12
|
+
limits: {
|
13
|
+
audio: 1,
|
14
|
+
video: 1,
|
15
|
+
resource: 1,
|
16
|
+
image: 1
|
17
|
+
},
|
18
|
+
allowed_types: ["image"],
|
19
|
+
upload_details_container: "files",
|
20
|
+
upload_button_id: "upload",
|
21
|
+
upload_complete_post_url: "#{options[:form_options][:post_url]}",
|
22
|
+
upload_complete_form_to_fill: "#{options[:form_options][:form_el]}",
|
23
|
+
upload_automatically: "#{options[:form_options][:auto_upload]}",
|
24
|
+
resource: "#{resource.class.to_s.underscore}",
|
25
|
+
attribute: "#{options[:as]}"
|
26
|
+
});
|
27
|
+
});
|
28
|
+
|
29
|
+
}).call(this);
|
data/lib/anaconda.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
module Anaconda
|
2
|
+
module Model
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.send :extend, ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def anaconda_for( anaconda_columns, options = {})
|
11
|
+
send :include, InstanceMethods
|
12
|
+
|
13
|
+
anaconda_columns = [anaconda_columns] if anaconda_columns.kind_of?(Symbol) || anaconda_columns.kind_of?(String)
|
14
|
+
class_attribute :anaconda_columns
|
15
|
+
self.anaconda_columns = anaconda_columns.collect{ |c| c.to_sym }
|
16
|
+
# Class.anaconda_columns is now an array of symbols
|
17
|
+
|
18
|
+
class_attribute :anaconda_options
|
19
|
+
self.anaconda_options = options.reverse_merge(
|
20
|
+
aws_access_key_id: AWS[:aws_access_key],
|
21
|
+
aws_secret_access_key: AWS[:aws_secret_key],
|
22
|
+
bucket: AWS[:aws_bucket],
|
23
|
+
acl: "public-read",
|
24
|
+
max_file_size: 500.megabytes,
|
25
|
+
base_key: "#{self.to_s.pluralize.downcase}/#{anaconda_columns.first.to_s.pluralize}/#{(0...32).map{(65+rand(26)).chr}.join.downcase}"
|
26
|
+
)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
module InstanceMethods
|
30
|
+
def method_missing(method, *args, &block)
|
31
|
+
checking_column = checking_method = nil
|
32
|
+
if self.class.anaconda_columns.present? && self.class.anaconda_columns.any? do |column|
|
33
|
+
checking_column = column
|
34
|
+
Anaconda::MagicMethods.any? do |magic_method|
|
35
|
+
checking_method = magic_method
|
36
|
+
"#{column.to_s}_#{magic_method.to_s}" == method.to_s
|
37
|
+
end
|
38
|
+
end
|
39
|
+
case checking_method
|
40
|
+
when :url
|
41
|
+
magic_url(checking_column)
|
42
|
+
else
|
43
|
+
super
|
44
|
+
end
|
45
|
+
else
|
46
|
+
super
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
def magic_url(column_name)
|
52
|
+
if send("#{column_name}_stored_privately")
|
53
|
+
aws = Fog::Storage.new({:provider => 'AWS', :aws_access_key_id => AWS[:aws_access_key], :aws_secret_access_key => AWS[:aws_secret_key]})
|
54
|
+
aws.get_object_https_url(AWS[:aws_bucket], send("#{column_name}_file_path"), 1.hour.from_now)
|
55
|
+
else
|
56
|
+
"https://s3.amazonaws.com/#{AWS[:aws_bucket]}/#{send("#{column_name}_file_path")}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module Anaconda
|
2
|
+
# Vastly inspired by http://railscasts.com/episodes/383-uploading-to-amazon-s3
|
3
|
+
module UploadHelper
|
4
|
+
def anaconda_uploader_form_for(instance, attribute, form_options = {})
|
5
|
+
a_class = instance.class unless instance.kind_of? Class
|
6
|
+
options = a_class.anaconda_options.dup
|
7
|
+
options[:base_key] = instance.send(options[:base_key].to_s) if options[:base_key].kind_of? Symbol
|
8
|
+
render(:template =>"anaconda/_uploader_form_for.html.haml", :locals => {resource: instance, options: options.merge(as: attribute, form_options: form_options)}, layout: false).to_s
|
9
|
+
end
|
10
|
+
def anaconda_uploader_form(options = {}, &block)
|
11
|
+
uploader = S3Uploader.new(options)
|
12
|
+
form_tag(uploader.url, uploader.form_options) do
|
13
|
+
uploader.fields.map do |name, value|
|
14
|
+
hidden_field_tag(name, value)
|
15
|
+
end.join.html_safe + file_field_tag("file").html_safe# + capture(&block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class S3Uploader
|
20
|
+
def initialize(options)
|
21
|
+
@options = options.reverse_merge(
|
22
|
+
id: "fileupload",
|
23
|
+
aws_access_key_id: AWS[:aws_access_key],
|
24
|
+
aws_secret_access_key: AWS[:aws_secret_key],
|
25
|
+
bucket: AWS[:aws_bucket],
|
26
|
+
acl: "public-read",
|
27
|
+
expiration: 10.hours.from_now,
|
28
|
+
max_file_size: 500.megabytes,
|
29
|
+
as: "file"
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
def form_options
|
34
|
+
{
|
35
|
+
id: @options[:id],
|
36
|
+
method: "post",
|
37
|
+
authenticity_token: false,
|
38
|
+
multipart: true,
|
39
|
+
data: {
|
40
|
+
post: @options[:post],
|
41
|
+
as: @options[:as],
|
42
|
+
base_key: base_key
|
43
|
+
}
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
def fields
|
48
|
+
{
|
49
|
+
:key => key,
|
50
|
+
:acl => @options[:acl],
|
51
|
+
:policy => policy,
|
52
|
+
:signature => signature,
|
53
|
+
"AWSAccessKeyId" => @options[:aws_access_key_id],
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
def key
|
58
|
+
@key ||= "#{base_key}/${filename}"
|
59
|
+
end
|
60
|
+
|
61
|
+
def base_key
|
62
|
+
@options[:base_key]
|
63
|
+
end
|
64
|
+
|
65
|
+
def url
|
66
|
+
"https://s3.amazonaws.com/#{@options[:bucket]}/"
|
67
|
+
end
|
68
|
+
|
69
|
+
def policy
|
70
|
+
Base64.encode64(policy_data.to_json).gsub("\n", "")
|
71
|
+
end
|
72
|
+
|
73
|
+
def policy_data
|
74
|
+
{
|
75
|
+
expiration: @options[:expiration],
|
76
|
+
conditions: [
|
77
|
+
["starts-with", "$utf8", ""],
|
78
|
+
["starts-with", "$key", base_key],
|
79
|
+
["content-length-range", 1, @options[:max_file_size]],
|
80
|
+
{bucket: @options[:bucket]},
|
81
|
+
{acl: @options[:acl]}
|
82
|
+
]
|
83
|
+
}
|
84
|
+
end
|
85
|
+
|
86
|
+
def signature
|
87
|
+
Base64.encode64(
|
88
|
+
OpenSSL::HMAC.digest(
|
89
|
+
OpenSSL::Digest::Digest.new('sha1'),
|
90
|
+
@options[:aws_secret_access_key], policy
|
91
|
+
)
|
92
|
+
).gsub("\n", "")
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'anaconda'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
File without changes
|
@@ -0,0 +1,9 @@
|
|
1
|
+
/*
|
2
|
+
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
|
3
|
+
* Digest Algorithm, as defined in RFC 1321.
|
4
|
+
* Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
|
5
|
+
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
|
6
|
+
* Distributed under the BSD License
|
7
|
+
* See http://pajhome.org.uk/crypt/md5 for more info.
|
8
|
+
*/
|
9
|
+
var hexcase=0;function hex_md5(a){return rstr2hex(rstr_md5(str2rstr_utf8(a)))}function hex_hmac_md5(a,b){return rstr2hex(rstr_hmac_md5(str2rstr_utf8(a),str2rstr_utf8(b)))}function md5_vm_test(){return hex_md5("abc").toLowerCase()=="900150983cd24fb0d6963f7d28e17f72"}function rstr_md5(a){return binl2rstr(binl_md5(rstr2binl(a),a.length*8))}function rstr_hmac_md5(c,f){var e=rstr2binl(c);if(e.length>16){e=binl_md5(e,c.length*8)}var a=Array(16),d=Array(16);for(var b=0;b<16;b++){a[b]=e[b]^909522486;d[b]=e[b]^1549556828}var g=binl_md5(a.concat(rstr2binl(f)),512+f.length*8);return binl2rstr(binl_md5(d.concat(g),512+128))}function rstr2hex(c){try{hexcase}catch(g){hexcase=0}var f=hexcase?"0123456789ABCDEF":"0123456789abcdef";var b="";var a;for(var d=0;d<c.length;d++){a=c.charCodeAt(d);b+=f.charAt((a>>>4)&15)+f.charAt(a&15)}return b}function str2rstr_utf8(c){var b="";var d=-1;var a,e;while(++d<c.length){a=c.charCodeAt(d);e=d+1<c.length?c.charCodeAt(d+1):0;if(55296<=a&&a<=56319&&56320<=e&&e<=57343){a=65536+((a&1023)<<10)+(e&1023);d++}if(a<=127){b+=String.fromCharCode(a)}else{if(a<=2047){b+=String.fromCharCode(192|((a>>>6)&31),128|(a&63))}else{if(a<=65535){b+=String.fromCharCode(224|((a>>>12)&15),128|((a>>>6)&63),128|(a&63))}else{if(a<=2097151){b+=String.fromCharCode(240|((a>>>18)&7),128|((a>>>12)&63),128|((a>>>6)&63),128|(a&63))}}}}}return b}function rstr2binl(b){var a=Array(b.length>>2);for(var c=0;c<a.length;c++){a[c]=0}for(var c=0;c<b.length*8;c+=8){a[c>>5]|=(b.charCodeAt(c/8)&255)<<(c%32)}return a}function binl2rstr(b){var a="";for(var c=0;c<b.length*32;c+=8){a+=String.fromCharCode((b[c>>5]>>>(c%32))&255)}return a}function binl_md5(p,k){p[k>>5]|=128<<((k)%32);p[(((k+64)>>>9)<<4)+14]=k;var o=1732584193;var n=-271733879;var m=-1732584194;var l=271733878;for(var g=0;g<p.length;g+=16){var j=o;var h=n;var f=m;var e=l;o=md5_ff(o,n,m,l,p[g+0],7,-680876936);l=md5_ff(l,o,n,m,p[g+1],12,-389564586);m=md5_ff(m,l,o,n,p[g+2],17,606105819);n=md5_ff(n,m,l,o,p[g+3],22,-1044525330);o=md5_ff(o,n,m,l,p[g+4],7,-176418897);l=md5_ff(l,o,n,m,p[g+5],12,1200080426);m=md5_ff(m,l,o,n,p[g+6],17,-1473231341);n=md5_ff(n,m,l,o,p[g+7],22,-45705983);o=md5_ff(o,n,m,l,p[g+8],7,1770035416);l=md5_ff(l,o,n,m,p[g+9],12,-1958414417);m=md5_ff(m,l,o,n,p[g+10],17,-42063);n=md5_ff(n,m,l,o,p[g+11],22,-1990404162);o=md5_ff(o,n,m,l,p[g+12],7,1804603682);l=md5_ff(l,o,n,m,p[g+13],12,-40341101);m=md5_ff(m,l,o,n,p[g+14],17,-1502002290);n=md5_ff(n,m,l,o,p[g+15],22,1236535329);o=md5_gg(o,n,m,l,p[g+1],5,-165796510);l=md5_gg(l,o,n,m,p[g+6],9,-1069501632);m=md5_gg(m,l,o,n,p[g+11],14,643717713);n=md5_gg(n,m,l,o,p[g+0],20,-373897302);o=md5_gg(o,n,m,l,p[g+5],5,-701558691);l=md5_gg(l,o,n,m,p[g+10],9,38016083);m=md5_gg(m,l,o,n,p[g+15],14,-660478335);n=md5_gg(n,m,l,o,p[g+4],20,-405537848);o=md5_gg(o,n,m,l,p[g+9],5,568446438);l=md5_gg(l,o,n,m,p[g+14],9,-1019803690);m=md5_gg(m,l,o,n,p[g+3],14,-187363961);n=md5_gg(n,m,l,o,p[g+8],20,1163531501);o=md5_gg(o,n,m,l,p[g+13],5,-1444681467);l=md5_gg(l,o,n,m,p[g+2],9,-51403784);m=md5_gg(m,l,o,n,p[g+7],14,1735328473);n=md5_gg(n,m,l,o,p[g+12],20,-1926607734);o=md5_hh(o,n,m,l,p[g+5],4,-378558);l=md5_hh(l,o,n,m,p[g+8],11,-2022574463);m=md5_hh(m,l,o,n,p[g+11],16,1839030562);n=md5_hh(n,m,l,o,p[g+14],23,-35309556);o=md5_hh(o,n,m,l,p[g+1],4,-1530992060);l=md5_hh(l,o,n,m,p[g+4],11,1272893353);m=md5_hh(m,l,o,n,p[g+7],16,-155497632);n=md5_hh(n,m,l,o,p[g+10],23,-1094730640);o=md5_hh(o,n,m,l,p[g+13],4,681279174);l=md5_hh(l,o,n,m,p[g+0],11,-358537222);m=md5_hh(m,l,o,n,p[g+3],16,-722521979);n=md5_hh(n,m,l,o,p[g+6],23,76029189);o=md5_hh(o,n,m,l,p[g+9],4,-640364487);l=md5_hh(l,o,n,m,p[g+12],11,-421815835);m=md5_hh(m,l,o,n,p[g+15],16,530742520);n=md5_hh(n,m,l,o,p[g+2],23,-995338651);o=md5_ii(o,n,m,l,p[g+0],6,-198630844);l=md5_ii(l,o,n,m,p[g+7],10,1126891415);m=md5_ii(m,l,o,n,p[g+14],15,-1416354905);n=md5_ii(n,m,l,o,p[g+5],21,-57434055);o=md5_ii(o,n,m,l,p[g+12],6,1700485571);l=md5_ii(l,o,n,m,p[g+3],10,-1894986606);m=md5_ii(m,l,o,n,p[g+10],15,-1051523);n=md5_ii(n,m,l,o,p[g+1],21,-2054922799);o=md5_ii(o,n,m,l,p[g+8],6,1873313359);l=md5_ii(l,o,n,m,p[g+15],10,-30611744);m=md5_ii(m,l,o,n,p[g+6],15,-1560198380);n=md5_ii(n,m,l,o,p[g+13],21,1309151649);o=md5_ii(o,n,m,l,p[g+4],6,-145523070);l=md5_ii(l,o,n,m,p[g+11],10,-1120210379);m=md5_ii(m,l,o,n,p[g+2],15,718787259);n=md5_ii(n,m,l,o,p[g+9],21,-343485551);o=safe_add(o,j);n=safe_add(n,h);m=safe_add(m,f);l=safe_add(l,e)}return Array(o,n,m,l)}function md5_cmn(h,e,d,c,g,f){return safe_add(bit_rol(safe_add(safe_add(e,h),safe_add(c,f)),g),d)}function md5_ff(g,f,k,j,e,i,h){return md5_cmn((f&k)|((~f)&j),g,f,e,i,h)}function md5_gg(g,f,k,j,e,i,h){return md5_cmn((f&j)|(k&(~j)),g,f,e,i,h)}function md5_hh(g,f,k,j,e,i,h){return md5_cmn(f^k^j,g,f,e,i,h)}function md5_ii(g,f,k,j,e,i,h){return md5_cmn(k^(f|(~j)),g,f,e,i,h)}function safe_add(a,d){var c=(a&65535)+(d&65535);var b=(a>>16)+(d>>16)+(c>>16);return(b<<16)|(c&65535)}function bit_rol(a,b){return(a<<b)|(a>>>(32-b))};
|
File without changes
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: anaconda
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben McFadden
|
8
|
+
- Jeff McFadden
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-01-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: jquery-fileupload-rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 0.4.1
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 0.4.1
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: javascript_dlog-rails
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.0.1
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 1.0.1
|
42
|
+
description: Dead simple file uploading to S3
|
43
|
+
email: ben@forgeapps.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files:
|
47
|
+
- LICENSE.txt
|
48
|
+
files:
|
49
|
+
- .document
|
50
|
+
- .gitignore
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.markdown
|
54
|
+
- Rakefile
|
55
|
+
- anaconda.gemspec
|
56
|
+
- app/assets/javascripts/anaconda.js
|
57
|
+
- app/assets/javascripts/anaconda_uploader.js.coffee
|
58
|
+
- app/views/anaconda/_uploader_form_for.html.haml
|
59
|
+
- lib/anaconda.rb
|
60
|
+
- lib/anaconda/anaconda.rb
|
61
|
+
- lib/anaconda/anaconda_for.rb
|
62
|
+
- lib/anaconda/engine.rb
|
63
|
+
- lib/anaconda/railtie.rb
|
64
|
+
- lib/anaconda/upload_helper.rb
|
65
|
+
- lib/anaconda/version.rb
|
66
|
+
- test/helper.rb
|
67
|
+
- test/test_anaconda.rb
|
68
|
+
- vendor/assets/javascripts/.gitkeep
|
69
|
+
- vendor/assets/javascripts/md5-min.js
|
70
|
+
- vendor/assets/stylesheets/.gitkeep
|
71
|
+
homepage: http://github.com/ForgeApps/anaconda
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.2.1
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Dead simple file uploading to S3
|
95
|
+
test_files: []
|