2013-11-05 13:04:47 -05:00
|
|
|
require 'file_store/base_store'
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
module FileStore
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
class S3Store < BaseStore
|
|
|
|
@fog_loaded ||= require 'fog'
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def store_upload(file, upload)
|
|
|
|
path = get_path_for_upload(file, upload)
|
|
|
|
store_file(file, path, upload.original_filename, file.content_type)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def store_optimized_image(file, optimized_image)
|
|
|
|
path = get_path_for_optimized_image(file, optimized_image)
|
|
|
|
store_file(file, path)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def store_avatar(file, avatar, size)
|
|
|
|
path = get_path_for_avatar(file, avatar, size)
|
|
|
|
store_file(file, path)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def remove_upload(upload)
|
|
|
|
remove_file(upload.url)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def remove_optimized_image(optimized_image)
|
|
|
|
remove_file(optimized_image.url)
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def has_been_uploaded?(url)
|
|
|
|
url.start_with?(absolute_base_url)
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def absolute_base_url
|
|
|
|
"//#{s3_bucket}.s3.amazonaws.com"
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def external?
|
|
|
|
true
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def internal?
|
|
|
|
!external?
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def download(upload)
|
|
|
|
@open_uri_loaded ||= require 'open-uri'
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
extension = File.extname(upload.original_filename)
|
|
|
|
temp_file = Tempfile.new(["discourse-s3", extension])
|
|
|
|
url = (SiteSetting.use_ssl? ? "https:" : "http:") + upload.url
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
File.open(temp_file.path, "wb") do |f|
|
|
|
|
f.write(open(url, "rb", read_timeout: 5).read)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
temp_file
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def absolute_avatar_template(avatar)
|
|
|
|
"#{absolute_base_url}/avatars/#{avatar.sha1}/{size}#{avatar.extension}"
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
private
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def get_path_for_upload(file, upload)
|
|
|
|
"#{upload.id}#{upload.sha1}#{upload.extension}"
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def get_path_for_optimized_image(file, optimized_image)
|
|
|
|
"#{optimized_image.id}#{optimized_image.sha1}_#{optimized_image.width}x#{optimized_image.height}#{optimized_image.extension}"
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def get_path_for_avatar(file, avatar, size)
|
|
|
|
"avatars/#{avatar.sha1}/#{size}#{avatar.extension}"
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def store_file(file, path, filename = nil, content_type = nil)
|
|
|
|
# if this fails, it will throw an exception
|
|
|
|
upload(file, path, filename, content_type)
|
|
|
|
# url
|
|
|
|
"#{absolute_base_url}/#{path}"
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def remove_file(url)
|
|
|
|
return unless has_been_uploaded?(url)
|
|
|
|
filename = File.basename(url)
|
|
|
|
remove(filename)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def s3_bucket
|
|
|
|
SiteSetting.s3_upload_bucket.downcase
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def check_missing_site_settings
|
|
|
|
raise Discourse::SiteSettingMissing.new("s3_upload_bucket") if SiteSetting.s3_upload_bucket.blank?
|
|
|
|
raise Discourse::SiteSettingMissing.new("s3_access_key_id") if SiteSetting.s3_access_key_id.blank?
|
|
|
|
raise Discourse::SiteSettingMissing.new("s3_secret_access_key") if SiteSetting.s3_secret_access_key.blank?
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def get_or_create_directory(bucket)
|
|
|
|
check_missing_site_settings
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
fog = Fog::Storage.new(s3_options)
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
directory = fog.directories.get(bucket)
|
|
|
|
directory = fog.directories.create(key: bucket) unless directory
|
|
|
|
directory
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def s3_options
|
|
|
|
options = {
|
|
|
|
provider: 'AWS',
|
|
|
|
aws_access_key_id: SiteSetting.s3_access_key_id,
|
|
|
|
aws_secret_access_key: SiteSetting.s3_secret_access_key,
|
|
|
|
}
|
|
|
|
options[:region] = SiteSetting.s3_region unless SiteSetting.s3_region.empty?
|
|
|
|
options
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def upload(file, unique_filename, filename=nil, content_type=nil)
|
|
|
|
args = {
|
|
|
|
key: unique_filename,
|
|
|
|
public: true,
|
|
|
|
body: file
|
|
|
|
}
|
|
|
|
args[:content_disposition] = "attachment; filename=\"#{filename}\"" if filename
|
|
|
|
args[:content_type] = content_type if content_type
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
get_or_create_directory(s3_bucket).files.create(args)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def remove(unique_filename)
|
|
|
|
check_missing_site_settings
|
|
|
|
|
|
|
|
fog = Fog::Storage.new(s3_options)
|
|
|
|
|
|
|
|
fog.delete_object(s3_bucket, unique_filename)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|