s3_file_cache_download 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/MIT-LICENSE +20 -0
- data/README.md +63 -0
- data/Rakefile +7 -0
- data/app/models/application_record.rb +3 -0
- data/app/models/s3_file_cache.rb +86 -0
- data/db/migrate/20170404044432_create_s3_file_caches.rb +10 -0
- data/lib/s3_file_cache_download/engine.rb +4 -0
- data/lib/s3_file_cache_download/version.rb +3 -0
- data/lib/s3_file_cache_download.rb +42 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c1622cbece94d617f6c1aae6f4a7c398949536a5
|
4
|
+
data.tar.gz: f0f67fcc8d9a09e061d2bbb279116611368122a5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5bb0399303c6b2187f04e230ec34b47af56a6c6a0fa5f8a96dd0c16bc0d77046581c6e0fe8cfd178a0f51af54f8df224a592377fad7050bc8c9a275209e80de8
|
7
|
+
data.tar.gz: bcdcf67d96d9bacc3ecf08432e37186d0394d25f89683a2cdf2302d0ef73af01c923795cc53ae0bbd94297dbfd83e9cc079e9df32b042c0ee50631a1de2b5d88
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 wat-aro
|
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,63 @@
|
|
1
|
+
# S3FileCacheDownload
|
2
|
+
|
3
|
+
PROVIDE helper method that S3 file download use temporaly file
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
First, execute `bin/rails s3_file_cache_download_engine:install:migrations`.
|
8
|
+
|
9
|
+
Second, include `S3FileCacheDownload::Helper` module to your controller.
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
class YourController
|
13
|
+
include S3FileCacheDownload::Helper
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
17
|
+
Call `send_s3_file` method.
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class YourController
|
21
|
+
include S3FileCacheDownload
|
22
|
+
|
23
|
+
def show
|
24
|
+
send_s3_file :your_bucket_name, :your_file_key
|
25
|
+
end
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
If you want to use option, you can pass option.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class YourController
|
33
|
+
include S3FileCacheDownload
|
34
|
+
|
35
|
+
def show
|
36
|
+
send_s3_file :your_bucket_name, :your_file_key, disposition: 'inline'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
|
42
|
+
## Installation
|
43
|
+
Add this line to your application's Gemfile:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
gem 's3_file_cache_download'
|
47
|
+
```
|
48
|
+
|
49
|
+
And then execute:
|
50
|
+
```bash
|
51
|
+
$ bundle
|
52
|
+
```
|
53
|
+
|
54
|
+
Or install it yourself as:
|
55
|
+
```bash
|
56
|
+
$ gem install s3_file_cache_download
|
57
|
+
```
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
Contribution directions go here.
|
61
|
+
|
62
|
+
## License
|
63
|
+
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,86 @@
|
|
1
|
+
class S3FileCache < ApplicationRecord
|
2
|
+
class << self
|
3
|
+
def find_s3_cache_file_or_create!(s3_file_path, bucket_name)
|
4
|
+
S3FileCache.transaction do
|
5
|
+
s3_file_cache = S3FileCache.find_or_create_by!(s3_full_path: s3_file_path, bucket_name: bucket_name)
|
6
|
+
|
7
|
+
if s3_file_cache.expire?
|
8
|
+
FileUtils.rm(s3_file_cache.place)
|
9
|
+
s3_file_cache.destroy
|
10
|
+
|
11
|
+
s3_file_cache = S3FileCache.create!(s3_full_path: s3_file_path, bucket_name: bucket_name)
|
12
|
+
end
|
13
|
+
|
14
|
+
s3_file_cache.fetch!
|
15
|
+
s3_file_cache
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def cleaning_directory_and_record!
|
20
|
+
limit = Time.zone.now - expire_seconds
|
21
|
+
|
22
|
+
expired_files = where('created_at < ?', limit)
|
23
|
+
expired_files.each do |expired_file|
|
24
|
+
FileUtils.rm(expired_file.place)
|
25
|
+
expired_file.destroy
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def expire_seconds
|
30
|
+
S3FileCacheDownload.expire_seconds.seconds
|
31
|
+
end
|
32
|
+
|
33
|
+
def file_cache_directory
|
34
|
+
S3FileCacheDownload.file_cache_directory
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def expire?
|
39
|
+
created_at + S3FileCacheDownload.expire_seconds < Time.zone.now
|
40
|
+
end
|
41
|
+
|
42
|
+
def fetch!
|
43
|
+
return if File.exist?(place)
|
44
|
+
|
45
|
+
s3_object = S3FileCache::S3Object.new(bucket_name, s3_full_path)
|
46
|
+
File.open(place, 'w') do |file|
|
47
|
+
s3_object.get.each_line do |line|
|
48
|
+
file.write line
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def place
|
54
|
+
"#{S3FileCache.file_cache_directory}/#{id}"
|
55
|
+
end
|
56
|
+
|
57
|
+
def filename
|
58
|
+
File.basename(s3_full_path)
|
59
|
+
end
|
60
|
+
|
61
|
+
class S3Object
|
62
|
+
def initialize(bucket_name, path)
|
63
|
+
@bucket = Aws::S3::Resource.new(client: client).bucket(bucket_name)
|
64
|
+
@path = path
|
65
|
+
end
|
66
|
+
|
67
|
+
def get
|
68
|
+
body = @bucket.object(@path).get.body
|
69
|
+
|
70
|
+
if block_given?
|
71
|
+
yield body
|
72
|
+
else
|
73
|
+
body
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def client
|
80
|
+
@client ||= Aws::S3::Client.new(
|
81
|
+
access_key_id: S3FileCacheDownload.aws_access_key_id,
|
82
|
+
secret_access_key: S3FileCacheDownload.aws_secret_access_key
|
83
|
+
)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 's3_file_cache_download/engine'
|
2
|
+
require 'action_controller'
|
3
|
+
require 'active_support/configurable'
|
4
|
+
require 'aws-sdk'
|
5
|
+
|
6
|
+
module S3FileCacheDownload
|
7
|
+
class FileCacheDirectoryNotFound < StandardError; end
|
8
|
+
|
9
|
+
include ActiveSupport::Configurable
|
10
|
+
|
11
|
+
config_accessor :aws_access_key_id, instance_reader: false, instance_writer: false do
|
12
|
+
ENV['AWS_ACCESS_KEY']
|
13
|
+
end
|
14
|
+
|
15
|
+
config_accessor :aws_secret_access_key, instance_reader: false, instance_writer: false do
|
16
|
+
ENV['AWS_SECRET_ACCESS_KEY']
|
17
|
+
end
|
18
|
+
|
19
|
+
config_accessor :file_cache_directory, instance_reader: false, instance_writer: false do
|
20
|
+
ENV['FILE_CACHE_DIRECTORY']
|
21
|
+
end
|
22
|
+
|
23
|
+
config_accessor :expire_seconds, instance_reader: false, instance_writer: false do
|
24
|
+
ENV['EXPIRE_SECONDS']
|
25
|
+
end
|
26
|
+
|
27
|
+
class << self
|
28
|
+
def configure
|
29
|
+
yield config
|
30
|
+
|
31
|
+
raise FileCacheDirectoryNotFound unless Dir.exist?(config.file_cache_directory)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
module Helper
|
36
|
+
def send_s3_file(bucket_name, path, option = {})
|
37
|
+
s3_file_cache = S3FileCache.find_s3_cache_file_or_create!(path, bucket_name)
|
38
|
+
|
39
|
+
send_file s3_file_cache.place, { filename: s3_file_cache.filename }.merge(option)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: s3_file_cache_download
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- wat-aro
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-04 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.0
|
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.0
|
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: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: provide helper method that S3 file download use temporaly file
|
84
|
+
email:
|
85
|
+
- kazutas1008@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- MIT-LICENSE
|
91
|
+
- README.md
|
92
|
+
- Rakefile
|
93
|
+
- app/models/application_record.rb
|
94
|
+
- app/models/s3_file_cache.rb
|
95
|
+
- db/migrate/20170404044432_create_s3_file_caches.rb
|
96
|
+
- lib/s3_file_cache_download.rb
|
97
|
+
- lib/s3_file_cache_download/engine.rb
|
98
|
+
- lib/s3_file_cache_download/version.rb
|
99
|
+
homepage: https://github.com/esminc/s3_file_cache_download
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata: {}
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.6.8
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: provide helper method that S3 file download use temporaly file
|
123
|
+
test_files: []
|