mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 23:58:31 -05:00
storage engines refactor
This commit is contained in:
parent
0483f05154
commit
5a143c0c6e
6 changed files with 93 additions and 132 deletions
|
@ -3,33 +3,50 @@ module FileStore
|
||||||
class BaseStore
|
class BaseStore
|
||||||
|
|
||||||
def store_upload(file, upload, content_type = nil)
|
def store_upload(file, upload, content_type = nil)
|
||||||
|
path = get_path_for_upload(upload)
|
||||||
|
store_file(file, path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def store_optimized_image(file, optimized_image)
|
def store_optimized_image(file, optimized_image)
|
||||||
|
path = get_path_for_optimized_image(optimized_image)
|
||||||
|
store_file(file, path)
|
||||||
|
end
|
||||||
|
|
||||||
|
def store_file(file, path, opts = {})
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_upload(upload)
|
def remove_upload(upload)
|
||||||
|
remove_file(upload.url)
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_optimized_image(optimized_image)
|
def remove_optimized_image(optimized_image)
|
||||||
|
remove_file(optimized_image.url)
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_file(url)
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_been_uploaded?(url)
|
def has_been_uploaded?(url)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def download_url(upload)
|
||||||
|
end
|
||||||
|
|
||||||
|
def cdn_url(url)
|
||||||
|
url
|
||||||
|
end
|
||||||
|
|
||||||
def absolute_base_url
|
def absolute_base_url
|
||||||
end
|
end
|
||||||
|
|
||||||
def relative_base_url
|
def relative_base_url
|
||||||
end
|
end
|
||||||
|
|
||||||
def download_url(upload)
|
|
||||||
end
|
|
||||||
|
|
||||||
def external?
|
def external?
|
||||||
end
|
end
|
||||||
|
|
||||||
def internal?
|
def internal?
|
||||||
|
!external?
|
||||||
end
|
end
|
||||||
|
|
||||||
def path_for(upload)
|
def path_for(upload)
|
||||||
|
@ -38,16 +55,9 @@ module FileStore
|
||||||
def download(upload)
|
def download(upload)
|
||||||
end
|
end
|
||||||
|
|
||||||
def avatar_template(avatar)
|
|
||||||
end
|
|
||||||
|
|
||||||
def purge_tombstone(grace_period)
|
def purge_tombstone(grace_period)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cdn_url(url)
|
|
||||||
url
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_path_for(type, id, sha, extension)
|
def get_path_for(type, id, sha, extension)
|
||||||
depth = [0, Math.log(id / 1_000.0, 16).ceil].max
|
depth = [0, Math.log(id / 1_000.0, 16).ceil].max
|
||||||
tree = File.join(*sha[0, depth].split(""), "")
|
tree = File.join(*sha[0, depth].split(""), "")
|
||||||
|
|
|
@ -4,22 +4,19 @@ module FileStore
|
||||||
|
|
||||||
class LocalStore < BaseStore
|
class LocalStore < BaseStore
|
||||||
|
|
||||||
def store_upload(file, upload, content_type = nil)
|
def store_file(file, path)
|
||||||
path = get_path_for_upload(upload)
|
copy_file(file, "#{public_dir}#{path}")
|
||||||
store_file(file, path)
|
"#{Discourse.base_uri}#{path}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def store_optimized_image(file, optimized_image)
|
def remove_file(url)
|
||||||
path = get_path_for_optimized_image(optimized_image)
|
return unless is_relative?(url)
|
||||||
store_file(file, path)
|
path = public_dir + url
|
||||||
end
|
tombstone = public_dir + url.gsub("/uploads/", "/tombstone/")
|
||||||
|
FileUtils.mkdir_p(Pathname.new(tombstone).dirname)
|
||||||
def remove_upload(upload)
|
FileUtils.move(path, tombstone)
|
||||||
remove_file(upload.url)
|
rescue Errno::ENOENT
|
||||||
end
|
# don't care if the file isn't there
|
||||||
|
|
||||||
def remove_optimized_image(optimized_image)
|
|
||||||
remove_file(optimized_image.url)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_been_uploaded?(url)
|
def has_been_uploaded?(url)
|
||||||
|
@ -34,19 +31,15 @@ module FileStore
|
||||||
"/uploads/#{RailsMultisite::ConnectionManagement.current_db}"
|
"/uploads/#{RailsMultisite::ConnectionManagement.current_db}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def external?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
def download_url(upload)
|
def download_url(upload)
|
||||||
return unless upload
|
return unless upload
|
||||||
"#{relative_base_url}/#{upload.sha1}"
|
"#{relative_base_url}/#{upload.sha1}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def external?
|
|
||||||
!internal?
|
|
||||||
end
|
|
||||||
|
|
||||||
def internal?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def path_for(upload)
|
def path_for(upload)
|
||||||
"#{public_dir}#{upload.url}"
|
"#{public_dir}#{upload.url}"
|
||||||
end
|
end
|
||||||
|
@ -55,19 +48,10 @@ module FileStore
|
||||||
`find #{tombstone_dir} -mtime +#{grace_period} -type f -delete`
|
`find #{tombstone_dir} -mtime +#{grace_period} -type f -delete`
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def get_path_for(type, upload_id, sha, extension)
|
def get_path_for(type, upload_id, sha, extension)
|
||||||
"#{relative_base_url}/#{super(type, upload_id, sha, extension)}"
|
"#{relative_base_url}/#{super(type, upload_id, sha, extension)}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def store_file(file, path)
|
|
||||||
# copy the file to the right location
|
|
||||||
copy_file(file, "#{public_dir}#{path}")
|
|
||||||
# url
|
|
||||||
"#{Discourse.base_uri}#{path}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def copy_file(file, path)
|
def copy_file(file, path)
|
||||||
FileUtils.mkdir_p(Pathname.new(path).dirname)
|
FileUtils.mkdir_p(Pathname.new(path).dirname)
|
||||||
# move the file to the right location
|
# move the file to the right location
|
||||||
|
@ -75,16 +59,6 @@ module FileStore
|
||||||
File.open(path, "wb") { |f| f.write(file.read) }
|
File.open(path, "wb") { |f| f.write(file.read) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_file(url)
|
|
||||||
return unless is_relative?(url)
|
|
||||||
path = public_dir + url
|
|
||||||
tombstone = public_dir + url.gsub("/uploads/", "/tombstone/")
|
|
||||||
FileUtils.mkdir_p(Pathname.new(tombstone).dirname)
|
|
||||||
FileUtils.move(path, tombstone)
|
|
||||||
rescue Errno::ENOENT
|
|
||||||
# don't care if the file isn't there
|
|
||||||
end
|
|
||||||
|
|
||||||
def is_relative?(url)
|
def is_relative?(url)
|
||||||
url.present? && url.start_with?(relative_base_url)
|
url.present? && url.start_with?(relative_base_url)
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,17 +18,32 @@ module FileStore
|
||||||
store_file(file, path, filename: upload.original_filename, content_type: content_type, cache_locally: true)
|
store_file(file, path, filename: upload.original_filename, content_type: content_type, cache_locally: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def store_optimized_image(file, optimized_image)
|
# options
|
||||||
path = get_path_for_optimized_image(optimized_image)
|
# - filename
|
||||||
store_file(file, path)
|
# - content_type
|
||||||
|
# - cache_locally
|
||||||
|
def store_file(file, path, opts={})
|
||||||
|
filename = opts[:filename].presence
|
||||||
|
content_type = opts[:content_type].presence
|
||||||
|
# cache file locally when needed
|
||||||
|
cache_file(file, File.basename(path)) if opts[:cache_locally]
|
||||||
|
# stored uploaded are public by default
|
||||||
|
options = { acl: "public-read" }
|
||||||
|
# add a "content disposition" header for "attachments"
|
||||||
|
options[:content_disposition] = "attachment; filename=\"#{filename}\"" if filename && !FileHelper.is_image?(filename)
|
||||||
|
# add a "content type" header when provided
|
||||||
|
options[:content_type] = content_type if content_type
|
||||||
|
# if this fails, it will throw an exception
|
||||||
|
@s3_helper.upload(file, path, options)
|
||||||
|
# return the upload url
|
||||||
|
"#{absolute_base_url}/#{path}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_upload(upload)
|
def remove_file(url)
|
||||||
remove_file(upload.url)
|
return unless has_been_uploaded?(url)
|
||||||
end
|
filename = File.basename(url)
|
||||||
|
# copy the removed file to tombstone
|
||||||
def remove_optimized_image(optimized_image)
|
@s3_helper.remove(filename, true)
|
||||||
remove_file(optimized_image.url)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_been_uploaded?(url)
|
def has_been_uploaded?(url)
|
||||||
|
@ -51,10 +66,6 @@ module FileStore
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def internal?
|
|
||||||
!external?
|
|
||||||
end
|
|
||||||
|
|
||||||
def download(upload)
|
def download(upload)
|
||||||
return unless has_been_uploaded?(upload.url)
|
return unless has_been_uploaded?(upload.url)
|
||||||
|
|
||||||
|
@ -85,41 +96,18 @@ module FileStore
|
||||||
end
|
end
|
||||||
|
|
||||||
def cdn_url(url)
|
def cdn_url(url)
|
||||||
if SiteSetting.s3_cdn_url.present?
|
return url if SiteSetting.s3_cdn_url.blank?
|
||||||
url.sub(absolute_base_url, SiteSetting.s3_cdn_url)
|
url.sub(absolute_base_url, SiteSetting.s3_cdn_url)
|
||||||
else
|
|
||||||
url
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def cache_avatar(avatar, user_id)
|
||||||
|
source = avatar.url.sub(absolute_base_url + "/", "")
|
||||||
# options
|
destination = avatar_template(avatar, user_id).sub(absolute_base_url + "/", "")
|
||||||
# - filename
|
@s3_helper.copy(source, destination)
|
||||||
# - content_type
|
|
||||||
# - cache_locally
|
|
||||||
def store_file(file, path, opts={})
|
|
||||||
filename = opts[:filename].presence
|
|
||||||
content_type = opts[:content_type].presence
|
|
||||||
# cache file locally when needed
|
|
||||||
cache_file(file, File.basename(path)) if opts[:cache_locally]
|
|
||||||
# stored uploaded are public by default
|
|
||||||
options = { acl: "public-read" }
|
|
||||||
# add a "content disposition" header for "attachments"
|
|
||||||
options[:content_disposition] = "attachment; filename=\"#{filename}\"" if filename && !FileHelper.is_image?(filename)
|
|
||||||
# add a "content type" header when provided
|
|
||||||
options[:content_type] = content_type if content_type
|
|
||||||
# if this fails, it will throw an exception
|
|
||||||
@s3_helper.upload(file, path, options)
|
|
||||||
# return the upload url
|
|
||||||
"#{absolute_base_url}/#{path}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_file(url)
|
def avatar_template(avatar, user_id)
|
||||||
return unless has_been_uploaded?(url)
|
UserAvatar.external_avatar_url(user_id, avatar.upload_id, avatar.width)
|
||||||
filename = File.basename(url)
|
|
||||||
# copy the removed file to tombstone
|
|
||||||
@s3_helper.remove(filename, true)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
CACHE_DIR ||= "#{Rails.root}/tmp/s3_cache/"
|
CACHE_DIR ||= "#{Rails.root}/tmp/s3_cache/"
|
||||||
|
@ -148,6 +136,7 @@ module FileStore
|
||||||
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?
|
||||||
@s3_bucket = SiteSetting.s3_upload_bucket.downcase
|
@s3_bucket = SiteSetting.s3_upload_bucket.downcase
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,8 +10,6 @@ describe FileStore::LocalStore do
|
||||||
|
|
||||||
let(:optimized_image) { Fabricate(:optimized_image) }
|
let(:optimized_image) { Fabricate(:optimized_image) }
|
||||||
|
|
||||||
let(:avatar) { Fabricate(:upload) }
|
|
||||||
|
|
||||||
describe ".store_upload" do
|
describe ".store_upload" do
|
||||||
|
|
||||||
it "returns a relative url" do
|
it "returns a relative url" do
|
||||||
|
|
|
@ -13,8 +13,6 @@ describe FileStore::S3Store do
|
||||||
let(:optimized_image) { Fabricate(:optimized_image) }
|
let(:optimized_image) { Fabricate(:optimized_image) }
|
||||||
let(:optimized_image_file) { file_from_fixtures("logo.png") }
|
let(:optimized_image_file) { file_from_fixtures("logo.png") }
|
||||||
|
|
||||||
let(:avatar) { Fabricate(:upload) }
|
|
||||||
|
|
||||||
before(:each) do
|
before(:each) do
|
||||||
SiteSetting.stubs(:s3_upload_bucket).returns("S3_Upload_Bucket")
|
SiteSetting.stubs(:s3_upload_bucket).returns("S3_Upload_Bucket")
|
||||||
SiteSetting.stubs(:s3_access_key_id).returns("s3_access_key_id")
|
SiteSetting.stubs(:s3_access_key_id).returns("s3_access_key_id")
|
||||||
|
|
|
@ -110,12 +110,8 @@ end
|
||||||
|
|
||||||
class FakeInternalStore
|
class FakeInternalStore
|
||||||
|
|
||||||
def internal?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def external?
|
def external?
|
||||||
!internal?
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def path_for(upload)
|
def path_for(upload)
|
||||||
|
@ -134,10 +130,6 @@ class FakeExternalStore
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def internal?
|
|
||||||
!external?
|
|
||||||
end
|
|
||||||
|
|
||||||
def store_optimized_image(file, optimized_image)
|
def store_optimized_image(file, optimized_image)
|
||||||
"/externally/stored/optimized/image#{optimized_image.extension}"
|
"/externally/stored/optimized/image#{optimized_image.extension}"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue