mirror of
https://github.com/strapi/strapi.git
synced 2025-08-11 18:27:22 +00:00

This fixes the `generateFileName` function in the Strapi upload plugin, so that reserved and unsafe charactes for URLs are replaced with underscores. Signed-off-by: Marvin König <dev@mkqavi.com>
101 lines
2.5 KiB
JavaScript
101 lines
2.5 KiB
JavaScript
const uploadService = require('../Upload');
|
|
|
|
describe('Upload service', () => {
|
|
describe('formatFileInfo', () => {
|
|
test('Generates hash', () => {
|
|
const fileData = {
|
|
filename: 'File Name.png',
|
|
type: 'image/png',
|
|
size: 1000 * 1000,
|
|
};
|
|
|
|
expect(uploadService.formatFileInfo(fileData)).toMatchObject({
|
|
name: 'File Name',
|
|
hash: expect.stringContaining('File_Name'),
|
|
ext: '.png',
|
|
mime: 'image/png',
|
|
size: 1000,
|
|
});
|
|
});
|
|
|
|
test('Replaces reserved and unsafe characters for URLs and files in hash', () => {
|
|
const fileData = {
|
|
filename: 'File%&Näme\\<>:"|?*.png',
|
|
type: 'image/png',
|
|
size: 1000 * 1000,
|
|
};
|
|
|
|
expect(uploadService.formatFileInfo(fileData)).toMatchObject({
|
|
name: 'File%&Näme\\<>:"|?*',
|
|
hash: expect.stringContaining('File_and_Naeme'),
|
|
ext: '.png',
|
|
mime: 'image/png',
|
|
size: 1000,
|
|
});
|
|
});
|
|
|
|
test('Overrides name with fileInfo', () => {
|
|
const fileData = {
|
|
filename: 'File Name.png',
|
|
type: 'image/png',
|
|
size: 1000 * 1000,
|
|
};
|
|
|
|
const fileInfo = {
|
|
name: 'Custom File Name',
|
|
};
|
|
|
|
expect(uploadService.formatFileInfo(fileData, fileInfo)).toMatchObject({
|
|
name: fileInfo.name,
|
|
hash: expect.stringContaining('Custom_File_Name'),
|
|
ext: '.png',
|
|
mime: 'image/png',
|
|
size: 1000,
|
|
});
|
|
});
|
|
|
|
test('Sets alternativeText and caption', () => {
|
|
const fileData = {
|
|
filename: 'File Name.png',
|
|
type: 'image/png',
|
|
size: 1000 * 1000,
|
|
};
|
|
|
|
const fileInfo = {
|
|
alternativeText: 'some text',
|
|
caption: 'caption this',
|
|
};
|
|
|
|
expect(uploadService.formatFileInfo(fileData, fileInfo)).toMatchObject({
|
|
name: 'File Name',
|
|
caption: fileInfo.caption,
|
|
alternativeText: fileInfo.alternativeText,
|
|
hash: expect.stringContaining('File_Name'),
|
|
ext: '.png',
|
|
mime: 'image/png',
|
|
size: 1000,
|
|
});
|
|
});
|
|
|
|
test('Set a path folder', () => {
|
|
const fileData = {
|
|
filename: 'File Name.png',
|
|
type: 'image/png',
|
|
size: 1000 * 1000,
|
|
};
|
|
|
|
const fileMetas = {
|
|
path: 'folder',
|
|
};
|
|
|
|
expect(uploadService.formatFileInfo(fileData, {}, fileMetas)).toMatchObject({
|
|
name: 'File Name',
|
|
ext: '.png',
|
|
mime: 'image/png',
|
|
size: 1000,
|
|
path: expect.stringContaining('folder'),
|
|
});
|
|
});
|
|
});
|
|
});
|