2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-30 22:55:01 +08:00
|
|
|
require "uri"
|
2017-06-13 19:27:05 +08:00
|
|
|
require "mini_mime"
|
2015-06-01 17:13:56 +08:00
|
|
|
require_dependency "file_store/base_store"
|
2014-09-25 04:52:09 +08:00
|
|
|
require_dependency "s3_helper"
|
2014-04-15 19:04:14 +08:00
|
|
|
require_dependency "file_helper"
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
module FileStore
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
class S3Store < BaseStore
|
2015-05-25 23:59:00 +08:00
|
|
|
TOMBSTONE_PREFIX ||= "tombstone/"
|
|
|
|
|
2018-09-10 15:14:30 +08:00
|
|
|
attr_reader :s3_helper
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def initialize(s3_helper = nil)
|
2018-12-19 13:32:32 +08:00
|
|
|
@s3_helper = s3_helper || S3Helper.new(s3_bucket,
|
|
|
|
Rails.configuration.multisite ? multisite_tombstone_prefix : TOMBSTONE_PREFIX
|
|
|
|
)
|
2014-09-25 04:52:09 +08:00
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2015-05-30 00:39:47 +08:00
|
|
|
def store_upload(file, upload, content_type = nil)
|
2016-08-15 16:06:29 +08:00
|
|
|
path = get_path_for_upload(upload)
|
2019-11-18 09:25:42 +08:00
|
|
|
url, upload.etag = store_file(file, path, filename: upload.original_filename, content_type: content_type, cache_locally: true, private_acl: upload.secure?)
|
2019-01-04 14:16:22 +08:00
|
|
|
url
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2019-11-18 09:25:42 +08:00
|
|
|
def store_optimized_image(file, optimized_image, content_type = nil, secure: false)
|
2016-08-15 16:06:29 +08:00
|
|
|
path = get_path_for_optimized_image(optimized_image)
|
2019-11-18 09:25:42 +08:00
|
|
|
url, optimized_image.etag = store_file(file, path, content_type: content_type, private_acl: secure)
|
2019-01-04 14:16:22 +08:00
|
|
|
url
|
2016-08-12 17:18:19 +08:00
|
|
|
end
|
|
|
|
|
2015-05-30 00:39:47 +08:00
|
|
|
# options
|
|
|
|
# - filename
|
|
|
|
# - content_type
|
|
|
|
# - cache_locally
|
2017-07-28 09:20:09 +08:00
|
|
|
def store_file(file, path, opts = {})
|
2019-05-03 06:17:27 +08:00
|
|
|
path = path.dup
|
|
|
|
|
2016-10-18 01:16:29 +08:00
|
|
|
filename = opts[:filename].presence || File.basename(path)
|
2015-05-30 00:39:47 +08:00
|
|
|
# cache file locally when needed
|
|
|
|
cache_file(file, File.basename(path)) if opts[:cache_locally]
|
2016-10-18 01:16:29 +08:00
|
|
|
options = {
|
2019-11-18 09:25:42 +08:00
|
|
|
acl: opts[:private_acl] ? "private" : "public-read",
|
2019-08-07 01:55:17 +08:00
|
|
|
cache_control: 'max-age=31556952, public, immutable',
|
2017-06-13 19:27:05 +08:00
|
|
|
content_type: opts[:content_type].presence || MiniMime.lookup_by_filename(filename)&.content_type
|
2016-10-18 01:16:29 +08:00
|
|
|
}
|
2015-05-30 00:39:47 +08:00
|
|
|
# add a "content disposition" header for "attachments"
|
2019-11-18 09:25:42 +08:00
|
|
|
options[:content_disposition] = "attachment; filename=\"#{filename}\"" unless FileHelper.is_supported_media?(filename)
|
2018-11-29 12:11:48 +08:00
|
|
|
|
2018-12-03 12:04:14 +08:00
|
|
|
path.prepend(File.join(upload_path, "/")) if Rails.configuration.multisite
|
2018-11-29 12:11:48 +08:00
|
|
|
|
2019-01-04 14:16:22 +08:00
|
|
|
# if this fails, it will throw an exception
|
|
|
|
path, etag = @s3_helper.upload(file, path, options)
|
|
|
|
|
|
|
|
# return the upload url and etag
|
2019-11-15 04:10:51 +08:00
|
|
|
[File.join(absolute_base_url, path), etag]
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2016-08-15 11:21:24 +08:00
|
|
|
def remove_file(url, path)
|
2015-05-30 00:39:47 +08:00
|
|
|
return unless has_been_uploaded?(url)
|
|
|
|
# copy the removed file to tombstone
|
2016-08-15 16:06:29 +08:00
|
|
|
@s3_helper.remove(path, true)
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-14 04:08:29 +08:00
|
|
|
|
2018-08-08 11:26:05 +08:00
|
|
|
def copy_file(url, source, destination)
|
|
|
|
return unless has_been_uploaded?(url)
|
|
|
|
@s3_helper.copy(source, destination)
|
|
|
|
end
|
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def has_been_uploaded?(url)
|
2015-05-26 17:47:33 +08:00
|
|
|
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]
|
|
|
|
|
2017-10-06 13:20:01 +08:00
|
|
|
return false if SiteSetting.Upload.s3_cdn_url.blank?
|
|
|
|
cdn_hostname = URI.parse(SiteSetting.Upload.s3_cdn_url || "").hostname
|
2016-06-30 22:55:01 +08:00
|
|
|
cdn_hostname.presence && url[cdn_hostname]
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-14 04:08:29 +08:00
|
|
|
|
2017-10-06 13:20:01 +08:00
|
|
|
def s3_bucket_name
|
|
|
|
@s3_helper.s3_bucket_name
|
|
|
|
end
|
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def absolute_base_url
|
2017-10-06 13:20:01 +08:00
|
|
|
@absolute_base_url ||= SiteSetting.Upload.absolute_base_url
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-14 04:08:29 +08:00
|
|
|
|
2019-11-18 09:25:42 +08:00
|
|
|
def s3_upload_host
|
|
|
|
SiteSetting.Upload.s3_cdn_url.present? ? SiteSetting.Upload.s3_cdn_url : "https:#{absolute_base_url}"
|
|
|
|
end
|
|
|
|
|
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)
|
2014-09-25 04:52:09 +08:00
|
|
|
@s3_helper.update_tombstone_lifecycle(grace_period)
|
2013-11-28 05:01:41 +08:00
|
|
|
end
|
|
|
|
|
2018-12-19 13:32:32 +08:00
|
|
|
def multisite_tombstone_prefix
|
|
|
|
File.join("uploads", "tombstone", RailsMultisite::ConnectionManagement.current_db, "/")
|
|
|
|
end
|
|
|
|
|
2019-07-04 23:32:51 +08:00
|
|
|
def download_url(upload)
|
|
|
|
return unless upload
|
|
|
|
"#{upload.short_path}?dl=1"
|
|
|
|
end
|
|
|
|
|
2015-05-26 11:08:31 +08:00
|
|
|
def path_for(upload)
|
2019-05-29 09:00:25 +08:00
|
|
|
url = upload&.url
|
2019-05-29 18:36:09 +08:00
|
|
|
FileStore::LocalStore.new.path_for(upload) if url && url[/^\/[^\/]/]
|
2015-05-26 11:08:31 +08:00
|
|
|
end
|
|
|
|
|
2019-07-04 23:32:51 +08:00
|
|
|
def url_for(upload, force_download: false)
|
2019-11-18 09:25:42 +08:00
|
|
|
upload.secure? || force_download ?
|
|
|
|
presigned_url(get_upload_key(upload), force_download: force_download, filename: upload.original_filename) :
|
|
|
|
upload.url
|
2019-06-06 11:27:24 +08:00
|
|
|
end
|
|
|
|
|
2015-05-27 10:02:57 +08:00
|
|
|
def cdn_url(url)
|
2017-10-06 13:20:01 +08:00
|
|
|
return url if SiteSetting.Upload.s3_cdn_url.blank?
|
2016-06-30 22:55:01 +08:00
|
|
|
schema = url[/^(https?:)?\/\//, 1]
|
2018-07-07 06:15:28 +08:00
|
|
|
folder = @s3_helper.s3_bucket_folder_path.nil? ? "" : "#{@s3_helper.s3_bucket_folder_path}/"
|
2018-11-29 12:11:48 +08:00
|
|
|
url.sub(File.join("#{schema}#{absolute_base_url}", folder), File.join(SiteSetting.Upload.s3_cdn_url, "/"))
|
2015-05-27 10:02:57 +08:00
|
|
|
end
|
|
|
|
|
2019-11-18 09:25:42 +08:00
|
|
|
def signed_url_for_path(path)
|
|
|
|
key = path.sub(absolute_base_url + "/", "")
|
|
|
|
presigned_url(key)
|
|
|
|
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
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2016-08-19 14:08:04 +08:00
|
|
|
def s3_bucket
|
2017-10-06 13:20:01 +08:00
|
|
|
raise Discourse::SiteSettingMissing.new("s3_upload_bucket") if SiteSetting.Upload.s3_upload_bucket.blank?
|
|
|
|
SiteSetting.Upload.s3_upload_bucket.downcase
|
2015-05-30 00:39:47 +08:00
|
|
|
end
|
2018-11-27 03:24:51 +08:00
|
|
|
|
2019-02-15 03:04:35 +08:00
|
|
|
def list_missing_uploads(skip_optimized: false)
|
2019-02-01 12:40:48 +08:00
|
|
|
if SiteSetting.enable_s3_inventory
|
|
|
|
require 's3_inventory'
|
2019-02-20 00:24:35 +08:00
|
|
|
S3Inventory.new(s3_helper, :upload).backfill_etags_and_list_missing
|
|
|
|
S3Inventory.new(s3_helper, :optimized).backfill_etags_and_list_missing unless skip_optimized
|
2019-02-01 12:40:48 +08:00
|
|
|
else
|
2019-03-13 17:39:07 +08:00
|
|
|
list_missing(Upload.by_users, "original/")
|
2019-02-01 12:40:48 +08:00
|
|
|
list_missing(OptimizedImage, "optimized/") unless skip_optimized
|
|
|
|
end
|
2018-11-27 03:24:51 +08:00
|
|
|
end
|
|
|
|
|
2019-06-06 11:27:24 +08:00
|
|
|
def update_upload_ACL(upload)
|
|
|
|
key = get_upload_key(upload)
|
2019-11-18 09:25:42 +08:00
|
|
|
update_ACL(key, upload.secure?)
|
2019-06-06 11:27:24 +08:00
|
|
|
|
2019-11-18 09:25:42 +08:00
|
|
|
upload.optimized_images.find_each do |optimized_image|
|
|
|
|
optimized_image_key = get_path_for_optimized_image(optimized_image)
|
|
|
|
update_ACL(optimized_image_key, upload.secure?)
|
2019-06-06 11:27:24 +08:00
|
|
|
end
|
2019-11-18 09:25:42 +08:00
|
|
|
|
|
|
|
true
|
2019-06-06 11:27:24 +08:00
|
|
|
end
|
|
|
|
|
2019-07-02 02:38:36 +08:00
|
|
|
def download_file(upload, destination_path)
|
|
|
|
@s3_helper.download_file(get_upload_key(upload), destination_path)
|
|
|
|
end
|
|
|
|
|
2020-01-13 07:12:27 +08:00
|
|
|
def copy_from(source_path)
|
|
|
|
local_store = FileStore::LocalStore.new
|
|
|
|
public_upload_path = File.join(local_store.public_dir, local_store.upload_path)
|
|
|
|
|
|
|
|
# The migration to S3 and lots of other code expects files to exist in public/uploads,
|
|
|
|
# so lets move them there before executing the migration.
|
|
|
|
if public_upload_path != source_path
|
|
|
|
if Dir.exist?(public_upload_path)
|
|
|
|
old_upload_path = "#{public_upload_path}_#{SecureRandom.hex}"
|
|
|
|
FileUtils.mv(public_upload_path, old_upload_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
FileUtils.mkdir_p(File.expand_path("..", public_upload_path))
|
|
|
|
FileUtils.symlink(source_path, public_upload_path)
|
|
|
|
|
|
|
|
FileStore::ToS3Migration.new(
|
|
|
|
s3_options: FileStore::ToS3Migration.s3_options_from_env,
|
|
|
|
migrate_to_multisite: Rails.configuration.multisite,
|
|
|
|
).migrate
|
|
|
|
|
|
|
|
ensure
|
|
|
|
FileUtils.rm(public_upload_path) if File.symlink?(public_upload_path)
|
|
|
|
FileUtils.mv(old_upload_path, public_upload_path) if old_upload_path
|
|
|
|
end
|
|
|
|
|
2018-11-27 03:24:51 +08:00
|
|
|
private
|
|
|
|
|
2019-11-18 09:25:42 +08:00
|
|
|
def presigned_url(url, force_download: false, filename: false)
|
|
|
|
opts = { expires_in: S3Helper::DOWNLOAD_URL_EXPIRES_AFTER_SECONDS }
|
|
|
|
if force_download && filename
|
|
|
|
opts[:response_content_disposition] = ActionDispatch::Http::ContentDisposition.format(
|
|
|
|
disposition: "attachment", filename: filename
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
obj = @s3_helper.object(url)
|
|
|
|
obj.presigned_url(:get, opts)
|
|
|
|
end
|
|
|
|
|
2019-06-06 11:27:24 +08:00
|
|
|
def get_upload_key(upload)
|
|
|
|
if Rails.configuration.multisite
|
|
|
|
File.join(upload_path, "/", get_path_for_upload(upload))
|
|
|
|
else
|
|
|
|
get_path_for_upload(upload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-18 09:25:42 +08:00
|
|
|
def update_ACL(key, secure)
|
|
|
|
begin
|
|
|
|
@s3_helper.object(key).acl.put(acl: secure ? "private" : "public-read")
|
|
|
|
rescue Aws::S3::Errors::NoSuchKey
|
|
|
|
Rails.logger.warn("Could not update ACL on upload with key: '#{key}'. Upload is missing.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-27 03:24:51 +08:00
|
|
|
def list_missing(model, prefix)
|
|
|
|
connection = ActiveRecord::Base.connection.raw_connection
|
|
|
|
connection.exec('CREATE TEMP TABLE verified_ids(val integer PRIMARY KEY)')
|
|
|
|
marker = nil
|
|
|
|
files = @s3_helper.list(prefix, marker)
|
|
|
|
|
|
|
|
while files.count > 0 do
|
|
|
|
verified_ids = []
|
|
|
|
|
|
|
|
files.each do |f|
|
2019-10-21 18:32:27 +08:00
|
|
|
id = model.where("url LIKE '%#{f.key}' AND etag = '#{f.etag}'").pluck_first(:id)
|
2018-11-27 03:24:51 +08:00
|
|
|
verified_ids << id if id.present?
|
|
|
|
marker = f.key
|
|
|
|
end
|
|
|
|
|
|
|
|
verified_id_clause = verified_ids.map { |id| "('#{PG::Connection.escape_string(id.to_s)}')" }.join(",")
|
|
|
|
connection.exec("INSERT INTO verified_ids VALUES #{verified_id_clause}")
|
|
|
|
files = @s3_helper.list(prefix, marker)
|
|
|
|
end
|
|
|
|
|
2019-02-01 12:40:48 +08:00
|
|
|
missing_uploads = model.joins('LEFT JOIN verified_ids ON verified_ids.val = id').where("verified_ids.val IS NULL")
|
2018-11-27 03:24:51 +08:00
|
|
|
missing_count = missing_uploads.count
|
|
|
|
|
|
|
|
if missing_count > 0
|
|
|
|
missing_uploads.find_each do |upload|
|
|
|
|
puts upload.url
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "#{missing_count} of #{model.count} #{model.name.underscore.pluralize} are missing"
|
|
|
|
end
|
|
|
|
ensure
|
2018-11-27 03:45:29 +08:00
|
|
|
connection.exec('DROP TABLE verified_ids') unless connection.nil?
|
2018-11-27 03:24:51 +08:00
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
end
|
|
|
|
end
|