From e6a8205dfc4cf3454c8d6d67da8b2d80b1f2a3b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Sat, 5 Dec 2015 19:14:27 +0100 Subject: [PATCH] Initial commit --- .gitignore | 3 ++ CODE_OF_CONDUCT.md | 13 ++++++ Gemfile | 5 +++ LICENSE.txt | 21 +++++++++ README.md | 58 ++++++++++++++++++++++++ Rakefile | 1 + lib/shrine/storage/fog.rb | 95 +++++++++++++++++++++++++++++++++++++++ shrine-fog.gemspec | 24 ++++++++++ test/storage_test.rb | 46 +++++++++++++++++++ test/test_helper.rb | 26 +++++++++++ 10 files changed, 292 insertions(+) create mode 100644 .gitignore create mode 100644 CODE_OF_CONDUCT.md create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 lib/shrine/storage/fog.rb create mode 100644 shrine-fog.gemspec create mode 100644 test/storage_test.rb create mode 100644 test/test_helper.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0cfea32 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +Gemfile.lock +pkg/ +.env diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..ce9bee7 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,13 @@ +# Contributor Code of Conduct + +As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. + +We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. + +Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. + +This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..cff616c --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +source 'https://rubygems.org' + +gemspec + +gem 'pry' diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..6514e66 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Janko Marohnić + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..949458a --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +# Shrine::Fog + +Provides [Fog] storage for [Shrine]. + +## Installation + +```ruby +gem "shrine-fog" +gem "fog-xyz # Fog gem for the storage you want to use +``` + +## Usage + +Require the appropriate Fog gem, and assign the parameters for initializing +the storage: + +```rb +require "fog/google" + +Shrine.storages[:store] = Shrine::Storage::Fog.new( + provider: "Google", # + google_storage_access_key_id: "ACCESS_KEY_ID", # Fog credentials + google_storage_secret_access_key: "SECRET_ACCESS_KEY", # + directory: "uploads", +) +``` + +You can also assign a Fog storage object as the `:connection`: + +```rb +require "fog/google" + +google = Fog::Storage.new( + provider: "Google", + google_storage_access_key_id: "ACCESS_KEY_ID", + google_storage_secret_access_key: "SECRET_ACCESS_KEY", +) + +Shrine.storages[:store] = Shrine::Storage::Fog.new( + connection: google, + directory: "uploads", +) +``` + +## S3 or Filesystem + +If you want to store your files to Amazon S3 or the filesystem, you should use +the storages that ship with Shrine (instead of [fog-aws] or [fog-local]) as +they are much more advanced. + +## License + +[MIT](http://opensource.org/licenses/MIT). + +[Fog]: http://fog.io/ +[Shrine]: https://github.com/janko-m/shrine +[fog-aws]: https://github.com/fog/fog-aws +[fog-local]: https://github.com/fog/fog-local diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..2995527 --- /dev/null +++ b/Rakefile @@ -0,0 +1 @@ +require "bundler/gem_tasks" diff --git a/lib/shrine/storage/fog.rb b/lib/shrine/storage/fog.rb new file mode 100644 index 0000000..53dd6ab --- /dev/null +++ b/lib/shrine/storage/fog.rb @@ -0,0 +1,95 @@ +require "down" + +class Shrine + module Storage + class Fog + attr_reader :connection, :directory, :prefix + + def initialize(directory:, prefix: nil, public: true, connection: nil, **options) + @connection = connection || ::Fog::Storage.new(options) + @directory = @connection.directories.new(key: directory) + @prefix = prefix + @public = public + end + + def upload(io, id, metadata = {}) + if copyable?(io) + copy(io, id, metadata) + else + put(io, id, metadata) + end + end + + def download(id) + Down.download(url(id)) + end + + def open(id) + download(id) + end + + def read(id) + get(id).body + end + + def exists?(id) + !!head(id) + end + + def delete(id) + head(id).destroy + end + + def url(id, **options) + head(id).public_url + end + + def clear!(confirm = nil) + raise Shrine::Confirm unless confirm == :confirm + list.each(&:destroy) + end + + protected + + def get(id) + directory.files.get(path(id)) + end + + def head(id) + directory.files.head(path(id)) + end + + def provider + connection.class + end + + private + + def list + directory.files.select { |file| file.key.start_with?(prefix.to_s) } + end + + def path(id) + [*prefix, id].join("/") + end + + def put(io, id, metadata = {}) + options = {key: path(id), body: io, public: @public} + options[:content_type] = metadata["mime_type"] + + directory.files.create(options) + io.rewind + end + + def copy(io, id, metadata = {}) + io.storage.head(io.id).copy(directory.key, path(id)) + end + + def copyable?(io) + io.respond_to?(:storage) && + io.storage.is_a?(Storage::Fog) && + io.storage.provider == provider + end + end + end +end diff --git a/shrine-fog.gemspec b/shrine-fog.gemspec new file mode 100644 index 0000000..fbe8343 --- /dev/null +++ b/shrine-fog.gemspec @@ -0,0 +1,24 @@ +Gem::Specification.new do |gem| + gem.name = "shrine-fog" + gem.version = "0.1.0" + + gem.required_ruby_version = ">= 2.1" + + gem.summary = "Provides Fog storage for Shrine." + gem.description = "Provides Fog storage for Shrine." + gem.homepage = "https://github.com/janko-m/shrine-fog" + gem.authors = ["Janko Marohnić"] + gem.email = ["janko.marohnic@gmail.com"] + gem.license = "MIT" + + gem.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "shrine-fog.gemspec"] + gem.require_path = "lib" + + gem.add_development_dependency "down", ">= 1.0.3" + + gem.add_development_dependency "fog-aws" + gem.add_development_dependency "mime-types" + gem.add_development_dependency "dotenv" + gem.add_development_dependency "shrine" + gem.add_development_dependency "minitest", "~> 5.8" +end diff --git a/test/storage_test.rb b/test/storage_test.rb new file mode 100644 index 0000000..2f70726 --- /dev/null +++ b/test/storage_test.rb @@ -0,0 +1,46 @@ +require "test_helper" +require "shrine/storage/linter" + +describe Shrine::Storage::Fog do + def storage(**options) + options[:provider] ||= "AWS" + options[:aws_access_key_id] ||= ENV.fetch("S3_ACCESS_KEY_ID") + options[:aws_secret_access_key] ||= ENV.fetch("S3_SECRET_ACCESS_KEY") + options[:region] ||= ENV.fetch("S3_REGION") + options[:directory] ||= ENV.fetch("S3_BUCKET") + + Shrine::Storage::Fog.new(**options) + end + + before do + @storage = storage + shrine = Class.new(Shrine) + shrine.storages = {fog: @storage} + @uploader = shrine.new(:fog) + end + + after do + @storage.clear!(:confirm) + end + + it "passes the linter" do + Shrine::Storage::Linter.call(storage) + Shrine::Storage::Linter.call(storage(prefix: "prefix")) + end + + describe "#upload" do + it "assigns the content type" do + @storage.upload(fakeio, "foo", {"mime_type" => "image/jpeg"}) + tempfile = @storage.download("foo") + + assert_equal "image/jpeg", tempfile.content_type + end + + it "copies the file if it's from the same storage" do + uploaded_file = @uploader.upload(fakeio, location: "foo") + @storage.upload(uploaded_file, "bar") + + assert @storage.exists?("bar") + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..be0ef7b --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,26 @@ +require "minitest/autorun" +require "minitest/pride" + +require "fog/aws" +require "shrine/storage/fog" + +require "forwardable" +require "stringio" + +require "dotenv" +Dotenv.load! + +class FakeIO + def initialize(content) + @io = StringIO.new(content) + end + + extend Forwardable + delegate [:read, :size, :close, :eof?, :rewind] => :@io +end + +class Minitest::Test + def fakeio(content = "file") + FakeIO.new(content) + end +end