2016-06-30 09:55:01 -05:00
|
|
|
require "uri"
|
2017-06-13 06:27:05 -05:00
|
|
|
require "mini_mime"
|
2015-06-01 04:13:56 -05:00
|
|
|
require_dependency "file_store/base_store"
|
2014-09-24 15:52:09 -05:00
|
|
|
require_dependency "s3_helper"
|
2014-04-15 06:04:14 -05:00
|
|
|
require_dependency "file_helper"
|
2013-07-31 16:26:34 -05:00
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
module FileStore
|
2013-07-31 16:26:34 -05:00
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
class S3Store < BaseStore
|
2015-05-25 10:59:00 -05:00
|
|
|
TOMBSTONE_PREFIX ||= "tombstone/"
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def initialize(s3_helper = nil)
|
2016-08-19 01:08:04 -05:00
|
|
|
@s3_helper = s3_helper || S3Helper.new(s3_bucket, TOMBSTONE_PREFIX)
|
2014-09-24 15:52:09 -05:00
|
|
|
end
|
2013-07-31 16:26:34 -05:00
|
|
|
|
2015-05-29 11:39:47 -05:00
|
|
|
def store_upload(file, upload, content_type = nil)
|
2016-08-15 03:06:29 -05:00
|
|
|
path = get_path_for_upload(upload)
|
2015-05-25 10:59:00 -05:00
|
|
|
store_file(file, path, filename: upload.original_filename, content_type: content_type, cache_locally: true)
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
2013-07-31 16:26:34 -05:00
|
|
|
|
2016-10-17 12:16:29 -05:00
|
|
|
def store_optimized_image(file, optimized_image, content_type = nil)
|
2016-08-15 03:06:29 -05:00
|
|
|
path = get_path_for_optimized_image(optimized_image)
|
2016-10-17 12:16:29 -05:00
|
|
|
store_file(file, path, content_type: content_type)
|
2016-08-12 04:18:19 -05:00
|
|
|
end
|
|
|
|
|
2015-05-29 11:39:47 -05:00
|
|
|
# options
|
|
|
|
# - filename
|
|
|
|
# - content_type
|
|
|
|
# - cache_locally
|
2017-07-27 20:20:09 -05:00
|
|
|
def store_file(file, path, opts = {})
|
2016-10-17 12:16:29 -05:00
|
|
|
filename = opts[:filename].presence || File.basename(path)
|
2015-05-29 11:39:47 -05:00
|
|
|
# cache file locally when needed
|
|
|
|
cache_file(file, File.basename(path)) if opts[:cache_locally]
|
|
|
|
# stored uploaded are public by default
|
2016-10-17 12:16:29 -05:00
|
|
|
options = {
|
|
|
|
acl: "public-read",
|
2017-06-13 06:27:05 -05:00
|
|
|
content_type: opts[:content_type].presence || MiniMime.lookup_by_filename(filename)&.content_type
|
2016-10-17 12:16:29 -05:00
|
|
|
}
|
2015-05-29 11:39:47 -05:00
|
|
|
# add a "content disposition" header for "attachments"
|
2016-10-17 12:16:29 -05:00
|
|
|
options[:content_disposition] = "attachment; filename=\"#{filename}\"" unless FileHelper.is_image?(filename)
|
2015-05-29 11:39:47 -05:00
|
|
|
# if this fails, it will throw an exception
|
2016-08-15 03:06:29 -05:00
|
|
|
path = @s3_helper.upload(file, path, options)
|
2015-05-29 11:39:47 -05:00
|
|
|
# return the upload url
|
2018-07-06 17:15:28 -05:00
|
|
|
"#{absolute_base_url}/#{path}"
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
2013-07-31 16:26:34 -05:00
|
|
|
|
2016-08-14 22:21:24 -05:00
|
|
|
def remove_file(url, path)
|
2015-05-29 11:39:47 -05:00
|
|
|
return unless has_been_uploaded?(url)
|
|
|
|
# copy the removed file to tombstone
|
2016-08-15 03:06:29 -05:00
|
|
|
@s3_helper.remove(path, true)
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
2013-08-13 15:08:29 -05:00
|
|
|
|
2018-08-07 22:26:05 -05:00
|
|
|
def copy_file(url, source, destination)
|
|
|
|
return unless has_been_uploaded?(url)
|
|
|
|
@s3_helper.copy(source, destination)
|
|
|
|
end
|
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
def has_been_uploaded?(url)
|
2015-05-26 04:47:33 -05:00
|
|
|
return false if url.blank?
|
2016-06-30 09:55:01 -05:00
|
|
|
|
|
|
|
base_hostname = URI.parse(absolute_base_url).hostname
|
|
|
|
return true if url[base_hostname]
|
|
|
|
|
2017-10-06 00:20:01 -05:00
|
|
|
return false if SiteSetting.Upload.s3_cdn_url.blank?
|
|
|
|
cdn_hostname = URI.parse(SiteSetting.Upload.s3_cdn_url || "").hostname
|
2016-06-30 09:55:01 -05:00
|
|
|
cdn_hostname.presence && url[cdn_hostname]
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
2013-08-13 15:08:29 -05:00
|
|
|
|
2017-10-06 00:20:01 -05:00
|
|
|
def s3_bucket_name
|
|
|
|
@s3_helper.s3_bucket_name
|
|
|
|
end
|
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
def absolute_base_url
|
2017-10-06 00:20:01 -05:00
|
|
|
@absolute_base_url ||= SiteSetting.Upload.absolute_base_url
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
2013-08-13 15:08:29 -05:00
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
def external?
|
|
|
|
true
|
|
|
|
end
|
2013-08-13 15:08:29 -05:00
|
|
|
|
2013-11-27 15:01:41 -06:00
|
|
|
def purge_tombstone(grace_period)
|
2014-09-24 15:52:09 -05:00
|
|
|
@s3_helper.update_tombstone_lifecycle(grace_period)
|
2013-11-27 15:01:41 -06:00
|
|
|
end
|
|
|
|
|
2015-05-25 22:08:31 -05:00
|
|
|
def path_for(upload)
|
2015-06-01 10:49:58 -05:00
|
|
|
url = upload.try(:url)
|
2016-06-30 09:55:01 -05:00
|
|
|
FileStore::LocalStore.new.path_for(upload) if url && url[/^\/[^\/]/]
|
2015-05-25 22:08:31 -05:00
|
|
|
end
|
|
|
|
|
2015-05-26 21:02:57 -05:00
|
|
|
def cdn_url(url)
|
2017-10-06 00:20:01 -05:00
|
|
|
return url if SiteSetting.Upload.s3_cdn_url.blank?
|
2016-06-30 09:55:01 -05:00
|
|
|
schema = url[/^(https?:)?\/\//, 1]
|
2018-07-06 17:15:28 -05:00
|
|
|
folder = @s3_helper.s3_bucket_folder_path.nil? ? "" : "#{@s3_helper.s3_bucket_folder_path}/"
|
2018-08-21 21:31:13 -05:00
|
|
|
url.sub("#{schema}#{absolute_base_url}/#{folder}", "#{SiteSetting.Upload.s3_cdn_url}/")
|
2015-05-26 21:02:57 -05:00
|
|
|
end
|
|
|
|
|
2015-05-29 11:39:47 -05: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-27 15:01:41 -06:00
|
|
|
|
2015-05-29 11:39:47 -05:00
|
|
|
def avatar_template(avatar, user_id)
|
|
|
|
UserAvatar.external_avatar_url(user_id, avatar.upload_id, avatar.width)
|
|
|
|
end
|
2013-07-31 16:26:34 -05:00
|
|
|
|
2016-08-19 01:08:04 -05:00
|
|
|
def s3_bucket
|
2017-10-06 00:20:01 -05:00
|
|
|
raise Discourse::SiteSettingMissing.new("s3_upload_bucket") if SiteSetting.Upload.s3_upload_bucket.blank?
|
|
|
|
SiteSetting.Upload.s3_upload_bucket.downcase
|
2015-05-29 11:39:47 -05:00
|
|
|
end
|
2013-07-31 16:26:34 -05:00
|
|
|
end
|
|
|
|
end
|