FIX: Make site tasks work with duplicated uploads (#13972)

Uploads can be reused between site settings. This change allows the same
upload to be exported only once and then the same file is reused. The
same applies to import.
This commit is contained in:
Bianca Nenciu
2021-08-18 15:57:31 +03:00
committed by GitHub
parent d13716286c
commit 4380ba34d5

View File

@ -8,6 +8,7 @@ class ZippedSiteStructure
def initialize(path, create: false) def initialize(path, create: false)
@zip = Zip::File.open(path, create) @zip = Zip::File.open(path, create)
@uploads = {}
end end
def close def close
@ -41,26 +42,53 @@ class ZippedSiteStructure
return nil return nil
end end
if @uploads[upload.id].present?
puts " - Already exported upload #{upload_or_id_or_url} to #{@uploads[upload.id][:path]}"
return @uploads[upload.id]
end
local_path = upload.local? ? Discourse.store.path_for(upload) : Discourse.store.download(upload).path local_path = upload.local? ? Discourse.store.path_for(upload) : Discourse.store.download(upload).path
zip_path = File.join('uploads', File.basename(local_path)) zip_path = File.join('uploads', File.basename(local_path))
zip_path = get_unique_path(zip_path)
puts " - Exporting upload #{upload_or_id_or_url} to #{zip_path}" puts " - Exporting upload #{upload_or_id_or_url} to #{zip_path}"
@zip.add(zip_path, local_path) @zip.add(zip_path, local_path)
{ filename: upload.original_filename, path: zip_path } @uploads[upload.id] ||= { filename: upload.original_filename, path: zip_path }
end end
def get_upload(upload, opts = {}) def get_upload(upload, opts = {})
return nil if upload.blank? return nil if upload.blank?
if @uploads[upload['path']].present?
puts " - Already imported upload #{upload['filename']} from #{upload['path']}"
return @uploads[upload['path']]
end
puts " - Importing upload #{upload['filename']} from #{upload['path']}" puts " - Importing upload #{upload['filename']} from #{upload['path']}"
tempfile = Tempfile.new(upload['filename'], binmode: true) tempfile = Tempfile.new(upload['filename'], binmode: true)
tempfile.write(@zip.get_input_stream(upload['path']).read) tempfile.write(@zip.get_input_stream(upload['path']).read)
tempfile.rewind tempfile.rewind
UploadCreator.new(tempfile, upload['filename'], opts) @uploads[upload['path']] ||= UploadCreator.new(tempfile, upload['filename'], opts).create_for(Discourse::SYSTEM_USER_ID)
.create_for(Discourse::SYSTEM_USER_ID) end
private
def get_unique_path(path)
return path if @zip.find_entry(path).blank?
extname = File.extname(path)
basename = File.basename(path, extname)
dirname = File.dirname(path)
i = 0
loop do
i += 1
path = File.join(dirname, "#{basename}_#{i}#{extname}")
return path if @zip.find_entry(path).blank?
end
end end
end end