fix(export): don't fail when trying to write a file name > 255 bytes

This commit is contained in:
Colin Darie 2023-05-04 12:45:58 +02:00
parent 53b7a37fb4
commit f615facbba
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
2 changed files with 23 additions and 1 deletions

View file

@ -28,7 +28,7 @@ module DownloadManager
# can't be used with typhoeus, otherwise block is closed before the request is run by hydra
def download_one(attachment:, path_in_download_dir:, http_client:)
attachment_path = File.join(destination, path_in_download_dir)
attachment_path = File.join(destination, sanitize_filename(path_in_download_dir))
attachment_dir = File.dirname(attachment_path)
FileUtils.mkdir_p(attachment_dir) if !Dir.exist?(attachment_dir) # defensive, do not write in undefined dir
@ -49,5 +49,16 @@ module DownloadManager
http_client.queue(request)
end
end
private
def sanitize_filename(filename)
return filename if filename.bytesize <= 255
ext = File.extname(filename)
basename = File.basename(filename, ext).byteslice(0, 255 - ext.bytesize)
basename + ext
end
end
end

View file

@ -40,5 +40,16 @@ describe DownloadManager::ParallelDownloadQueue do
# expect(downloadable_manager.errors).to have_key(destination)
end
end
context 'with a destination filename too long' do
let(:destination) { 'a' * 252 + '.txt' }
it 'limit the file path to 255 bytes' do
target = File.join(download_to_dir, 'a' * 251 + '.txt')
expect { subject }.to change { File.exist?(target) }
attachment.file.rewind
expect(attachment.file.read).to eq(File.read(target))
end
end
end
end