2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-06-01 17:13:56 +08:00
|
|
|
require_dependency '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
|
|
|
|
2016-08-15 11:21:24 +08:00
|
|
|
def remove_file(url, _)
|
2015-05-30 00:39:47 +08:00
|
|
|
return unless is_relative?(url)
|
2016-06-30 22:55:01 +08:00
|
|
|
source = "#{public_dir}#{url}"
|
|
|
|
return unless File.exists?(source)
|
|
|
|
destination = "#{public_dir}#{url.sub("/uploads/", "/uploads/tombstone/")}"
|
|
|
|
dir = Pathname.new(destination).dirname
|
|
|
|
FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
|
2019-04-24 16:44:32 +08:00
|
|
|
FileUtils.remove(destination) if File.exists?(destination)
|
2016-06-30 22:55:01 +08:00
|
|
|
FileUtils.move(source, destination, force: true)
|
2018-09-18 10:48:29 +08:00
|
|
|
FileUtils.touch(destination)
|
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)
|
2016-06-30 22:55:01 +08:00
|
|
|
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
|
|
|
|
2015-06-01 23:49:58 +08:00
|
|
|
def absolute_base_cdn_url
|
|
|
|
"#{Discourse.asset_host}#{relative_base_url}"
|
|
|
|
end
|
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def relative_base_url
|
2018-11-29 12:19:56 +08:00
|
|
|
File.join(Discourse.base_uri, upload_path)
|
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 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
|
2018-11-29 12:11:48 +08:00
|
|
|
File.join(relative_base_url, upload.sha1)
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2016-06-30 22:55:01 +08:00
|
|
|
def cdn_url(url)
|
2019-02-21 02:24:38 +08:00
|
|
|
UrlHelper.local_cdn_url(url)
|
2016-06-30 22:55:01 +08:00
|
|
|
end
|
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def path_for(upload)
|
2015-06-01 23:49:58 +08:00
|
|
|
url = upload.try(:url)
|
|
|
|
"#{public_dir}#{upload.url}" if url && url[0] == "/" && url[1] != "/"
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-14 04:08:29 +08:00
|
|
|
|
2013-11-28 05:01:41 +08:00
|
|
|
def purge_tombstone(grace_period)
|
2017-04-07 15:50:17 +08:00
|
|
|
if Dir.exists?(Discourse.store.tombstone_dir)
|
|
|
|
Discourse::Utils.execute_command(
|
|
|
|
'find', tombstone_dir, '-mtime', "+#{grace_period}", '-type', 'f', '-delete'
|
|
|
|
)
|
|
|
|
end
|
2013-11-28 05:01:41 +08:00
|
|
|
end
|
|
|
|
|
2015-05-28 07:03:24 +08:00
|
|
|
def get_path_for(type, upload_id, sha, extension)
|
2018-11-29 12:11:48 +08:00
|
|
|
File.join("/", upload_path, 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)
|
2016-06-30 22:55:01 +08:00
|
|
|
dir = Pathname.new(path).dirname
|
|
|
|
FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
|
2013-11-06 02:04:47 +08:00
|
|
|
# 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 public_dir
|
2018-11-29 12:11:48 +08:00
|
|
|
File.join(Rails.root, "public")
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2013-11-28 05:01:41 +08:00
|
|
|
def tombstone_dir
|
2016-06-30 22:55:01 +08:00
|
|
|
"#{public_dir}#{relative_base_url.sub("/uploads/", "/uploads/tombstone/")}"
|
2013-11-28 05:01:41 +08:00
|
|
|
end
|
|
|
|
|
2018-11-27 03:24:51 +08:00
|
|
|
def list_missing_uploads(skip_optimized: false)
|
|
|
|
list_missing(Upload)
|
|
|
|
list_missing(OptimizedImage) unless skip_optimized
|
|
|
|
end
|
|
|
|
|
2020-01-13 07:12:27 +08:00
|
|
|
def copy_from(source_path)
|
|
|
|
FileUtils.mkdir_p(File.join(public_dir, upload_path))
|
|
|
|
|
|
|
|
Discourse::Utils.execute_command(
|
|
|
|
'rsync', '-a', '--safe-links', "#{source_path}/", "#{upload_path}/",
|
|
|
|
failure_message: "Failed to copy uploads.",
|
|
|
|
chdir: public_dir
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-11-27 03:24:51 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def list_missing(model)
|
|
|
|
count = 0
|
|
|
|
model.find_each do |upload|
|
2013-08-01 05:26:34 +08:00
|
|
|
|
2018-11-27 03:24:51 +08:00
|
|
|
# could be a remote image
|
|
|
|
next unless upload.url =~ /^\/[^\/]/
|
|
|
|
|
2018-11-29 12:11:48 +08:00
|
|
|
path = "#{public_dir}#{upload.url}"
|
2018-11-27 03:24:51 +08:00
|
|
|
bad = true
|
|
|
|
begin
|
|
|
|
bad = false if File.size(path) != 0
|
|
|
|
rescue
|
|
|
|
# something is messed up
|
|
|
|
end
|
|
|
|
if bad
|
|
|
|
count += 1
|
|
|
|
puts path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
puts "#{count} of #{model.count} #{model.name.underscore.pluralize} are missing" if count > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-08-01 05:26:34 +08:00
|
|
|
end
|