2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
module FileStore
|
|
|
|
|
|
|
|
class BaseStore
|
|
|
|
|
2014-04-15 19:04:14 +08:00
|
|
|
def store_upload(file, upload, content_type = nil)
|
2015-05-30 00:39:47 +08:00
|
|
|
path = get_path_for_upload(upload)
|
|
|
|
store_file(file, path)
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
2020-01-16 11:50:27 +08:00
|
|
|
def store_optimized_image(file, optimized_image, content_type = nil, secure: false)
|
2015-05-30 00:39:47 +08:00
|
|
|
path = get_path_for_optimized_image(optimized_image)
|
|
|
|
store_file(file, path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_file(file, path, opts = {})
|
2016-08-12 11:43:57 +08:00
|
|
|
not_implemented
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_upload(upload)
|
2016-08-15 11:21:24 +08:00
|
|
|
remove_file(upload.url, get_path_for_upload(upload))
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_optimized_image(optimized_image)
|
2016-08-15 11:21:24 +08:00
|
|
|
remove_file(optimized_image.url, get_path_for_optimized_image(optimized_image))
|
2015-05-30 00:39:47 +08:00
|
|
|
end
|
|
|
|
|
2016-08-15 11:21:24 +08:00
|
|
|
def remove_file(url, path)
|
2016-08-12 11:43:57 +08:00
|
|
|
not_implemented
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
2018-11-29 12:11:48 +08:00
|
|
|
def upload_path
|
2019-12-18 13:51:57 +08:00
|
|
|
path = File.join("uploads", RailsMultisite::ConnectionManagement.current_db)
|
|
|
|
return path unless Discourse.is_parallel_test?
|
|
|
|
|
|
|
|
n = ENV['TEST_ENV_NUMBER'].presence || '1'
|
|
|
|
File.join(path, n)
|
2018-11-29 12:11:48 +08:00
|
|
|
end
|
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def has_been_uploaded?(url)
|
2016-08-12 11:43:57 +08:00
|
|
|
not_implemented
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
2015-05-30 00:39:47 +08:00
|
|
|
def download_url(upload)
|
2016-08-12 11:43:57 +08:00
|
|
|
not_implemented
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
2015-05-30 00:39:47 +08:00
|
|
|
def cdn_url(url)
|
2016-08-12 11:43:57 +08:00
|
|
|
not_implemented
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
2015-05-30 00:39:47 +08:00
|
|
|
def absolute_base_url
|
2016-08-12 11:43:57 +08:00
|
|
|
not_implemented
|
2015-05-30 00:39:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def relative_base_url
|
2016-08-12 11:43:57 +08:00
|
|
|
not_implemented
|
2014-10-16 01:20:04 +08:00
|
|
|
end
|
|
|
|
|
2019-11-18 09:25:42 +08:00
|
|
|
def s3_upload_host
|
|
|
|
not_implemented
|
|
|
|
end
|
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def external?
|
2016-08-12 11:43:57 +08:00
|
|
|
not_implemented
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def internal?
|
2015-05-30 00:39:47 +08:00
|
|
|
!external?
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def path_for(upload)
|
2016-08-12 11:43:57 +08:00
|
|
|
not_implemented
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
2018-11-27 03:24:51 +08:00
|
|
|
def list_missing_uploads(skip_optimized: false)
|
|
|
|
not_implemented
|
|
|
|
end
|
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def download(upload)
|
2015-06-01 17:13:56 +08:00
|
|
|
DistributedMutex.synchronize("download_#{upload.sha1}") do
|
|
|
|
filename = "#{upload.sha1}#{File.extname(upload.original_filename)}"
|
|
|
|
file = get_from_cache(filename)
|
|
|
|
|
|
|
|
if !file
|
|
|
|
max_file_size_kb = [SiteSetting.max_image_size_kb, SiteSetting.max_attachment_size_kb].max.kilobytes
|
2019-11-18 09:25:42 +08:00
|
|
|
|
|
|
|
url = upload.secure? ?
|
|
|
|
Discourse.store.signed_url_for_path(upload.url) :
|
|
|
|
Discourse.store.cdn_url(upload.url)
|
|
|
|
|
2019-06-06 21:47:19 +08:00
|
|
|
url = SiteSetting.scheme + ":" + url if url =~ /^\/\//
|
2017-05-25 01:42:52 +08:00
|
|
|
file = FileHelper.download(
|
|
|
|
url,
|
|
|
|
max_file_size: max_file_size_kb,
|
|
|
|
tmp_file_name: "discourse-download",
|
|
|
|
follow_redirect: true
|
|
|
|
)
|
2015-06-01 17:13:56 +08:00
|
|
|
cache_file(file, filename)
|
2019-05-17 18:26:08 +08:00
|
|
|
file = get_from_cache(filename)
|
2015-06-01 17:13:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
file
|
|
|
|
end
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
2013-11-28 05:01:41 +08:00
|
|
|
def purge_tombstone(grace_period)
|
|
|
|
end
|
|
|
|
|
2015-05-28 07:03:24 +08:00
|
|
|
def get_path_for(type, id, sha, extension)
|
2015-10-17 05:08:16 +08:00
|
|
|
depth = get_depth_for(id)
|
2018-12-27 00:34:49 +08:00
|
|
|
tree = File.join(*sha[0, depth].chars, "")
|
2015-05-28 07:03:24 +08:00
|
|
|
"#{type}/#{depth + 1}X/#{tree}#{sha}#{extension}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_path_for_upload(upload)
|
2017-08-03 10:56:55 +08:00
|
|
|
extension =
|
|
|
|
if upload.extension
|
|
|
|
".#{upload.extension}"
|
|
|
|
else
|
2018-12-27 00:34:49 +08:00
|
|
|
# Maintain backward compatibility before Jobs::MigrateUploadExtensions runs
|
2017-08-03 10:56:55 +08:00
|
|
|
File.extname(upload.original_filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
get_path_for("original".freeze, upload.id, upload.sha1, extension)
|
2015-05-28 07:03:24 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_path_for_optimized_image(optimized_image)
|
|
|
|
upload = optimized_image.upload
|
2019-01-11 21:05:38 +08:00
|
|
|
version = optimized_image.version || 1
|
|
|
|
extension = "_#{version}_#{optimized_image.width}x#{optimized_image.height}#{optimized_image.extension}"
|
2015-05-28 07:03:24 +08:00
|
|
|
get_path_for("optimized".freeze, upload.id, upload.sha1, extension)
|
|
|
|
end
|
|
|
|
|
2015-06-01 17:13:56 +08: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)
|
|
|
|
File.open(path) if File.exists?(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_file(file, filename)
|
|
|
|
path = get_cache_path_for(filename)
|
|
|
|
dir = File.dirname(path)
|
2018-07-05 00:18:39 +08:00
|
|
|
FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
|
2015-06-01 17:13:56 +08:00
|
|
|
FileUtils.cp(file.path, path)
|
2015-06-01 23:49:58 +08:00
|
|
|
# keep latest 500 files
|
2019-11-08 23:34:03 +08:00
|
|
|
processes = Open3.pipeline(
|
|
|
|
"ls -t #{CACHE_DIR}",
|
|
|
|
"tail -n +#{CACHE_MAXIMUM_SIZE + 1}",
|
|
|
|
"awk '$0=\"#{CACHE_DIR}\"$0'",
|
|
|
|
"xargs rm -f"
|
|
|
|
)
|
|
|
|
raise "Error clearing old cache" if !processes.all?(&:success?)
|
2015-06-01 17:13:56 +08:00
|
|
|
end
|
|
|
|
|
2016-08-12 11:43:57 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def not_implemented
|
|
|
|
raise "Not implemented."
|
|
|
|
end
|
|
|
|
|
2019-01-02 15:29:17 +08:00
|
|
|
def get_depth_for(id)
|
|
|
|
depths = [0]
|
|
|
|
depths << Math.log(id / 1_000.0, 16).ceil if id.positive?
|
|
|
|
depths.max
|
|
|
|
end
|
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|