async_paperclip_uploader 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +6 -0
- data/async_paperclip_uploader.gemspec +38 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/async_paperclip_uploader.rb +2 -0
- data/lib/async_paperclip_uploader/permanent.rb +29 -0
- data/lib/async_paperclip_uploader/temporary.rb +67 -0
- data/lib/async_paperclip_uploader/version.rb +3 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5d77f312cfc665bb0f172d455db26f5b63946b08233e6e63bd0ad03b73bc0f3e
|
4
|
+
data.tar.gz: 1f6cb6ee735509f4258c145e935a7667c04be4c7f84ec080da252038845f4a41
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bd55a8a148867357826308da2416071e8ad6d0fc541de932550d87fd7cabc897c0bdafce81f8e9d44b9d1eb1b1eec294956b666b21f470183bd330d77ad67c3a
|
7
|
+
data.tar.gz: a6c2c7fcf0c07cc66fd6d688d4a57079e0e3489801f05c62d24fcd445d7aac7e6293325e384a574a75cdd64dc9f8d3cc013087d120d84ca69e53d58749660aa3
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Sarah
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# AsyncPaperclipUploader
|
2
|
+
|
3
|
+
Save your attachments first to filesystem and then save async to your model
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'async_paperclip_uploader'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install async_paperclip_uploader
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
First, call AsyncPaperclipUploader::Temporary.new passing the model object, the related attachment attribute, and the uploaded file.
|
24
|
+
e.g.:
|
25
|
+
|
26
|
+
```AsyncPaperclipUploader::Temporary.new(@user, 'avatar', uploaded_file).call```
|
27
|
+
|
28
|
+
Next, create the sidekiq job class, which should follow the following name convention: object class name + UploadJob, e.g.:
|
29
|
+
|
30
|
+
``` UserUploadJob ```
|
31
|
+
|
32
|
+
In this class, you must receive the attributes object_id, attribute and filepath. e.g.:
|
33
|
+
|
34
|
+
```
|
35
|
+
class UserUploadJob
|
36
|
+
include Sidekiq::Worker
|
37
|
+
|
38
|
+
def perform(user_id, attribute, filepath)
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
```
|
45
|
+
|
46
|
+
Inside perform method, you will want to call AsyncPaperclipUploader::Permanent.new, which expects to receive the object class name, the object id, the attribute, and the filepath. e.g.:
|
47
|
+
|
48
|
+
|
49
|
+
```
|
50
|
+
def perform(user_id, attribute, filepath)
|
51
|
+
AsyncPaperclipUploader::Permanent.new('User', user_id, attribute, filepath).call
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
If you want a callback to be ran, the call method accepts a block. e.g.:
|
56
|
+
|
57
|
+
```
|
58
|
+
AsyncPaperclipUploader::Permanent.new('User', user_id, attribute, filepath).call do
|
59
|
+
# you callback here
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
|
64
|
+
## Development
|
65
|
+
|
66
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
67
|
+
|
68
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "async_paperclip_uploader/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "async_paperclip_uploader"
|
8
|
+
spec.version = AsyncPaperclipUploader::VERSION
|
9
|
+
spec.authors = ["Husky Dev Team"]
|
10
|
+
spec.email = ["dev@husky.io"]
|
11
|
+
|
12
|
+
spec.summary = 'Async paperclip uploader'
|
13
|
+
#spec.description = %q{TODO: Write a longer description or delete this line.}
|
14
|
+
#spec.homepage = "TODO: Put your gem's website or public repo URL here."
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
end
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
38
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "async_paperclip_uploader"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
module AsyncPaperclipUploader
|
3
|
+
class Permanent
|
4
|
+
def initialize(class_name, object_id, attribute, filepath)
|
5
|
+
@object = Object::const_get(class_name).find_by_id(object_id)
|
6
|
+
@attribute = attribute
|
7
|
+
@filepath = filepath
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
@object.send("#{@attribute}=", file)
|
12
|
+
if @object.save
|
13
|
+
if block_given?
|
14
|
+
yield
|
15
|
+
end
|
16
|
+
clean
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def file
|
22
|
+
File.open(@filepath)
|
23
|
+
end
|
24
|
+
|
25
|
+
def clean
|
26
|
+
File.delete(@filepath)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module AsyncPaperclipUploader
|
2
|
+
class Temporary
|
3
|
+
def initialize(object, attribute, uploaded_file)
|
4
|
+
@object = object
|
5
|
+
@attribute = attribute
|
6
|
+
@uploaded_file = uploaded_file
|
7
|
+
end
|
8
|
+
|
9
|
+
def call
|
10
|
+
if valid?
|
11
|
+
upload
|
12
|
+
run_permanent_upload_job
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid?
|
17
|
+
valid_class_attr? && valid_attachment?
|
18
|
+
end
|
19
|
+
|
20
|
+
def valid_class_attr?
|
21
|
+
@object.respond_to?(@attribute.to_sym)
|
22
|
+
end
|
23
|
+
|
24
|
+
def valid_attachment?
|
25
|
+
attachment = {
|
26
|
+
:filename => @uploaded_file[:filename],
|
27
|
+
:content_type => @uploaded_file[:type],
|
28
|
+
:headers => @uploaded_file[:head],
|
29
|
+
:tempfile => @uploaded_file[:tempfile]
|
30
|
+
}
|
31
|
+
@object.send("#{@attribute}=", ActionDispatch::Http::UploadedFile.new(attachment))
|
32
|
+
@object.valid?
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.base_path
|
36
|
+
ENV["FILE_UPLOAD_STAGING_DIR"]
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def upload
|
41
|
+
File.open(path, "wb") { |f| f.write(tempfile.read) }
|
42
|
+
end
|
43
|
+
|
44
|
+
def path
|
45
|
+
extname = File.extname(@uploaded_file[:filename])
|
46
|
+
name = "#{@attribute}-#{@object.id}#{extname}"
|
47
|
+
File.join(self.class.base_path, name)
|
48
|
+
end
|
49
|
+
|
50
|
+
def tempfile
|
51
|
+
@uploaded_file[:tempfile]
|
52
|
+
end
|
53
|
+
|
54
|
+
def run_permanent_upload_job
|
55
|
+
job_class.perform_async(@object.id, @attribute, path)
|
56
|
+
end
|
57
|
+
|
58
|
+
def job_class
|
59
|
+
name = "#{@object.class.name}#{base_job_class_name}"
|
60
|
+
Object::const_get(name)
|
61
|
+
end
|
62
|
+
|
63
|
+
def base_job_class_name
|
64
|
+
'UploadJob'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: async_paperclip_uploader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Husky Dev Team
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- dev@husky.io
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- async_paperclip_uploader.gemspec
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/async_paperclip_uploader.rb
|
73
|
+
- lib/async_paperclip_uploader/permanent.rb
|
74
|
+
- lib/async_paperclip_uploader/temporary.rb
|
75
|
+
- lib/async_paperclip_uploader/version.rb
|
76
|
+
homepage:
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata:
|
80
|
+
allowed_push_host: https://rubygems.org
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.7.6
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Async paperclip uploader
|
101
|
+
test_files: []
|