Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
janko committed Dec 5, 2015
0 parents commit e6a8205
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Gemfile.lock
pkg/
.env
13 changes: 13 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -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/)
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gemspec

gem 'pry'
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "bundler/gem_tasks"
95 changes: 95 additions & 0 deletions lib/shrine/storage/fog.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions shrine-fog.gemspec
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions test/storage_test.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e6a8205

Please sign in to comment.