lib/attachs/storages/s3.rb in attachs-0.3.5 vs lib/attachs/storages/s3.rb in attachs-0.4.0
- old
+ new
@@ -1,87 +1,96 @@
module Attachs
module Storages
- class S3
-
- cattr_accessor :config
+ class S3 < Base
- def initialize(default, private)
- if (Rails.env.test? and !default)
- env = 'test'
- elsif default
- env = 'production'
- else
- env = Rails.env
+ def url(*args)
+ if attachment.url?
+ options = args.extract_options!
+ style = (args[0] || :original)
+ if Attachs.config.base_url.present?
+ Pathname.new(Attachs.config.base_url, path(style)).to_s
+ else
+ if options[:ssl].present?
+ secure = options[:ssl]
+ elsif attachment.options[:ssl].present?
+ secure = attachment.options[:ssl]
+ else
+ secure = Attachs.config.s3[:ssl]
+ end
+ object(style).public_url(secure: secure).to_s
+ end
end
- AWS.config access_key_id: config[env]['access_key_id'], secret_access_key: config[env]['secret_access_key']
- @bucket = AWS::S3.new.buckets[config[env]['bucket']]
- @private = private
end
- def exists?(path)
- object(path).exists?
+ def process(force=false)
+ if attachment.upload?
+ stream attachment.upload, path
+ attachment.uploaded!
+ end
+ process_styles force
end
- def size(path)
- object(path).content_length
+ def process_styles(force=false)
+ if attachment.image?
+ unless cache[path]
+ download = Tempfile.new('s3')
+ object.read do |chunk|
+ download.write chunk
+ end
+ cache[path] = download
+ end
+ attachment.styles.each do |style|
+ if force == true
+ object(style).delete
+ end
+ unless object(style).exists?
+ tmp = Tempfile.new('s3')
+ resize cache[path].path, style, tmp.path
+ stream tmp, path(style)
+ end
+ end
+ end
end
- def url(path)
- base_url.present? ? ::File.join(base_url, path) : object(path).public_url(secure: false)
+ def destroy
+ object.delete
+ destroy_styles
end
- def store(upload, path)
- bucket.objects.create(path, Pathname.new(upload.path)).acl = private? ? :private : :public_read
- end
-
- def delete(path)
- object(path).delete
- end
-
- def magick(source, output, upload)
- if cache[source].blank?
- if upload.present?
- cache[source] = upload.path
- else
- remote = create_tmp
- remote.binmode
- object(source).read { |chunk| remote.write(chunk) }
- remote.close
- remote.open
- cache[source] = remote.path
+ def destroy_styles
+ if attachment.image?
+ attachment.styles.each do |style|
+ object(style).delete
end
end
- tmp = create_tmp
- yield Attachs::Magick::Image.new(cache[source], tmp.path)
- store tmp, output
end
protected
- attr_reader :bucket
-
- def config
- self.class.config
+ def move(origin, destination)
+ bucket.objects[origin].move_to(destination)
end
- def base_url
- Rails.application.config.attachs.base_url
+ def cache
+ @cache ||= {}
end
- def private?
- @private
+ def bucket
+ @bucket ||= AWS::S3.new.buckets[Attachs.config.s3[:bucket]]
end
-
- def create_tmp
- Tempfile.new 's3'
- end
- def cache
- @cache ||= {}
+ def object(style=:original)
+ bucket.objects[path(style)]
end
- def object(path)
- bucket.objects[path]
+ def stream(file, path)
+ object = bucket.objects.create(path, File.open(file.path, 'rb'))
+ if attachment.private?
+ object.acl = :private
+ else
+ object.acl = :public_read
+ end
+ cache[path] = file
end
end
end
end