fix(export): don't fail when trying to write a file name > 255 bytes
This commit is contained in:
parent
53b7a37fb4
commit
f615facbba
2 changed files with 23 additions and 1 deletions
|
@ -28,7 +28,7 @@ module DownloadManager
|
||||||
|
|
||||||
# can't be used with typhoeus, otherwise block is closed before the request is run by hydra
|
# 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:)
|
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)
|
attachment_dir = File.dirname(attachment_path)
|
||||||
|
|
||||||
FileUtils.mkdir_p(attachment_dir) if !Dir.exist?(attachment_dir) # defensive, do not write in undefined dir
|
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)
|
http_client.queue(request)
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -40,5 +40,16 @@ describe DownloadManager::ParallelDownloadQueue do
|
||||||
# expect(downloadable_manager.errors).to have_key(destination)
|
# expect(downloadable_manager.errors).to have_key(destination)
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue