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

View File

@ -13,7 +13,7 @@ class OptimizedImage < ActiveRecord::Base
# do we already have that thumbnail? # do we already have that thumbnail?
thumbnail = find_by(upload_id: upload.id, width: width, height: height) 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? if thumbnail && thumbnail.url.blank?
thumbnail.destroy thumbnail.destroy
thumbnail = nil thumbnail = nil

View File

@ -104,7 +104,7 @@ module FileStore
dir = File.dirname(path) dir = File.dirname(path)
FileUtils.mkdir_p(dir) unless Dir[dir].present? FileUtils.mkdir_p(dir) unless Dir[dir].present?
FileUtils.cp(file.path, path) 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` `ls -tr #{CACHE_DIR} | head -n +#{CACHE_MAXIMUM_SIZE} | xargs rm -f`
end end

View File

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

View File

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