pull_tempfile 0.1.0
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +165 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/pull_tempfile/version.rb +3 -0
- data/lib/pull_tempfile.rb +39 -0
- data/pull_tempfile.gemspec +28 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: caf9eb1fd40ac992ddd93ac1e53514c560d2c4c1
|
4
|
+
data.tar.gz: 6a288c61ead7821e62d2cf4b89aa57d0039e9e7c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8618caa4f7c3cc2689d5ab7d36e0649ff2913e2a79089ec8edc42f86fca8bded6e6fa8935703bd0e87d7f2604525495de1c55a3ab558bf0c52b2af858d4eb51d
|
7
|
+
data.tar.gz: c26e94ac0e8df02659ddb1ed61c872e2422e3f416b7f7c9a5ecd104939e5aa3a31fc46c00e744f0da870db79034b0db3a43217e56beb62001a5c5cb5f0ee9232
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at equivalent@eq8.eu. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Tomas Valent
|
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,165 @@
|
|
1
|
+
# PullTempfile
|
2
|
+
|
3
|
+
[](https://travis-ci.org/equivalent/pull_tempfile)
|
4
|
+
[](https://codeclimate.com/github/equivalent/pull_tempfile)
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'pull_tempfile'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install pull_tempfile
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```
|
25
|
+
class Medium < ActiveRecord::Base
|
26
|
+
has_attached_file :file
|
27
|
+
|
28
|
+
# ...
|
29
|
+
end
|
30
|
+
|
31
|
+
require 'pull_tempfile'
|
32
|
+
|
33
|
+
# when used as "AWS S3 browser upload" you can fetch original filename as "${filename}" metadata
|
34
|
+
# or use some different way to determine the file name.
|
35
|
+
original_filename = 'my face.jpg'
|
36
|
+
|
37
|
+
medium = Media.new
|
38
|
+
|
39
|
+
PullTempfile.transaction(url: url, original_filename: original_filename) do |tmp_file|
|
40
|
+
medium.file = tmp_file
|
41
|
+
medium.save!
|
42
|
+
end
|
43
|
+
|
44
|
+
```
|
45
|
+
|
46
|
+
Transaction will automatically delete (`unlink`) the temporary file after block
|
47
|
+
finish. If you want to keep the tmp file for longer you can use:
|
48
|
+
|
49
|
+
```
|
50
|
+
file = PullTempfile.pull_tempfile(url: url, original_filename: original_filename)
|
51
|
+
# ...
|
52
|
+
file.unlink # delete file
|
53
|
+
```
|
54
|
+
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
59
|
+
|
60
|
+
|
61
|
+
## Reason why this Gem exist:
|
62
|
+
|
63
|
+
When you using gem like [paperclip](https://github.com/thoughtbot/paperclip) `> 3.1.4` you can do something like this to pull file from url:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class User < ActiveRecord::Base
|
67
|
+
attr_reader :avatar_remote_url
|
68
|
+
has_attached_file :avatar
|
69
|
+
|
70
|
+
def avatar_remote_url=(url_value)
|
71
|
+
self.avatar = URI.parse(url_value)
|
72
|
+
# Assuming url_value is http://example.com/photos/face.png
|
73
|
+
# avatar_file_name == "face.png"
|
74
|
+
# avatar_content_type == "image/png"
|
75
|
+
@avatar_remote_url = url_value
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
user = User.new
|
80
|
+
user.avatar_remote_url = 'http://example.com/photos/face.png'
|
81
|
+
user.save!
|
82
|
+
```
|
83
|
+
|
84
|
+
And this is how you upload file from url.
|
85
|
+
|
86
|
+
> source
|
87
|
+
> https://github.com/thoughtbot/paperclip/wiki/Attachment-downloaded-from-a-URL
|
88
|
+
|
89
|
+
The problem is that if you do this on AWS S3 signd url is sending binary mime type
|
90
|
+
`binary/octet-stream` and therefore
|
91
|
+
if you are implementing validations on your uploads:
|
92
|
+
|
93
|
+
```
|
94
|
+
class User < ActiveRecord::Base
|
95
|
+
# ...
|
96
|
+
validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
|
97
|
+
# ...
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
...you'll get error:
|
102
|
+
|
103
|
+
```
|
104
|
+
Validation failed: File has contents that are not what they are
|
105
|
+
reported to be, File is invalid, File content type is invalid
|
106
|
+
```
|
107
|
+
|
108
|
+
One solution is to skip validations in this context:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
#...
|
112
|
+
medium.save!(validate: false) # will skip validations
|
113
|
+
```
|
114
|
+
|
115
|
+
...but that's not secure.
|
116
|
+
|
117
|
+
> Yes you can use context validations, or implement validations differently
|
118
|
+
> http://www.eq8.eu/blogs/22-different-ways-how-to-do-rails-validations
|
119
|
+
> but that's not the point,
|
120
|
+
|
121
|
+
So way how to get around this is to download the file to your `/tmp/` folder as a temp
|
122
|
+
file, and upload it via Paperclip
|
123
|
+
|
124
|
+
Install gem `httparty`
|
125
|
+
|
126
|
+
```
|
127
|
+
# Gemfile
|
128
|
+
# ...
|
129
|
+
gem 'httparty'
|
130
|
+
# ...
|
131
|
+
```
|
132
|
+
|
133
|
+
Run `bundle install`
|
134
|
+
|
135
|
+
|
136
|
+
Next create new file `./lib/s3_helper.rb`
|
137
|
+
|
138
|
+
```
|
139
|
+
require 'httparty' # gem
|
140
|
+
module S3Helper
|
141
|
+
def pull_asset(url:, destination:)
|
142
|
+
File.open(destination, "wb") do |f|
|
143
|
+
f.binmode
|
144
|
+
f.write HTTParty.get(url).parsed_response
|
145
|
+
f.close
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
```
|
150
|
+
|
151
|
+
And now you can do something like:
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
user = User.new
|
155
|
+
require Rails.root.join('lib', 's3_helper')
|
156
|
+
|
157
|
+
S3Helper.pull_asset(url: "http://s3....../bbuesubeueueue", destination: "/tmp/my-file.jpg")
|
158
|
+
|
159
|
+
user.avatar = File.open("/tmp/my-file.jpg")
|
160
|
+
user.save!
|
161
|
+
```
|
162
|
+
|
163
|
+
But the problem is that you need to delete this file manually. So why
|
164
|
+
not to take advantage of Ruby `Tempfile`. This is exactly what this gem
|
165
|
+
is doing. Have a look on the Source code https://github.com/equivalent/pull_tempfile/blob/master/lib/pull_tempfile.rb
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "pull_tempfile"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'httparty' # gem
|
2
|
+
require 'pathname' # standard Ruby lib
|
3
|
+
require 'tempfile' # standard Ruby lib
|
4
|
+
|
5
|
+
require "pull_tempfile/version"
|
6
|
+
|
7
|
+
module PullTempfile
|
8
|
+
# Creates Temporary file
|
9
|
+
#
|
10
|
+
# file = PullTempfile.pull_tempfile(original_filename: "image asset.jpg", url: 'http://..../uaoeuoeueoauoueao' )
|
11
|
+
# # ...do stuff
|
12
|
+
# file.unlink
|
13
|
+
#
|
14
|
+
# Temporary files have suffix so your file "image asset.jpg"
|
15
|
+
# will be saved as "/tmp/image asset20160317-18066-gw1q0.jpg". This is because
|
16
|
+
# Tempfile lib is ensuring you won't override files
|
17
|
+
#
|
18
|
+
# Remmember to `file.unlink` or PullTempfile.transaction that does that automatically
|
19
|
+
#
|
20
|
+
def self.pull_tempfile(original_filename:, url:)
|
21
|
+
_generated_name = Pathname.new(original_filename)
|
22
|
+
extension = _generated_name.extname.to_s
|
23
|
+
tmp_file_name = _generated_name.basename(extension).to_s
|
24
|
+
|
25
|
+
file = Tempfile.new([tmp_file_name, extension])
|
26
|
+
file.binmode
|
27
|
+
file.write(HTTParty.get(url).parsed_response)
|
28
|
+
file.close
|
29
|
+
file
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.transaction(original_filename:, url:)
|
33
|
+
file = pull_tempfile(original_filename: original_filename, url: url)
|
34
|
+
|
35
|
+
yield file.open
|
36
|
+
ensure
|
37
|
+
file && file.unlink #delete temp file
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pull_tempfile/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "pull_tempfile"
|
8
|
+
spec.version = PullTempfile::VERSION
|
9
|
+
spec.authors = ["Tomas Valent"]
|
10
|
+
spec.email = ["equivalent@eq8.eu"]
|
11
|
+
|
12
|
+
spec.summary = %q{Pull URL to temporary file}
|
13
|
+
spec.description = %q{Lightway way to pull file from URL to tmp folder as a standard Ruby Tempfile}
|
14
|
+
spec.homepage = "https://github.com/equivalent/pull_tempfile"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "httparty", "~> 0.13"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.4"
|
26
|
+
spec.add_development_dependency "vcr", "~> 3.0"
|
27
|
+
spec.add_development_dependency "webmock", "~> 1.24"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pull_tempfile
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tomas Valent
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.13'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.4'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: vcr
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.24'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.24'
|
97
|
+
description: Lightway way to pull file from URL to tmp folder as a standard Ruby Tempfile
|
98
|
+
email:
|
99
|
+
- equivalent@eq8.eu
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- CODE_OF_CONDUCT.md
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- bin/console
|
113
|
+
- bin/setup
|
114
|
+
- lib/pull_tempfile.rb
|
115
|
+
- lib/pull_tempfile/version.rb
|
116
|
+
- pull_tempfile.gemspec
|
117
|
+
homepage: https://github.com/equivalent/pull_tempfile
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.4.8
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Pull URL to temporary file
|
141
|
+
test_files: []
|