rails_admin_s3_file 0.1.3

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: f0fcbbebb001f9bf48ca88284a48dd85a9fc3884
4
+ data.tar.gz: 94fe660ea68f93ca1e5bc9dc4362779930f3f114
5
+ SHA512:
6
+ metadata.gz: 7a91ed3f9a31ce769a22e325adbf80573501cf5bec90c3f64c973d1e83c7de85b84301fe6ca13273ec2390370f13e4a7dfa7ab872cd2105e0454946d0bc489f5
7
+ data.tar.gz: af2eb7b4432028653463bf3aee7d2624cb16bb02f6a520aa353a5801fad024c4c54a2ff71465dd7e2b9fe743691cbfb59d6409d9d358cb84a0d985c3632446ab
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Oscar Siniscalchi
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.md ADDED
@@ -0,0 +1,39 @@
1
+ # RailsAdminS3File
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+
6
+ - Add a new field to your model as string.
7
+ - Configure the field on your rails_admin.rb initializer
8
+
9
+ ```
10
+ field :your_field, :s3_file do
11
+ upload_path "#{ENV['AWS_ENV']}/videos/" # mandatory
12
+ upload_extension 'mp4' # mandatory
13
+ upload_content_type 'video' # Optional default video
14
+ upload_acl 'public-read' # Optional default public-read options
15
+ end
16
+ ```
17
+
18
+ ## Installation
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'rails_admin_s3_file'
23
+ ```
24
+
25
+ And then execute:
26
+ ```bash
27
+ $ bundle
28
+ ```
29
+
30
+ Or install it yourself as:
31
+ ```bash
32
+ $ gem install rails_admin_s3_file
33
+ ```
34
+
35
+ ## Contributing
36
+ Contribution directions go here.
37
+
38
+ ## License
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RailsAdminS3File'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ load 'rails/tasks/statistics.rake'
18
+
19
+ require 'bundler/gem_tasks'
@@ -0,0 +1,100 @@
1
+ - signed_post = field.presigned_post
2
+
3
+ = javascript_tag do
4
+ :plain
5
+ #{field.name}Uploader = {
6
+ _init: function() {
7
+ $('##{field.name}-file-selector').on('change', this.uploadToS3);
8
+ },
9
+ uploadToS3: function(){
10
+ var url = '#{signed_post.url}',
11
+ data = new FormData(),
12
+ file = $(this)[0].files[0];
13
+
14
+ $('.s3-fields input[type=hidden]', #{field.name}Uploader.uploaderWrapper).each(function(){
15
+ input = $(this)[0];
16
+ data.append(input.name, input.value);
17
+ });
18
+
19
+ data.append('file', file);
20
+
21
+ #{field.name}Uploader.toggleProgressBar();
22
+
23
+ $.ajax({
24
+ url: url,
25
+ type: 'post',
26
+ data: data,
27
+ cache: false,
28
+ processData: false,
29
+ contentType: false,
30
+ dataType: 'json',
31
+ complete: function(res, status) {
32
+ #{field.name}Uploader.toggleSuccessUpload(res.getResponseHeader('Location'));
33
+ },
34
+ xhr: #{field.name}Uploader.customXhr
35
+ });
36
+ },
37
+ uploaderWrapper: '.s3-uploader-#{field.name}',
38
+ toggleProgressBar: function(){
39
+ $('.progress', #{field.name}Uploader.uploaderWrapper).show();
40
+ $('.help', #{field.name}Uploader.uploaderWrapper).hide();
41
+ $('input[type=file]', #{field.name}Uploader.uploaderWrapper).hide();
42
+ $('button.upload-content', #{field.name}Uploader.uploaderWrapper).hide();
43
+ },
44
+ toggleSuccessUpload: function(url){
45
+ $('input.uploaded-file-url', #{field.name}Uploader.uploaderWrapper).val(url);
46
+ $('.progress', #{field.name}Uploader.uploaderWrapper).hide();
47
+ var video = $('<video />', {
48
+ src: url,
49
+ type: 'video/mp4',
50
+ width: 200,
51
+ controls: true
52
+ });
53
+ $('input[type=file]', #{field.name}Uploader.uploaderWrapper).remove();
54
+ video.appendTo($('.success', #{field.name}Uploader.uploaderWrapper));
55
+ $('.success', #{field.name}Uploader.uploaderWrapper).show();
56
+ },
57
+ customXhr: function() {
58
+ var xhr = new window.XMLHttpRequest();
59
+ xhr.upload.addEventListener("progress", function (evt) {
60
+ if (evt.lengthComputable) {
61
+ var percentComplete = evt.loaded / evt.total;
62
+ $('.progress-bar', #{field.name}Uploader.uploaderWrapper).css({
63
+ width: percentComplete * 100 + '%'
64
+ });
65
+ if (percentComplete === 1) {
66
+ $('.progress-bar', #{field.name}Uploader.uploaderWrapper).hide()
67
+ }
68
+ }
69
+ }, false);
70
+ xhr.addEventListener("progress", function (evt) {
71
+ if (evt.lengthComputable) {
72
+ var percentComplete = evt.loaded / evt.total;
73
+ $('.progress', #{field.name}Uploader.uploaderWrapper).css({
74
+ width: percentComplete * 100 + '%'
75
+ });
76
+ }
77
+ }, false);
78
+
79
+ return xhr;
80
+ }
81
+ };
82
+
83
+ .s3-uploader-wrapper{ class: "s3-uploader-#{field.name}" }
84
+ .s3-fields
85
+ - signed_post.fields.each_key do |key|
86
+ %input{ type: 'hidden', name: key, value: signed_post.fields[key] }
87
+ %input{ type: 'hidden', name: 'Content-Type', value: 'video/mp4' }
88
+
89
+ .file-selector
90
+ %label Choose the asset to upload
91
+ %input{ type: 'file', name: "#{field.name}-file", id: "#{field.name}-file-selector" }
92
+ .progress{ style: 'display:none' }
93
+ .progress-bar.progress-bar-info{ role: 'progressbar', style:'width:50%' }
94
+ Uploading ... (please wait)
95
+ %p.help Assets will be uploaded to s3
96
+ .success{ style: 'display:none' }
97
+
98
+ = form.send :hidden_field, field.method_name, field.html_attributes.merge(class: 'uploaded-file-url')
99
+ = javascript_tag do
100
+ $(document).ready(function(){ #{field.name}Uploader._init(); });
@@ -0,0 +1,4 @@
1
+ module RailsAdminS3File
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module RailsAdminS3File
2
+ VERSION = '0.1.3'
3
+ end
@@ -0,0 +1,75 @@
1
+ require "rails_admin_s3_file/engine"
2
+
3
+ module RailsAdminS3File
4
+ end
5
+
6
+ require 'rails_admin/config/fields'
7
+ require 'rails_admin/config/fields/base'
8
+
9
+ module RailsAdmin
10
+ module Config
11
+ module Fields
12
+ module Types
13
+ class S3File < RailsAdmin::Config::Fields::Base
14
+ RailsAdmin::Config::Fields::Types::register(self)
15
+
16
+ def initialize(*args, &block)
17
+ Aws.config.update(
18
+ region: ENV['AWS_REGION'] || 'us-east-1',
19
+ credentials: Aws::Credentials
20
+ .new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
21
+ )
22
+
23
+ super *args, &block
24
+ end
25
+
26
+ register_instance_option(:partial) do
27
+ :form_s3_file
28
+ end
29
+
30
+ register_instance_option(:upload_path) do
31
+ 'rails_admin_s3_uploads'
32
+ end
33
+
34
+ register_instance_option(:upload_extension) do
35
+ 'mp4'
36
+ end
37
+
38
+ register_instance_option(:upload_acl) do
39
+ 'public-read'
40
+ end
41
+
42
+ register_instance_option(:upload_content_type) do
43
+ 'video'
44
+ end
45
+
46
+ def upload_key
47
+ @upload_key ||= "#{upload_path}/#{SecureRandom.uuid}.#{upload_extension}"
48
+ end
49
+
50
+ def s3_bucket
51
+ @bucket ||= Aws::S3::Resource.new.bucket(ENV['FOG_DIRECTORY'])
52
+ end
53
+
54
+ def presigned_post
55
+ s3_bucket.presigned_post(
56
+ key: upload_key,
57
+ acl: upload_acl,
58
+ content_type_starts_with: upload_content_type,
59
+ expires: ::Time.now + 1.hour
60
+ )
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
69
+ if properties.name == :mp4
70
+ fields << RailsAdmin::Config::Fields::Types::S3File.new(parent, properties.name, properties)
71
+ true
72
+ else
73
+ false
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_admin_s3_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Oscar Siniscalchi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-24 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: 5.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2'
41
+ description: Upload big files directly to s3 and keep your server clean.
42
+ email:
43
+ - oscar.siniscalchi@neonroots.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/views/rails_admin/main/_form_s3_file.html.haml
52
+ - lib/rails_admin_s3_file.rb
53
+ - lib/rails_admin_s3_file/engine.rb
54
+ - lib/rails_admin_s3_file/version.rb
55
+ homepage: https://github.com/neonroots/rails_admin_s3_file
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.5.1
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Direct Upload to S3 from Rails Admin
79
+ test_files: []