2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
module FileStore
|
2023-05-11 04:27:27 -05:00
|
|
|
class DownloadError < StandardError
|
|
|
|
end
|
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
class BaseStore
|
2024-10-15 21:09:07 -05:00
|
|
|
UPLOAD_PATH_REGEX = %r{/(original/\d+X/.*)}
|
|
|
|
OPTIMIZED_IMAGE_PATH_REGEX = %r{/(optimized/\d+X/.*)}
|
|
|
|
TEMPORARY_UPLOAD_PREFIX = "temp/"
|
2013-11-05 12:04:47 -06:00
|
|
|
|
2014-04-15 06:04:14 -05:00
|
|
|
def store_upload(file, upload, content_type = nil)
|
2021-05-27 10:42:25 -05:00
|
|
|
upload.url = nil
|
2015-05-29 11:39:47 -05:00
|
|
|
path = get_path_for_upload(upload)
|
|
|
|
store_file(file, path)
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
|
2020-01-15 21:50:27 -06:00
|
|
|
def store_optimized_image(file, optimized_image, content_type = nil, secure: false)
|
2021-05-27 10:42:25 -05:00
|
|
|
optimized_image.url = nil
|
2015-05-29 11:39:47 -05:00
|
|
|
path = get_path_for_optimized_image(optimized_image)
|
|
|
|
store_file(file, path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_file(file, path, opts = {})
|
2016-08-11 22:43:57 -05:00
|
|
|
not_implemented
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_upload(upload)
|
2016-08-14 22:21:24 -05:00
|
|
|
remove_file(upload.url, get_path_for_upload(upload))
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_optimized_image(optimized_image)
|
2016-08-14 22:21:24 -05:00
|
|
|
remove_file(optimized_image.url, get_path_for_optimized_image(optimized_image))
|
2015-05-29 11:39:47 -05:00
|
|
|
end
|
|
|
|
|
2016-08-14 22:21:24 -05:00
|
|
|
def remove_file(url, path)
|
2016-08-11 22:43:57 -05:00
|
|
|
not_implemented
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
|
2018-11-28 22:11:48 -06:00
|
|
|
def upload_path
|
2019-12-17 23:51:57 -06:00
|
|
|
path = File.join("uploads", RailsMultisite::ConnectionManagement.current_db)
|
2020-04-28 08:03:04 -05:00
|
|
|
return path if !Rails.env.test?
|
|
|
|
File.join(path, "test_#{ENV["TEST_ENV_NUMBER"].presence || "0"}")
|
2018-11-28 22:11:48 -06:00
|
|
|
end
|
|
|
|
|
FEATURE: Direct S3 multipart uploads for backups (#14736)
This PR introduces a new `enable_experimental_backup_uploads` site setting (default false and hidden), which when enabled alongside `enable_direct_s3_uploads` will allow for direct S3 multipart uploads of backup .tar.gz files.
To make multipart external uploads work with both the S3BackupStore and the S3Store, I've had to move several methods out of S3Store and into S3Helper, including:
* presigned_url
* create_multipart
* abort_multipart
* complete_multipart
* presign_multipart_part
* list_multipart_parts
Then, S3Store and S3BackupStore either delegate directly to S3Helper or have their own special methods to call S3Helper for these methods. FileStore.temporary_upload_path has also removed its dependence on upload_path, and can now be used interchangeably between the stores. A similar change was made in the frontend as well, moving the multipart related JS code out of ComposerUppyUpload and into a mixin of its own, so it can also be used by UppyUploadMixin.
Some changes to ExternalUploadManager had to be made here as well. The backup direct uploads do not need an Upload record made for them in the database, so they can be moved to their final S3 resting place when completing the multipart upload.
This changeset is not perfect; it introduces some special cases in UploadController to handle backups that was previously in BackupController, because UploadController is where the multipart routes are located. A subsequent pull request will pull these routes into a module or some other sharing pattern, along with hooks, so the backup controller and the upload controller (and any future controllers that may need them) can include these routes in a nicer way.
2021-11-10 16:25:31 -06:00
|
|
|
def self.temporary_upload_path(file_name, folder_prefix: "")
|
2021-09-05 19:21:20 -05:00
|
|
|
# We don't want to use the original file name as it can contain special
|
|
|
|
# characters, which can interfere with external providers operations and
|
|
|
|
# introduce other unexpected behaviour.
|
|
|
|
file_name_random = "#{SecureRandom.hex}#{File.extname(file_name)}"
|
2021-07-27 17:42:25 -05:00
|
|
|
File.join(TEMPORARY_UPLOAD_PREFIX, folder_prefix, SecureRandom.hex, file_name_random)
|
|
|
|
end
|
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
def has_been_uploaded?(url)
|
2016-08-11 22:43:57 -05:00
|
|
|
not_implemented
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
|
2015-05-29 11:39:47 -05:00
|
|
|
def download_url(upload)
|
2016-08-11 22:43:57 -05:00
|
|
|
not_implemented
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
|
2015-05-29 11:39:47 -05:00
|
|
|
def cdn_url(url)
|
2016-08-11 22:43:57 -05:00
|
|
|
not_implemented
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
|
2015-05-29 11:39:47 -05:00
|
|
|
def absolute_base_url
|
2016-08-11 22:43:57 -05:00
|
|
|
not_implemented
|
2015-05-29 11:39:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def relative_base_url
|
2016-08-11 22:43:57 -05:00
|
|
|
not_implemented
|
2014-10-15 12:20:04 -05:00
|
|
|
end
|
|
|
|
|
2019-11-17 19:25:42 -06:00
|
|
|
def s3_upload_host
|
|
|
|
not_implemented
|
|
|
|
end
|
|
|
|
|
2013-11-05 12:04:47 -06:00
|
|
|
def external?
|
2016-08-11 22:43:57 -05:00
|
|
|
not_implemented
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def internal?
|
2015-05-29 11:39:47 -05:00
|
|
|
!external?
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def path_for(upload)
|
2016-08-11 22:43:57 -05:00
|
|
|
not_implemented
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
|
2018-11-26 13:24:51 -06:00
|
|
|
def list_missing_uploads(skip_optimized: false)
|
|
|
|
not_implemented
|
|
|
|
end
|
|
|
|
|
2023-05-11 04:27:27 -05:00
|
|
|
# TODO: Remove when #download becomes the canonical safe version.
|
|
|
|
def download_safe(*, **)
|
|
|
|
download(*, **, print_deprecation: false)
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def download!(*, **)
|
|
|
|
download(*, **, print_deprecation: false)
|
|
|
|
rescue StandardError
|
|
|
|
raise DownloadError
|
|
|
|
end
|
|
|
|
|
|
|
|
def download(object, max_file_size_kb: nil, print_deprecation: true)
|
2020-07-06 10:01:29 -05:00
|
|
|
DistributedMutex.synchronize("download_#{object.sha1}", validity: 3.minutes) do
|
|
|
|
extension =
|
|
|
|
File.extname(
|
|
|
|
object.respond_to?(:original_filename) ? object.original_filename : object.url,
|
|
|
|
)
|
|
|
|
filename = "#{object.sha1}#{extension}"
|
2015-06-01 04:13:56 -05:00
|
|
|
file = get_from_cache(filename)
|
|
|
|
|
|
|
|
if !file
|
2020-06-11 07:47:59 -05:00
|
|
|
max_file_size_kb ||= [
|
|
|
|
SiteSetting.max_image_size_kb,
|
|
|
|
SiteSetting.max_attachment_size_kb,
|
|
|
|
].max.kilobytes
|
2019-11-17 19:25:42 -06:00
|
|
|
|
2020-07-06 10:01:29 -05:00
|
|
|
secure = object.respond_to?(:secure) ? object.secure? : object.upload.secure?
|
|
|
|
url =
|
2023-01-09 06:10:19 -06:00
|
|
|
(
|
2020-07-06 10:01:29 -05:00
|
|
|
if secure
|
|
|
|
Discourse.store.signed_url_for_path(object.url)
|
2023-01-09 06:10:19 -06:00
|
|
|
else
|
2020-07-06 10:01:29 -05:00
|
|
|
Discourse.store.cdn_url(object.url)
|
2023-01-09 06:10:19 -06:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2023-01-20 12:52:49 -06:00
|
|
|
url = SiteSetting.scheme + ":" + url if url =~ %r{\A//}
|
2017-05-24 12:42:52 -05:00
|
|
|
file =
|
|
|
|
FileHelper.download(
|
|
|
|
url,
|
|
|
|
max_file_size: max_file_size_kb,
|
|
|
|
tmp_file_name: "discourse-download",
|
|
|
|
follow_redirect: true,
|
|
|
|
)
|
2020-07-06 10:01:29 -05:00
|
|
|
|
2022-10-06 07:44:53 -05:00
|
|
|
return nil if file.nil?
|
|
|
|
|
2015-06-01 04:13:56 -05:00
|
|
|
cache_file(file, filename)
|
2019-05-17 05:26:08 -05:00
|
|
|
file = get_from_cache(filename)
|
2015-06-01 04:13:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
file
|
|
|
|
end
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
|
2013-11-27 15:01:41 -06:00
|
|
|
def purge_tombstone(grace_period)
|
|
|
|
end
|
|
|
|
|
2015-05-27 18:03:24 -05:00
|
|
|
def get_path_for(type, id, sha, extension)
|
2015-10-16 16:08:16 -05:00
|
|
|
depth = get_depth_for(id)
|
2018-12-26 10:34:49 -06:00
|
|
|
tree = File.join(*sha[0, depth].chars, "")
|
2015-05-27 18:03:24 -05:00
|
|
|
"#{type}/#{depth + 1}X/#{tree}#{sha}#{extension}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_path_for_upload(upload)
|
2021-05-27 10:42:25 -05:00
|
|
|
# try to extract the path from the URL instead of calculating it,
|
|
|
|
# because the calculated path might differ from the actual path
|
|
|
|
if upload.url.present? && (path = upload.url[UPLOAD_PATH_REGEX, 1])
|
|
|
|
return prefix_path(path)
|
|
|
|
end
|
|
|
|
|
2017-08-02 21:56:55 -05:00
|
|
|
extension =
|
|
|
|
if upload.extension
|
|
|
|
".#{upload.extension}"
|
|
|
|
else
|
2018-12-26 10:34:49 -06:00
|
|
|
# Maintain backward compatibility before Jobs::MigrateUploadExtensions runs
|
2017-08-02 21:56:55 -05:00
|
|
|
File.extname(upload.original_filename)
|
|
|
|
end
|
|
|
|
|
2020-04-30 01:48:34 -05:00
|
|
|
get_path_for("original", upload.id, upload.sha1, extension)
|
2015-05-27 18:03:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_path_for_optimized_image(optimized_image)
|
2021-05-27 10:42:25 -05:00
|
|
|
# try to extract the path from the URL instead of calculating it,
|
|
|
|
# because the calculated path might differ from the actual path
|
|
|
|
if optimized_image.url.present? && (path = optimized_image.url[OPTIMIZED_IMAGE_PATH_REGEX, 1])
|
|
|
|
return prefix_path(path)
|
|
|
|
end
|
|
|
|
|
2015-05-27 18:03:24 -05:00
|
|
|
upload = optimized_image.upload
|
2019-01-11 07:05:38 -06:00
|
|
|
version = optimized_image.version || 1
|
|
|
|
extension =
|
|
|
|
"_#{version}_#{optimized_image.width}x#{optimized_image.height}#{optimized_image.extension}"
|
2020-04-30 01:48:34 -05:00
|
|
|
get_path_for("optimized", upload.id, upload.sha1, extension)
|
2015-05-27 18:03:24 -05:00
|
|
|
end
|
|
|
|
|
2015-06-01 04:13:56 -05:00
|
|
|
CACHE_DIR ||= "#{Rails.root}/tmp/download_cache/"
|
|
|
|
CACHE_MAXIMUM_SIZE ||= 500
|
|
|
|
|
|
|
|
def get_cache_path_for(filename)
|
|
|
|
"#{CACHE_DIR}#{filename}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_from_cache(filename)
|
|
|
|
path = get_cache_path_for(filename)
|
2022-01-05 11:45:08 -06:00
|
|
|
File.open(path) if File.exist?(path)
|
2015-06-01 04:13:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache_file(file, filename)
|
|
|
|
path = get_cache_path_for(filename)
|
|
|
|
dir = File.dirname(path)
|
2018-07-04 11:18:39 -05:00
|
|
|
FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
|
2015-06-01 04:13:56 -05:00
|
|
|
FileUtils.cp(file.path, path)
|
2020-01-26 19:59:54 -06:00
|
|
|
|
2020-04-28 10:24:04 -05:00
|
|
|
# Remove all but CACHE_MAXIMUM_SIZE most recent files
|
|
|
|
files = Dir.glob("#{CACHE_DIR}*")
|
2020-05-31 23:07:07 -05:00
|
|
|
files.sort_by! do |f|
|
2020-05-31 07:35:08 -05:00
|
|
|
begin
|
2020-05-31 23:07:07 -05:00
|
|
|
File.mtime(f)
|
2020-05-31 07:35:08 -05:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
Time.new(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
files.pop(CACHE_MAXIMUM_SIZE)
|
2020-04-28 10:24:04 -05:00
|
|
|
|
|
|
|
FileUtils.rm(files, force: true)
|
2015-06-01 04:13:56 -05:00
|
|
|
end
|
|
|
|
|
2016-08-11 22:43:57 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def not_implemented
|
|
|
|
raise "Not implemented."
|
|
|
|
end
|
|
|
|
|
2019-01-02 01:29:17 -06:00
|
|
|
def get_depth_for(id)
|
|
|
|
depths = [0]
|
|
|
|
depths << Math.log(id / 1_000.0, 16).ceil if id.positive?
|
|
|
|
depths.max
|
|
|
|
end
|
|
|
|
|
2021-05-27 10:42:25 -05:00
|
|
|
def prefix_path(path)
|
|
|
|
path
|
|
|
|
end
|
2013-11-05 12:04:47 -06:00
|
|
|
end
|
|
|
|
end
|