2013-11-05 13:04:47 -05:00
|
|
|
require 'file_store/base_store'
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
module FileStore
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
class LocalStore < BaseStore
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2014-04-15 07:04:14 -04:00
|
|
|
def store_upload(file, upload, content_type = nil)
|
2013-11-05 13:04:47 -05:00
|
|
|
path = get_path_for_upload(file, upload)
|
|
|
|
store_file(file, path)
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def store_optimized_image(file, optimized_image)
|
|
|
|
path = get_path_for_optimized_image(file, optimized_image)
|
|
|
|
store_file(file, path)
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def store_avatar(file, avatar, size)
|
|
|
|
path = get_path_for_avatar(file, avatar, size)
|
|
|
|
store_file(file, path)
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def remove_upload(upload)
|
|
|
|
remove_file(upload.url)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def remove_optimized_image(optimized_image)
|
|
|
|
remove_file(optimized_image.url)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def has_been_uploaded?(url)
|
2014-07-18 11:54:18 -04:00
|
|
|
url.present? && (is_relative?(url) || is_local?(url))
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def absolute_base_url
|
|
|
|
"#{Discourse.base_url_no_prefix}#{relative_base_url}"
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def relative_base_url
|
|
|
|
"/uploads/#{RailsMultisite::ConnectionManagement.current_db}"
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def external?
|
|
|
|
!internal?
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def internal?
|
|
|
|
true
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def path_for(upload)
|
|
|
|
"#{public_dir}#{upload.url}"
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2014-01-07 11:45:06 -05:00
|
|
|
def avatar_template(avatar)
|
|
|
|
relative_avatar_template(avatar)
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-27 16:01:41 -05:00
|
|
|
def purge_tombstone(grace_period)
|
|
|
|
`find #{tombstone_dir} -mtime +#{grace_period} -type f -delete`
|
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
private
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def get_path_for_upload(file, upload)
|
2014-04-14 16:55:57 -04:00
|
|
|
unique_sha1 = Digest::SHA1.hexdigest("#{Time.now.to_s}#{upload.original_filename}")[0..15]
|
|
|
|
extension = File.extname(upload.original_filename)
|
2013-11-05 13:04:47 -05:00
|
|
|
clean_name = "#{unique_sha1}#{extension}"
|
|
|
|
# path
|
|
|
|
"#{relative_base_url}/#{upload.id}/#{clean_name}"
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
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}"
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def get_path_for_avatar(file, avatar, size)
|
|
|
|
relative_avatar_template(avatar).gsub("{size}", size.to_s)
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def relative_avatar_template(avatar)
|
|
|
|
File.join(
|
2014-01-07 11:45:06 -05:00
|
|
|
relative_base_url,
|
2013-11-05 13:04:47 -05:00
|
|
|
"avatars",
|
|
|
|
avatar.sha1[0..2],
|
|
|
|
avatar.sha1[3..5],
|
|
|
|
avatar.sha1[6..15],
|
|
|
|
"{size}#{avatar.extension}"
|
|
|
|
)
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def store_file(file, path)
|
|
|
|
# copy the file to the right location
|
|
|
|
copy_file(file, "#{public_dir}#{path}")
|
|
|
|
# url
|
|
|
|
"#{Discourse.base_uri}#{path}"
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def copy_file(file, path)
|
|
|
|
FileUtils.mkdir_p(Pathname.new(path).dirname)
|
|
|
|
# move the file to the right location
|
2013-11-27 16:01:41 -05:00
|
|
|
# not using mv, cause permissions are no good on move
|
|
|
|
File.open(path, "wb") { |f| f.write(file.read) }
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def remove_file(url)
|
2013-11-27 16:01:41 -05:00
|
|
|
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)
|
2013-11-05 13:04:47 -05:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
# don't care if the file isn't there
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def is_relative?(url)
|
2014-07-18 11:54:18 -04:00
|
|
|
url.present? && url.start_with?(relative_base_url)
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def is_local?(url)
|
2014-07-18 11:54:18 -04:00
|
|
|
return false if url.blank?
|
2013-12-16 05:44:59 -05:00
|
|
|
absolute_url = url.start_with?("//") ? SiteSetting.scheme + ":" + url : url
|
2013-11-05 13:04:47 -05:00
|
|
|
absolute_url.start_with?(absolute_base_url) || absolute_url.start_with?(absolute_base_cdn_url)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def absolute_base_cdn_url
|
|
|
|
"#{Discourse.asset_host}#{relative_base_url}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_dir
|
|
|
|
"#{Rails.root}/public"
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-27 16:01:41 -05:00
|
|
|
def tombstone_dir
|
|
|
|
public_dir + relative_base_url.gsub("/uploads/", "/tombstone/")
|
|
|
|
end
|
|
|
|
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|