refile_cache 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 +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +42 -0
- data/Rakefile +2 -0
- data/lib/refile_cache/cache.rb +91 -0
- data/lib/refile_cache/cache_hasher.rb +7 -0
- data/lib/refile_cache/file_double.rb +13 -0
- data/lib/refile_cache/rails.rb +11 -0
- data/lib/refile_cache/version.rb +3 -0
- data/lib/refile_cache.rb +6 -0
- data/refile_cache.gemspec +24 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 57983cb55d17da798519ad854943fa6a458b9478
|
4
|
+
data.tar.gz: d63d01e0e75ad5cb412ad6a3d8156b62908091ee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e7b2cf4996ec2c4132d5f081e22e2a472206fb300929d14501c719a2cb561db2f34d75b536fd855a32c96e9cfa82700620d515bfc65480cec0e6fb179d82481f
|
7
|
+
data.tar.gz: 0cef1fc4c28d776191c7f026ccda4c2bd6cfcdf6ecad021ab09391560578b225ee9f1d585447021317711b15fb310a84fcd604a5cfe58e17254134aa483447b5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Ceda
|
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,42 @@
|
|
1
|
+
# RefileCache
|
2
|
+
|
3
|
+
RefileCache - S3 file caching for Refile
|
4
|
+
|
5
|
+
Features:
|
6
|
+
|
7
|
+
- Caching images on S3
|
8
|
+
|
9
|
+
## Quick start, Rails
|
10
|
+
|
11
|
+
Add the gem:
|
12
|
+
|
13
|
+
``` ruby
|
14
|
+
gem "refile"
|
15
|
+
gem 'refile-s3', require: 'refile/s3'
|
16
|
+
gem "refile_cache"
|
17
|
+
```
|
18
|
+
|
19
|
+
Now you can upload files to S3 easily by using these accessors:
|
20
|
+
|
21
|
+
``` ruby
|
22
|
+
# config/initializers/refile.rb
|
23
|
+
|
24
|
+
aws = {
|
25
|
+
access_key_id: "xyz",
|
26
|
+
secret_access_key: "abc",
|
27
|
+
region: "sa-east-1",
|
28
|
+
bucket: "my-bucket",
|
29
|
+
}
|
30
|
+
Refile.cache = Refile::S3.new(prefix: "cache", **aws)
|
31
|
+
Refile.store = Refile::S3.new(prefix: "store", **aws)
|
32
|
+
|
33
|
+
#Setup Refile Cache
|
34
|
+
Refile.backends['image_cache'] = Refile::S3.new(prefix: 'image_cache', hasher: RefileCache::CacheHasher.new, **aws)
|
35
|
+
|
36
|
+
Refile.cdn_host = "https://your-dist-url.cloudfront.net"
|
37
|
+
```
|
38
|
+
|
39
|
+
|
40
|
+
## License
|
41
|
+
|
42
|
+
[MIT](LICENSE.txt)
|
data/Rakefile
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
module RefileCache
|
2
|
+
|
3
|
+
class Cache
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
"#{self.class}::Logic".constantize.new(@app).call(env)
|
10
|
+
end
|
11
|
+
|
12
|
+
# This is for autoreloading to be working :D
|
13
|
+
class Logic
|
14
|
+
URL_PATH = /\A#{Refile.mount_point}\/.*\/store\/(fill|fit|limit|pad|convert)\//
|
15
|
+
URL_PROCESSOR = /\A#{Refile.mount_point}\/(?<token>.*)\/(?<backend>.*)\/(?<processor>(fill|fit|limit|pad|convert))\/(?<splat>.*)\/(?<id>.*)\/(?<file_base>.*)\.(?<extension>.*)/
|
16
|
+
|
17
|
+
def initialize(app)
|
18
|
+
@app = app
|
19
|
+
end
|
20
|
+
|
21
|
+
def call(env)
|
22
|
+
# do not process other endpoints
|
23
|
+
return @app.call(env) if env['PATH_INFO'] !~ URL_PATH
|
24
|
+
# do not process invalid token
|
25
|
+
return @app.call(env) unless valid_token?(env)
|
26
|
+
|
27
|
+
params = get_params(env)
|
28
|
+
cache_key = "cache#{params[:id]}#{params[:processor]}#{params[:splat].delete('/')}"
|
29
|
+
|
30
|
+
if backend.exists?(cache_key)
|
31
|
+
file = backend.get(cache_key)
|
32
|
+
filename = "#{params[:file_base]}.#{params[:extension]}"
|
33
|
+
return [200, own_headers(filename, file.size), stream_file(env, file)]
|
34
|
+
end
|
35
|
+
|
36
|
+
status, headers, response = @app.call(env)
|
37
|
+
|
38
|
+
# cache only existing images
|
39
|
+
if status == 200
|
40
|
+
image = RefileCache::FileDouble.new(File.open(response.path).read, cache_key, content_type: headers['Content-Type'])
|
41
|
+
backend.upload(image)
|
42
|
+
end
|
43
|
+
|
44
|
+
[status, headers, response]
|
45
|
+
end
|
46
|
+
|
47
|
+
def valid_token?(env)
|
48
|
+
token = get_params(env)[:token]
|
49
|
+
base_path = env['PATH_INFO'].gsub(::File.join(Refile.mount_point, token), '')
|
50
|
+
|
51
|
+
Refile.valid_token?(base_path, token)
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_params(env)
|
55
|
+
env['PATH_INFO'].match(URL_PROCESSOR)
|
56
|
+
end
|
57
|
+
|
58
|
+
def backend
|
59
|
+
Refile.backends.fetch('image_cache') do |name|
|
60
|
+
log_error("Could not find backend: #{name}")
|
61
|
+
halt 404
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def own_headers(filename, content_length)
|
66
|
+
{
|
67
|
+
'Content-Type' => 'image/jpeg',
|
68
|
+
'Access-Control-Allow-Origin' => '*',
|
69
|
+
'Access-Control-Allow-Headers' => '',
|
70
|
+
'Access-Control-Allow-Method' => '',
|
71
|
+
'Cache-Control' => 'public, max-age=31536000',
|
72
|
+
'Expires' => (Time.now + 1.year).to_s,
|
73
|
+
'Content-Disposition' => "inline; filename=\"#{filename}\"",
|
74
|
+
'Last-Modified' => (Time.now - 1.month).to_s,
|
75
|
+
'Content-Length' => content_length.to_s,
|
76
|
+
'X-Content-Type-Options' => 'nosniff'
|
77
|
+
}
|
78
|
+
end
|
79
|
+
|
80
|
+
def stream_file(env, file)
|
81
|
+
if file.respond_to?(:path)
|
82
|
+
path = file.path
|
83
|
+
else
|
84
|
+
path = Dir::Tmpname.create(get_params(env)[:id]) {}
|
85
|
+
IO.copy_stream file, path
|
86
|
+
end
|
87
|
+
File.open(path)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module RefileCache
|
2
|
+
class FileDouble
|
3
|
+
attr_reader :original_filename, :content_type
|
4
|
+
def initialize(data, name = nil, content_type: nil)
|
5
|
+
@io = StringIO.new(data)
|
6
|
+
@original_filename = name
|
7
|
+
@content_type = content_type
|
8
|
+
end
|
9
|
+
|
10
|
+
extend Forwardable
|
11
|
+
def_delegators :@io, :read, :rewind, :size, :eof?, :close
|
12
|
+
end
|
13
|
+
end
|
data/lib/refile_cache.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'refile_cache/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'refile_cache'
|
8
|
+
spec.version = RefileCache::VERSION
|
9
|
+
spec.authors = ['Blueberry']
|
10
|
+
spec.email = ['apleskac@blueberry.cz']
|
11
|
+
|
12
|
+
spec.summary = 'RefileCache'
|
13
|
+
spec.description = 'RefileCache - S3 file caching for Refile'
|
14
|
+
spec.homepage = 'http://github.com/blueberry/refile_cache'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
|
19
|
+
spec.add_dependency 'refile', '~> 0.6.2'
|
20
|
+
spec.add_dependency 'refile-s3', '~> 0.2.0'
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: refile_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Blueberry
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: refile
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.6.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.6.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: refile-s3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.2.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.2.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.12'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.12'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: RefileCache - S3 file caching for Refile
|
70
|
+
email:
|
71
|
+
- apleskac@blueberry.cz
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/refile_cache.rb
|
82
|
+
- lib/refile_cache/cache.rb
|
83
|
+
- lib/refile_cache/cache_hasher.rb
|
84
|
+
- lib/refile_cache/file_double.rb
|
85
|
+
- lib/refile_cache/rails.rb
|
86
|
+
- lib/refile_cache/version.rb
|
87
|
+
- refile_cache.gemspec
|
88
|
+
homepage: http://github.com/blueberry/refile_cache
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.4.5
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: RefileCache
|
112
|
+
test_files: []
|
113
|
+
has_rdoc:
|