mirror of
https://github.com/discourse/discourse.git
synced 2025-03-24 07:45:40 +08:00
FIX: optimized images fail if source is remote and S3 is disabled
This commit is contained in:
parent
296b8b1a35
commit
56f077db69
@ -484,6 +484,3 @@ DEPENDENCIES
|
|||||||
uglifier
|
uglifier
|
||||||
unf
|
unf
|
||||||
unicorn
|
unicorn
|
||||||
|
|
||||||
BUNDLED WITH
|
|
||||||
1.10.2
|
|
||||||
|
@ -53,6 +53,19 @@ module FileStore
|
|||||||
end
|
end
|
||||||
|
|
||||||
def download(upload)
|
def download(upload)
|
||||||
|
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
|
||||||
|
url = SiteSetting.scheme + ":" + upload.url
|
||||||
|
file = FileHelper.download(url, max_file_size_kb, "discourse-download", true)
|
||||||
|
cache_file(file, filename)
|
||||||
|
end
|
||||||
|
|
||||||
|
file
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def purge_tombstone(grace_period)
|
def purge_tombstone(grace_period)
|
||||||
@ -74,6 +87,27 @@ module FileStore
|
|||||||
get_path_for("optimized".freeze, upload.id, upload.sha1, extension)
|
get_path_for("optimized".freeze, upload.id, upload.sha1, extension)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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)
|
||||||
|
FileUtils.mkdir_p(dir) unless Dir[dir].present?
|
||||||
|
FileUtils.cp(file.path, path)
|
||||||
|
# keep up to 500 files
|
||||||
|
`ls -tr #{CACHE_DIR} | head -n +#{CACHE_MAXIMUM_SIZE} | xargs rm -f`
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
require 'file_store/base_store'
|
require_dependency 'file_store/base_store'
|
||||||
|
|
||||||
module FileStore
|
module FileStore
|
||||||
|
|
||||||
@ -41,6 +41,7 @@ module FileStore
|
|||||||
end
|
end
|
||||||
|
|
||||||
def path_for(upload)
|
def path_for(upload)
|
||||||
|
return unless upload && has_been_uploaded?(upload.url)
|
||||||
"#{public_dir}#{upload.url}"
|
"#{public_dir}#{upload.url}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
require "file_store/base_store"
|
require_dependency "file_store/base_store"
|
||||||
|
require_dependency "file_store/local_store"
|
||||||
require_dependency "s3_helper"
|
require_dependency "s3_helper"
|
||||||
require_dependency "file_helper"
|
require_dependency "file_helper"
|
||||||
require_dependency "file_store/local_store"
|
|
||||||
|
|
||||||
module FileStore
|
module FileStore
|
||||||
|
|
||||||
@ -66,24 +66,6 @@ module FileStore
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def download(upload)
|
|
||||||
return unless has_been_uploaded?(upload.url)
|
|
||||||
|
|
||||||
DistributedMutex.synchronize("s3_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
|
|
||||||
url = SiteSetting.scheme + ":" + upload.url
|
|
||||||
file = FileHelper.download(url, max_file_size_kb, "discourse-s3", true)
|
|
||||||
cache_file(file, filename)
|
|
||||||
end
|
|
||||||
|
|
||||||
file
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def purge_tombstone(grace_period)
|
def purge_tombstone(grace_period)
|
||||||
@s3_helper.update_tombstone_lifecycle(grace_period)
|
@s3_helper.update_tombstone_lifecycle(grace_period)
|
||||||
end
|
end
|
||||||
@ -110,27 +92,6 @@ module FileStore
|
|||||||
UserAvatar.external_avatar_url(user_id, avatar.upload_id, avatar.width)
|
UserAvatar.external_avatar_url(user_id, avatar.upload_id, avatar.width)
|
||||||
end
|
end
|
||||||
|
|
||||||
CACHE_DIR ||= "#{Rails.root}/tmp/s3_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)
|
|
||||||
FileUtils.mkdir_p(dir) unless Dir[dir].present?
|
|
||||||
FileUtils.cp(file.path, path)
|
|
||||||
# keep up to 500 files
|
|
||||||
`ls -tr #{CACHE_DIR} | head -n +#{CACHE_MAXIMUM_SIZE} | xargs rm -f`
|
|
||||||
end
|
|
||||||
|
|
||||||
def s3_bucket
|
def s3_bucket
|
||||||
return @s3_bucket if @s3_bucket
|
return @s3_bucket if @s3_bucket
|
||||||
raise Discourse::SiteSettingMissing.new("s3_upload_bucket") if SiteSetting.s3_upload_bucket.blank?
|
raise Discourse::SiteSettingMissing.new("s3_upload_bucket") if SiteSetting.s3_upload_bucket.blank?
|
||||||
|
Loading…
x
Reference in New Issue
Block a user