shrine-fog 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9190a56cee70da72f752a7cdb9f0f499b48467bf
4
+ data.tar.gz: 44f829d6eb84aae145249df2fbaefb747f2df8b3
5
+ SHA512:
6
+ metadata.gz: 3cff6c69c740f5051afc4e723feb600fb17a7c2f1a69390bcb7ac78d4586797591f5b99dcc06c51018f91bf8e67944b5b635d00f45a40aba0a8e36d29d5b911b
7
+ data.tar.gz: 41d45e6eb1b5c3a0e7d69ee8696719a89c8316bb4176c06acebb7d086c75cc841f2ac00fe4030fe5141c09050ccc819e87e82f9317e91a558619ba49a5d0e157
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Janko Marohnić
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,58 @@
1
+ # Shrine::Fog
2
+
3
+ Provides [Fog] storage for [Shrine].
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem "shrine-fog"
9
+ gem "fog-xyz # Fog gem for the storage you want to use
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ Require the appropriate Fog gem, and assign the parameters for initializing
15
+ the storage:
16
+
17
+ ```rb
18
+ require "fog/google"
19
+
20
+ Shrine.storages[:store] = Shrine::Storage::Fog.new(
21
+ provider: "Google", #
22
+ google_storage_access_key_id: "ACCESS_KEY_ID", # Fog credentials
23
+ google_storage_secret_access_key: "SECRET_ACCESS_KEY", #
24
+ directory: "uploads",
25
+ )
26
+ ```
27
+
28
+ You can also assign a Fog storage object as the `:connection`:
29
+
30
+ ```rb
31
+ require "fog/google"
32
+
33
+ google = Fog::Storage.new(
34
+ provider: "Google",
35
+ google_storage_access_key_id: "ACCESS_KEY_ID",
36
+ google_storage_secret_access_key: "SECRET_ACCESS_KEY",
37
+ )
38
+
39
+ Shrine.storages[:store] = Shrine::Storage::Fog.new(
40
+ connection: google,
41
+ directory: "uploads",
42
+ )
43
+ ```
44
+
45
+ ## S3 or Filesystem
46
+
47
+ If you want to store your files to Amazon S3 or the filesystem, you should use
48
+ the storages that ship with Shrine (instead of [fog-aws] or [fog-local]) as
49
+ they are much more advanced.
50
+
51
+ ## License
52
+
53
+ [MIT](http://opensource.org/licenses/MIT).
54
+
55
+ [Fog]: http://fog.io/
56
+ [Shrine]: https://github.com/janko-m/shrine
57
+ [fog-aws]: https://github.com/fog/fog-aws
58
+ [fog-local]: https://github.com/fog/fog-local
@@ -0,0 +1,95 @@
1
+ require "down"
2
+
3
+ class Shrine
4
+ module Storage
5
+ class Fog
6
+ attr_reader :connection, :directory, :prefix
7
+
8
+ def initialize(directory:, prefix: nil, public: true, connection: nil, **options)
9
+ @connection = connection || ::Fog::Storage.new(options)
10
+ @directory = @connection.directories.new(key: directory)
11
+ @prefix = prefix
12
+ @public = public
13
+ end
14
+
15
+ def upload(io, id, metadata = {})
16
+ if copyable?(io)
17
+ copy(io, id, metadata)
18
+ else
19
+ put(io, id, metadata)
20
+ end
21
+ end
22
+
23
+ def download(id)
24
+ Down.download(url(id))
25
+ end
26
+
27
+ def open(id)
28
+ download(id)
29
+ end
30
+
31
+ def read(id)
32
+ get(id).body
33
+ end
34
+
35
+ def exists?(id)
36
+ !!head(id)
37
+ end
38
+
39
+ def delete(id)
40
+ head(id).destroy
41
+ end
42
+
43
+ def url(id, **options)
44
+ head(id).public_url
45
+ end
46
+
47
+ def clear!(confirm = nil)
48
+ raise Shrine::Confirm unless confirm == :confirm
49
+ list.each(&:destroy)
50
+ end
51
+
52
+ protected
53
+
54
+ def get(id)
55
+ directory.files.get(path(id))
56
+ end
57
+
58
+ def head(id)
59
+ directory.files.head(path(id))
60
+ end
61
+
62
+ def provider
63
+ connection.class
64
+ end
65
+
66
+ private
67
+
68
+ def list
69
+ directory.files.select { |file| file.key.start_with?(prefix.to_s) }
70
+ end
71
+
72
+ def path(id)
73
+ [*prefix, id].join("/")
74
+ end
75
+
76
+ def put(io, id, metadata = {})
77
+ options = {key: path(id), body: io, public: @public}
78
+ options[:content_type] = metadata["mime_type"]
79
+
80
+ directory.files.create(options)
81
+ io.rewind
82
+ end
83
+
84
+ def copy(io, id, metadata = {})
85
+ io.storage.head(io.id).copy(directory.key, path(id))
86
+ end
87
+
88
+ def copyable?(io)
89
+ io.respond_to?(:storage) &&
90
+ io.storage.is_a?(Storage::Fog) &&
91
+ io.storage.provider == provider
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = "shrine-fog"
3
+ gem.version = "0.1.0"
4
+
5
+ gem.required_ruby_version = ">= 2.1"
6
+
7
+ gem.summary = "Provides Fog storage for Shrine."
8
+ gem.description = "Provides Fog storage for Shrine."
9
+ gem.homepage = "https://github.com/janko-m/shrine-fog"
10
+ gem.authors = ["Janko Marohnić"]
11
+ gem.email = ["janko.marohnic@gmail.com"]
12
+ gem.license = "MIT"
13
+
14
+ gem.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "shrine-fog.gemspec"]
15
+ gem.require_path = "lib"
16
+
17
+ gem.add_development_dependency "down", ">= 1.0.3"
18
+
19
+ gem.add_development_dependency "fog-aws"
20
+ gem.add_development_dependency "mime-types"
21
+ gem.add_development_dependency "dotenv"
22
+ gem.add_development_dependency "shrine"
23
+ gem.add_development_dependency "minitest", "~> 5.8"
24
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shrine-fog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Janko Marohnić
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: down
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: fog-aws
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: mime-types
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: dotenv
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: shrine
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
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.8'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.8'
97
+ description: Provides Fog storage for Shrine.
98
+ email:
99
+ - janko.marohnic@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - LICENSE.txt
105
+ - README.md
106
+ - lib/shrine/storage/fog.rb
107
+ - shrine-fog.gemspec
108
+ homepage: https://github.com/janko-m/shrine-fog
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '2.1'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.5
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Provides Fog storage for Shrine.
132
+ test_files: []
133
+ has_rdoc: