applicious_utils 0.1.30 → 0.1.31
Sign up to get free protection for your applications and to get access to all the features.
- data/app/helpers/uploads_helper.rb +126 -124
- data/lib/applicious_utils/version.rb +1 -1
- metadata +1 -1
@@ -1,148 +1,150 @@
|
|
1
1
|
module AppliciousUtils
|
2
|
+
module ViewHelpers
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
4
|
+
# Creates an instance of a plupload S3 file uploader
|
5
|
+
###
|
6
|
+
# required parameters:
|
7
|
+
###
|
8
|
+
# key s3 'path' to uploaded files
|
9
|
+
#
|
10
|
+
###
|
11
|
+
# optional parameters:
|
12
|
+
###
|
13
|
+
# s3_config_filename filename of s3 config yaml file (full path), defaults to "#{RAILS_ROOT}/config/amazon_s3.yml"
|
14
|
+
#
|
15
|
+
# content_type binary/octet-stream
|
16
|
+
#
|
17
|
+
# acl public-read
|
18
|
+
#
|
19
|
+
# expiration_date 10.hours.from_now.utc.iso8601
|
20
|
+
#
|
21
|
+
# max_filesize 2.megabytes
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
def s3_uploader(options = {})
|
24
|
+
options[:s3_config_filename] ||= "#{Rails.root}/config/amazon_s3.yml"
|
25
|
+
config = YAML.load_file(options[:s3_config_filename])[Rails.env].symbolize_keys
|
26
|
+
bucket = config[:bucket_name]
|
27
|
+
access_key_id = config[:access_key_id]
|
28
|
+
secret_access_key = config[:secret_access_key]
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
30
|
+
options[:key] ||= 'test' # folder on AWS to store file in
|
31
|
+
options[:acl] ||= 'public-read'
|
32
|
+
options[:expiration_date] ||= 10.hours.from_now.utc.iso8601
|
33
|
+
options[:max_filesize] ||= 500.megabytes
|
34
|
+
options[:content_type] ||= 'image/' # Videos would be binary/octet-stream
|
35
|
+
options[:filter_title] ||= 'Images'
|
36
|
+
options[:filter_extentions] ||= 'jpg,jpeg,gif,png,bmp'
|
36
37
|
|
37
|
-
|
38
|
+
id = options[:id] ? "_#{options[:id]}" : ''
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
40
|
+
policy = Base64.encode64(
|
41
|
+
"{'expiration': '#{options[:expiration_date]}',
|
42
|
+
'conditions': [
|
43
|
+
{'bucket': '#{bucket}'},
|
44
|
+
{'acl': '#{options[:acl]}'},
|
45
|
+
{'success_action_status': '201'},
|
46
|
+
['content-length-range', 0, #{options[:max_filesize]}],
|
47
|
+
['starts-with', '$key', ''],
|
48
|
+
['starts-with', '$Content-Type', ''],
|
49
|
+
['starts-with', '$name', ''],
|
50
|
+
['starts-with', '$Filename', '']
|
51
|
+
]
|
52
|
+
}").gsub(/\n|\r/, '')
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
signature = Base64.encode64(
|
55
|
+
OpenSSL::HMAC.digest(
|
56
|
+
OpenSSL::Digest::Digest.new('sha1'),
|
57
|
+
secret_access_key, policy)).gsub("\n","")
|
57
58
|
|
58
|
-
|
59
|
+
out = ""
|
59
60
|
|
60
|
-
|
61
|
+
out << javascript_tag("$(function() {
|
61
62
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
63
|
+
/*
|
64
|
+
* S3 Uploader instance
|
65
|
+
*/
|
66
|
+
// image uploader via plupload
|
67
|
+
var uploader = new plupload.Uploader({
|
68
|
+
runtimes : 'html5,flash,silverlight',
|
69
|
+
browse_button : 'pickfiles',
|
70
|
+
max_file_size : '500mb',
|
71
|
+
url : 'http://#{bucket}.s3.amazonaws.com/',
|
72
|
+
flash_swf_url: 'applicious/plupload/plupload.flash.swf',
|
73
|
+
silverlight_xap_url: '/applicious/plupload/plupload.silverlight.xap',
|
74
|
+
init : {
|
75
|
+
FilesAdded: function(up, files) {
|
76
|
+
plupload.each(files, function(file) {
|
77
|
+
if (up.files.length > 1) {
|
78
|
+
up.removeFile(file);
|
79
|
+
}
|
80
|
+
});
|
81
|
+
if (up.files.length >= 1) {
|
82
|
+
$('#pickfiles').fadeOut('slow');
|
83
|
+
}
|
84
|
+
},
|
85
|
+
FilesRemoved: function(up, files) {
|
86
|
+
if (up.files.length < 1) {
|
87
|
+
$('#pickfiles').fadeIn('slow');
|
78
88
|
}
|
79
|
-
});
|
80
|
-
if (up.files.length >= 1) {
|
81
|
-
$('#pickfiles').fadeOut('slow');
|
82
89
|
}
|
83
90
|
},
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
}
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
},
|
102
|
-
filters : [
|
103
|
-
{title : '#{options[:filter_title]}', extensions : '#{options[:filter_extentions]}'}
|
104
|
-
],
|
105
|
-
file_data_name: 'file'
|
106
|
-
});
|
91
|
+
multi_selection: false,
|
92
|
+
multipart: true,
|
93
|
+
multipart_params: {
|
94
|
+
'key': 'test/${filename}',
|
95
|
+
'Filename': '${filename}', // adding this to keep consistency across the runtimes
|
96
|
+
'acl': '#{options[:acl]}',
|
97
|
+
'Content-Type': '#{options[:content_type]}',
|
98
|
+
'success_action_status': '201',
|
99
|
+
'AWSAccessKeyId' : '#{access_key_id}',
|
100
|
+
'policy': '#{policy}',
|
101
|
+
'signature': '#{signature}'
|
102
|
+
},
|
103
|
+
filters : [
|
104
|
+
{title : '#{options[:filter_title]}', extensions : '#{options[:filter_extentions]}'}
|
105
|
+
],
|
106
|
+
file_data_name: 'file'
|
107
|
+
});
|
107
108
|
|
108
|
-
|
109
|
-
|
109
|
+
// instantiates the uploader
|
110
|
+
uploader.init();
|
110
111
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
112
|
+
// shows the progress bar and kicks off uploading
|
113
|
+
uploader.bind('FilesAdded', function(up, files) {
|
114
|
+
$('#progress_bar .ui-progress').css('width', '5%');
|
115
|
+
$('span.ui-label').show();
|
115
116
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
117
|
+
// start the uploader after the progress bar shows
|
118
|
+
$('#progress_bar').show(function () {
|
119
|
+
uploader.start();
|
120
|
+
});
|
121
|
+
});
|
121
122
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
123
|
+
// binds progress to progress bar
|
124
|
+
uploader.bind('UploadProgress', function(up, file) {
|
125
|
+
if(file.percent < 100){
|
126
|
+
$('#progress_bar .ui-progress').css('width', file.percent+'%');
|
127
|
+
}
|
128
|
+
else {
|
129
|
+
$('#progress_bar .ui-progress').css('width', '100%');
|
130
|
+
$('span.ui-label').text('Complete');
|
131
|
+
}
|
132
|
+
});
|
132
133
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
134
|
+
// shows error object in the browser console (for now)
|
135
|
+
uploader.bind('Error', function(up, error) {
|
136
|
+
// unfortunately PLUpload gives some extremely vague
|
137
|
+
// Flash error messages so you have to use WireShark
|
138
|
+
// for debugging them (read the README)
|
138
139
|
|
139
|
-
|
140
|
-
|
140
|
+
alert('There was an error. Check the browser console log for more info');
|
141
|
+
console.log('Expand the error object below to see the error. Use WireShark to debug.');
|
141
142
|
|
142
|
-
|
143
|
-
|
143
|
+
console.log(error);
|
144
|
+
});
|
144
145
|
|
145
|
-
|
146
|
+
});")
|
146
147
|
|
148
|
+
end
|
147
149
|
end
|
148
150
|
end
|