2013-11-06 02:04:47 +08:00
|
|
|
require 'file_store/base_store'
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
module FileStore
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
class LocalStore < BaseStore
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2015-05-30 00:39:47 +08:00
|
|
|
def store_file(file, path)
|
|
|
|
copy_file(file, "#{public_dir}#{path}")
|
|
|
|
"#{Discourse.base_uri}#{path}"
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2015-05-30 00:39:47 +08:00
|
|
|
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
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def has_been_uploaded?(url)
|
2014-07-18 23:54:18 +08:00
|
|
|
url.present? && (is_relative?(url) || is_local?(url))
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def absolute_base_url
|
|
|
|
"#{Discourse.base_url_no_prefix}#{relative_base_url}"
|
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def relative_base_url
|
|
|
|
"/uploads/#{RailsMultisite::ConnectionManagement.current_db}"
|
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def external?
|
2015-05-30 00:39:47 +08:00
|
|
|
false
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2015-05-30 00:39:47 +08:00
|
|
|
def download_url(upload)
|
|
|
|
return unless upload
|
|
|
|
"#{relative_base_url}/#{upload.sha1}"
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def path_for(upload)
|
|
|
|
"#{public_dir}#{upload.url}"
|
|
|
|
end
|
2013-08-14 04:08:29 +08:00
|
|
|
|
2013-11-28 05:01:41 +08:00
|
|
|
def purge_tombstone(grace_period)
|
|
|
|
`find #{tombstone_dir} -mtime +#{grace_period} -type f -delete`
|
|
|
|
end
|
|
|
|
|
2015-05-28 07:03:24 +08:00
|
|
|
def get_path_for(type, upload_id, sha, extension)
|
|
|
|
"#{relative_base_url}/#{super(type, upload_id, sha, extension)}"
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-14 04:08:29 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def copy_file(file, path)
|
|
|
|
FileUtils.mkdir_p(Pathname.new(path).dirname)
|
|
|
|
# move the file to the right location
|
2013-11-28 05:01:41 +08:00
|
|
|
# not using mv, cause permissions are no good on move
|
|
|
|
File.open(path, "wb") { |f| f.write(file.read) }
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-14 04:08:29 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def is_relative?(url)
|
2014-07-18 23:54:18 +08:00
|
|
|
url.present? && url.start_with?(relative_base_url)
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def is_local?(url)
|
2014-07-18 23:54:18 +08:00
|
|
|
return false if url.blank?
|
2013-12-16 18:44:59 +08:00
|
|
|
absolute_url = url.start_with?("//") ? SiteSetting.scheme + ":" + url : url
|
2013-11-06 02:04:47 +08:00
|
|
|
absolute_url.start_with?(absolute_base_url) || absolute_url.start_with?(absolute_base_cdn_url)
|
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def absolute_base_cdn_url
|
|
|
|
"#{Discourse.asset_host}#{relative_base_url}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_dir
|
|
|
|
"#{Rails.root}/public"
|
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-11-28 05:01:41 +08:00
|
|
|
def tombstone_dir
|
|
|
|
public_dir + relative_base_url.gsub("/uploads/", "/tombstone/")
|
|
|
|
end
|
|
|
|
|
2013-08-01 05:26:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|