better support for mixed content

This commit is contained in:
Régis Hanol 2015-06-01 17:49:58 +02:00
parent 67cc96f20d
commit 81a699e2b0
5 changed files with 23 additions and 25 deletions

View File

@ -74,16 +74,13 @@ class UserAvatarsController < ApplicationController
if user.uploaded_avatar && !upload
avatar_url = UserAvatar.local_avatar_url(hostname, user.username_lower, user.uploaded_avatar_id, size)
return redirect_to cdn_path(avatar_url)
elsif upload
original = Discourse.store.path_for(upload)
if Discourse.store.external? || File.exists?(original)
if optimized = get_optimized_image(upload, size)
unless optimized.local?
expires_in 1.day, public: true
return redirect_to Discourse.store.cdn_url(optimized.url)
end
image = Discourse.store.path_for(optimized)
end
elsif upload && optimized = get_optimized_image(upload, size)
if optimized.local?
optimized_path = Discourse.store.path_for(optimized)
image = optimized_path if File.exists?(optimized_path)
else
expires_in 1.day, public: true
return redirect_to Discourse.store.cdn_url(optimized.url)
end
end

View File

@ -13,7 +13,7 @@ class OptimizedImage < ActiveRecord::Base
# do we already have that thumbnail?
thumbnail = find_by(upload_id: upload.id, width: width, height: height)
# make sure the previous thumbnail has not failed
# make sure we have an url
if thumbnail && thumbnail.url.blank?
thumbnail.destroy
thumbnail = nil

View File

@ -104,7 +104,7 @@ module FileStore
dir = File.dirname(path)
FileUtils.mkdir_p(dir) unless Dir[dir].present?
FileUtils.cp(file.path, path)
# keep up to 500 files
# keep latest 500 files
`ls -tr #{CACHE_DIR} | head -n +#{CACHE_MAXIMUM_SIZE} | xargs rm -f`
end

View File

@ -12,7 +12,7 @@ module FileStore
def remove_file(url)
return unless is_relative?(url)
path = public_dir + url
tombstone = public_dir + url.gsub("/uploads/", "/tombstone/")
tombstone = public_dir + url.sub("/uploads/", "/tombstone/")
FileUtils.mkdir_p(Pathname.new(tombstone).dirname)
FileUtils.move(path, tombstone)
rescue Errno::ENOENT
@ -20,13 +20,20 @@ module FileStore
end
def has_been_uploaded?(url)
url.present? && (is_relative?(url) || is_local?(url))
return false if url.blank?
return true if is_relative?(url)
return true if is_local?(url)
false
end
def absolute_base_url
"#{Discourse.base_url_no_prefix}#{relative_base_url}"
end
def absolute_base_cdn_url
"#{Discourse.asset_host}#{relative_base_url}"
end
def relative_base_url
"/uploads/#{RailsMultisite::ConnectionManagement.current_db}"
end
@ -41,8 +48,8 @@ module FileStore
end
def path_for(upload)
return unless upload && has_been_uploaded?(upload.url)
"#{public_dir}#{upload.url}"
url = upload.try(:url)
"#{public_dir}#{upload.url}" if url && url[0] == "/" && url[1] != "/"
end
def purge_tombstone(grace_period)
@ -70,16 +77,12 @@ module FileStore
absolute_url.start_with?(absolute_base_url) || absolute_url.start_with?(absolute_base_cdn_url)
end
def absolute_base_cdn_url
"#{Discourse.asset_host}#{relative_base_url}"
end
def public_dir
"#{Rails.root}/public"
end
def tombstone_dir
public_dir + relative_base_url.gsub("/uploads/", "/tombstone/")
public_dir + relative_base_url.sub("/uploads/", "/tombstone/")
end
end

View File

@ -71,10 +71,8 @@ module FileStore
end
def path_for(upload)
url = upload.url
if url && url[0] == "/" && url[1] != "/"
FileStore::LocalStore.new.path_for(upload)
end
url = upload.try(:url)
FileStore::LocalStore.new.path_for(upload) if url && url[0] == "/" && url[1] != "/"
end
def cdn_url(url)