2013-08-01 05:26:34 +08:00
|
|
|
require 'digest/sha1'
|
|
|
|
require 'open-uri'
|
|
|
|
|
|
|
|
class S3Store
|
2013-08-30 23:27:42 +08:00
|
|
|
@fog_loaded ||= require 'fog'
|
2013-08-01 05:26:34 +08:00
|
|
|
|
|
|
|
def store_upload(file, upload)
|
2013-08-14 04:08:29 +08:00
|
|
|
# <id><sha1><extension>
|
|
|
|
path = "#{upload.id}#{upload.sha1}#{upload.extension}"
|
2013-08-01 05:26:34 +08:00
|
|
|
|
|
|
|
# if this fails, it will throw an exception
|
2013-08-30 23:27:42 +08:00
|
|
|
upload(file.tempfile, path, upload.original_filename, file.content_type)
|
2013-08-01 05:26:34 +08:00
|
|
|
|
|
|
|
# returns the url of the uploaded file
|
2013-08-14 04:08:29 +08:00
|
|
|
"#{absolute_base_url}/#{path}"
|
2013-08-01 05:26:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def store_optimized_image(file, optimized_image)
|
2013-08-14 04:08:29 +08:00
|
|
|
# <id><sha1>_<width>x<height><extension>
|
|
|
|
path = [
|
2013-08-01 05:26:34 +08:00
|
|
|
optimized_image.id,
|
|
|
|
optimized_image.sha1,
|
|
|
|
"_#{optimized_image.width}x#{optimized_image.height}",
|
2013-08-14 04:08:29 +08:00
|
|
|
optimized_image.extension
|
2013-08-01 05:26:34 +08:00
|
|
|
].join
|
|
|
|
|
|
|
|
# if this fails, it will throw an exception
|
2013-08-14 04:08:29 +08:00
|
|
|
upload(file, path)
|
2013-08-01 05:26:34 +08:00
|
|
|
|
|
|
|
# returns the url of the uploaded file
|
2013-08-14 04:08:29 +08:00
|
|
|
"#{absolute_base_url}/#{path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_avatar(file, upload, size)
|
|
|
|
# /avatars/<sha1>/200.jpg
|
|
|
|
path = File.join(
|
|
|
|
"avatars",
|
|
|
|
upload.sha1,
|
|
|
|
"#{size}#{upload.extension}"
|
|
|
|
)
|
|
|
|
|
|
|
|
# if this fails, it will throw an exception
|
|
|
|
upload(file, path)
|
|
|
|
|
|
|
|
# returns the url of the avatar
|
|
|
|
"#{absolute_base_url}/#{path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_upload(upload)
|
|
|
|
remove_file(upload.url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_optimized_image(optimized_image)
|
|
|
|
remove_file(optimized_image.url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_avatars(upload)
|
|
|
|
|
2013-08-01 05:26:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_file(url)
|
2013-08-30 23:27:42 +08:00
|
|
|
remove File.basename(url) if has_been_uploaded?(url)
|
2013-08-01 05:26:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_been_uploaded?(url)
|
|
|
|
url.start_with?(absolute_base_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def absolute_base_url
|
|
|
|
"//#{s3_bucket}.s3.amazonaws.com"
|
|
|
|
end
|
|
|
|
|
|
|
|
def external?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal?
|
|
|
|
!external?
|
|
|
|
end
|
|
|
|
|
|
|
|
def download(upload)
|
|
|
|
temp_file = Tempfile.new(["discourse-s3", File.extname(upload.original_filename)])
|
|
|
|
url = (SiteSetting.use_ssl? ? "https:" : "http:") + upload.url
|
|
|
|
|
|
|
|
File.open(temp_file.path, "wb") do |f|
|
|
|
|
f.write open(url, "rb", read_timeout: 20).read
|
|
|
|
end
|
|
|
|
|
|
|
|
temp_file
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def s3_bucket
|
|
|
|
SiteSetting.s3_upload_bucket.downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
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-08-30 23:27:42 +08:00
|
|
|
def get_or_create_directory(bucket)
|
2013-08-01 05:26:34 +08:00
|
|
|
check_missing_site_settings
|
|
|
|
|
2013-08-30 23:27:42 +08:00
|
|
|
fog = Fog::Storage.new s3_options
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-08-30 23:27:42 +08:00
|
|
|
directory = fog.directories.get(bucket)
|
|
|
|
directory = fog.directories.create(key: bucket) unless directory
|
2013-08-01 05:26:34 +08:00
|
|
|
directory
|
|
|
|
end
|
|
|
|
|
2013-08-30 23:27:42 +08:00
|
|
|
def s3_options
|
2013-08-01 05:26:34 +08:00
|
|
|
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-08-30 23:27:42 +08:00
|
|
|
def upload(file, unique_filename, filename=nil, content_type=nil)
|
2013-08-01 05:26:34 +08:00
|
|
|
args = {
|
2013-08-30 23:27:42 +08:00
|
|
|
key: unique_filename,
|
2013-08-01 05:26:34 +08:00
|
|
|
public: true,
|
2013-08-30 23:27:42 +08:00
|
|
|
body: file
|
2013-08-01 05:26:34 +08:00
|
|
|
}
|
2013-08-30 23:27:42 +08:00
|
|
|
args[:content_disposition] = "attachment; filename=\"#{filename}\"" if filename
|
2013-08-01 05:26:34 +08:00
|
|
|
args[:content_type] = content_type if content_type
|
|
|
|
|
2013-08-30 23:27:42 +08:00
|
|
|
get_or_create_directory(s3_bucket).files.create(args)
|
2013-08-01 05:26:34 +08:00
|
|
|
end
|
|
|
|
|
2013-08-30 23:27:42 +08:00
|
|
|
def remove(unique_filename)
|
|
|
|
fog = Fog::Storage.new s3_options
|
|
|
|
fog.delete_object(s3_bucket, unique_filename)
|
2013-08-01 05:26:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|