2013-07-17 06:06:47 +08:00
|
|
|
module S3Store
|
2013-06-05 06:34:53 +08:00
|
|
|
|
2013-07-11 04:54:05 +08:00
|
|
|
def self.store_file(file, sha1, upload_id)
|
2013-07-17 06:06:47 +08:00
|
|
|
S3Store.check_missing_site_settings
|
2013-06-20 03:51:41 +08:00
|
|
|
|
2013-07-17 06:06:47 +08:00
|
|
|
directory = S3Store.get_or_create_directory(SiteSetting.s3_upload_bucket)
|
2013-07-11 04:54:05 +08:00
|
|
|
extension = File.extname(file.original_filename)
|
|
|
|
remote_filename = "#{upload_id}#{sha1}#{extension}"
|
2013-06-20 03:51:41 +08:00
|
|
|
|
|
|
|
# if this fails, it will throw an exception
|
2013-07-17 06:06:47 +08:00
|
|
|
file = S3Store.upload(file, remote_filename, directory)
|
|
|
|
"#{S3Store.base_url}/#{remote_filename}"
|
2013-06-22 19:38:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.base_url
|
2013-07-17 06:32:09 +08:00
|
|
|
"//#{SiteSetting.s3_upload_bucket.downcase}.s3.amazonaws.com"
|
|
|
|
end
|
|
|
|
|
2013-06-20 03:51:41 +08:00
|
|
|
def self.remove_file(url)
|
2013-07-17 06:06:47 +08:00
|
|
|
S3Store.check_missing_site_settings
|
2013-06-20 03:51:41 +08:00
|
|
|
|
2013-07-17 06:06:47 +08:00
|
|
|
directory = S3Store.get_or_create_directory(SiteSetting.s3_upload_bucket)
|
2013-06-20 03:51:41 +08:00
|
|
|
|
2013-07-17 06:06:47 +08:00
|
|
|
file = S3Store.destroy(url, directory)
|
2013-06-20 03:51:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.check_missing_site_settings
|
2013-06-05 06:34:53 +08:00
|
|
|
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?
|
2013-06-20 03:51:41 +08:00
|
|
|
end
|
2013-06-05 06:34:53 +08:00
|
|
|
|
2013-06-20 03:51:41 +08:00
|
|
|
def self.get_or_create_directory(name)
|
2013-06-05 06:34:53 +08:00
|
|
|
@fog_loaded = require 'fog' unless @fog_loaded
|
|
|
|
|
2013-07-17 06:06:47 +08:00
|
|
|
options = S3Store.generate_options
|
2013-06-05 06:34:53 +08:00
|
|
|
|
2013-06-20 03:51:41 +08:00
|
|
|
fog = Fog::Storage.new(options)
|
|
|
|
|
|
|
|
directory = fog.directories.get(name)
|
|
|
|
directory = fog.directories.create(key: name) unless directory
|
|
|
|
|
|
|
|
directory
|
2013-06-05 06:34:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.generate_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
|
|
|
|
|
|
|
|
def self.upload(file, name, directory)
|
|
|
|
directory.files.create(
|
|
|
|
key: name,
|
|
|
|
public: true,
|
|
|
|
body: file.tempfile,
|
|
|
|
content_type: file.content_type
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-06-20 03:51:41 +08:00
|
|
|
def self.destroy(name, directory)
|
|
|
|
directory.files.destroy(key: name)
|
|
|
|
end
|
|
|
|
|
2013-06-05 06:34:53 +08:00
|
|
|
end
|