2018-10-07 18:31:08 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Uploads
|
|
|
|
class Local < Base
|
|
|
|
def keys(relation)
|
2018-12-04 19:48:21 +00:00
|
|
|
relation.includes(:model).find_each.map(&:absolute_path)
|
2018-10-07 18:31:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_keys(keys)
|
|
|
|
keys.each do |path|
|
|
|
|
delete_file(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def delete_file(path)
|
|
|
|
unless exists?(path)
|
|
|
|
logger.warn("File '#{path}' doesn't exist, skipping")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
unless in_uploads?(path)
|
|
|
|
message = "Path '#{path}' is not in uploads dir, skipping"
|
|
|
|
logger.warn(message)
|
2019-12-16 12:07:43 +00:00
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(
|
2019-12-13 12:07:41 +00:00
|
|
|
RuntimeError.new(message), uploads_dir: storage_dir)
|
2018-10-07 18:31:08 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
FileUtils.rm(path)
|
|
|
|
delete_dir!(File.dirname(path))
|
|
|
|
end
|
|
|
|
|
|
|
|
def exists?(path)
|
|
|
|
path.present? && File.exist?(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_uploads?(path)
|
|
|
|
path.start_with?(storage_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_dir!(path)
|
|
|
|
Dir.rmdir(path)
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
# Ignore: path does not exist
|
|
|
|
rescue Errno::ENOTDIR
|
|
|
|
# Ignore: path is not a dir
|
|
|
|
rescue Errno::ENOTEMPTY, Errno::EEXIST
|
|
|
|
# Ignore: dir is not empty
|
|
|
|
end
|
|
|
|
|
|
|
|
def storage_dir
|
|
|
|
@storage_dir ||= File.realpath(Gitlab.config.uploads.storage_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-11-01 21:10:32 +00:00
|
|
|
|
|
|
|
Uploads::Local.prepend_mod
|