discourse/lib/file_store/s3_store.rb

111 lines
3.4 KiB
Ruby
Raw Normal View History

2016-06-30 22:55:01 +08:00
require "uri"
require_dependency "file_store/base_store"
require_dependency "file_store/local_store"
require_dependency "s3_helper"
2014-04-15 19:04:14 +08:00
require_dependency "file_helper"
2013-11-06 02:04:47 +08:00
module FileStore
2013-11-06 02:04:47 +08:00
class S3Store < BaseStore
TOMBSTONE_PREFIX ||= "tombstone/"
def initialize(s3_helper=nil)
@s3_helper = s3_helper || S3Helper.new(s3_bucket, TOMBSTONE_PREFIX)
end
2015-05-30 00:39:47 +08:00
def store_upload(file, upload, content_type = nil)
path = get_path_for_upload(upload)
store_file(file, path, filename: upload.original_filename, content_type: content_type, cache_locally: true)
2013-11-06 02:04:47 +08:00
end
2015-05-30 00:39:47 +08:00
# options
# - filename
# - content_type
# - cache_locally
def store_file(file, path, opts={})
2016-06-30 22:55:01 +08:00
filename = opts[:filename].presence
content_type = opts[:content_type].presence
2015-05-30 00:39:47 +08:00
# cache file locally when needed
cache_file(file, File.basename(path)) if opts[:cache_locally]
# stored uploaded are public by default
options = { acl: "public-read" }
# add a "content disposition" header for "attachments"
options[:content_disposition] = "attachment; filename=\"#{filename}\"" if filename && !FileHelper.is_image?(filename)
# add a "content type" header when provided
options[:content_type] = content_type if content_type
# if this fails, it will throw an exception
@s3_helper.upload(file, path, options)
# return the upload url
"#{absolute_base_url}/#{path}"
2013-11-06 02:04:47 +08:00
end
2015-05-30 00:39:47 +08:00
def remove_file(url)
return unless has_been_uploaded?(url)
filename = File.basename(url)
# copy the removed file to tombstone
@s3_helper.remove(filename, true)
2013-11-06 02:04:47 +08:00
end
2013-08-14 04:08:29 +08:00
2013-11-06 02:04:47 +08:00
def has_been_uploaded?(url)
return false if url.blank?
2016-06-30 22:55:01 +08:00
base_hostname = URI.parse(absolute_base_url).hostname
return true if url[base_hostname]
return false if SiteSetting.s3_cdn_url.blank?
cdn_hostname = URI.parse(SiteSetting.s3_cdn_url || "").hostname
cdn_hostname.presence && url[cdn_hostname]
2013-11-06 02:04:47 +08:00
end
2013-08-14 04:08:29 +08:00
2013-11-06 02:04:47 +08:00
def absolute_base_url
# cf. http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
@absolute_base_url ||= if SiteSetting.s3_region == "us-east-1"
"//#{s3_bucket}.s3.amazonaws.com"
2016-07-14 22:56:09 +08:00
elsif SiteSetting.s3_region == 'cn-north-1'
"//#{s3_bucket}.s3.cn-north-1.amazonaws.com.cn"
else
"//#{s3_bucket}.s3-#{SiteSetting.s3_region}.amazonaws.com"
end
2013-11-06 02:04:47 +08:00
end
2013-08-14 04:08:29 +08:00
2013-11-06 02:04:47 +08:00
def external?
true
end
2013-08-14 04:08:29 +08:00
2013-11-28 05:01:41 +08:00
def purge_tombstone(grace_period)
@s3_helper.update_tombstone_lifecycle(grace_period)
2013-11-28 05:01:41 +08:00
end
def path_for(upload)
2015-06-01 23:49:58 +08:00
url = upload.try(:url)
2016-06-30 22:55:01 +08:00
FileStore::LocalStore.new.path_for(upload) if url && url[/^\/[^\/]/]
end
def cdn_url(url)
2015-05-30 00:39:47 +08:00
return url if SiteSetting.s3_cdn_url.blank?
2016-06-30 22:55:01 +08:00
schema = url[/^(https?:)?\/\//, 1]
url.sub("#{schema}#{absolute_base_url}", SiteSetting.s3_cdn_url)
end
2015-05-30 00:39:47 +08:00
def cache_avatar(avatar, user_id)
source = avatar.url.sub(absolute_base_url + "/", "")
destination = avatar_template(avatar, user_id).sub(absolute_base_url + "/", "")
@s3_helper.copy(source, destination)
end
2013-11-28 05:01:41 +08:00
2015-05-30 00:39:47 +08:00
def avatar_template(avatar, user_id)
UserAvatar.external_avatar_url(user_id, avatar.upload_id, avatar.width)
end
2015-05-30 00:39:47 +08:00
def s3_bucket
2016-06-30 22:55:01 +08:00
@s3_bucket ||= begin
raise Discourse::SiteSettingMissing.new("s3_upload_bucket") if SiteSetting.s3_upload_bucket.blank?
SiteSetting.s3_upload_bucket.downcase
end
2015-05-30 00:39:47 +08:00
end
end
end