mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: consistent and future-proof upload storage pattern
This commit is contained in:
@@ -51,10 +51,6 @@ module FileStore
|
||||
"#{public_dir}#{upload.url}"
|
||||
end
|
||||
|
||||
def avatar_template(avatar)
|
||||
relative_avatar_template(avatar)
|
||||
end
|
||||
|
||||
def purge_tombstone(grace_period)
|
||||
`find #{tombstone_dir} -mtime +#{grace_period} -type f -delete`
|
||||
end
|
||||
@@ -62,33 +58,16 @@ module FileStore
|
||||
private
|
||||
|
||||
def get_path_for_upload(file, upload)
|
||||
unique_sha1 = Digest::SHA1.hexdigest("#{Time.now}#{upload.original_filename}")[0..15]
|
||||
extension = File.extname(upload.original_filename)
|
||||
clean_name = "#{unique_sha1}#{extension}"
|
||||
# path
|
||||
"#{relative_base_url}/#{upload.id}/#{clean_name}"
|
||||
get_path_for("original".freeze, upload.sha1, upload.extension)
|
||||
end
|
||||
|
||||
def get_path_for_optimized_image(file, optimized_image)
|
||||
# 1234567890ABCDEF_100x200.jpg
|
||||
filename = [
|
||||
optimized_image.sha1[6..15],
|
||||
"_#{optimized_image.width}x#{optimized_image.height}",
|
||||
optimized_image.extension,
|
||||
].join
|
||||
# path
|
||||
"#{relative_base_url}/_optimized/#{optimized_image.sha1[0..2]}/#{optimized_image.sha1[3..5]}/#{filename}"
|
||||
extension = "_#{optimized_image.width}x#{optimized_image.height}#{optimized_image.extension}"
|
||||
get_path_for("optimized".freeze, optimized_image.sha1, extension)
|
||||
end
|
||||
|
||||
def relative_avatar_template(avatar)
|
||||
File.join(
|
||||
relative_base_url,
|
||||
"avatars",
|
||||
avatar.sha1[0..2],
|
||||
avatar.sha1[3..5],
|
||||
avatar.sha1[6..15],
|
||||
"{size}#{avatar.extension}"
|
||||
)
|
||||
def get_path_for(type, sha, extension)
|
||||
"#{relative_base_url}/#{type}/#{sha[0]}/#{sha[1]}/#{sha}#{extension}"
|
||||
end
|
||||
|
||||
def store_file(file, path)
|
||||
|
||||
Reference in New Issue
Block a user