mirror of
https://github.com/strapi/strapi.git
synced 2025-09-26 17:00:55 +00:00
fix(logo-customization): fix bad use of upload providers
This commit is contained in:
parent
9ce78034ef
commit
3432eeade1
@ -1,6 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { unlinkSync } = require('fs');
|
|
||||||
const {
|
const {
|
||||||
parseFilesData,
|
parseFilesData,
|
||||||
getProjectSettings,
|
getProjectSettings,
|
||||||
@ -10,18 +9,24 @@ const {
|
|||||||
|
|
||||||
jest.mock('fs', () => ({
|
jest.mock('fs', () => ({
|
||||||
createReadStream: () => null,
|
createReadStream: () => null,
|
||||||
unlinkSync: jest.fn(),
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const storeSet = jest.fn();
|
const storeSet = jest.fn();
|
||||||
|
const providerDelete = jest.fn();
|
||||||
global.strapi = {
|
global.strapi = {
|
||||||
dirs: {
|
dirs: {
|
||||||
public: 'publicDir',
|
public: 'publicDir',
|
||||||
},
|
},
|
||||||
|
config: {
|
||||||
|
get: () => ({ provider: 'local' }),
|
||||||
|
},
|
||||||
plugins: {
|
plugins: {
|
||||||
upload: {
|
upload: {
|
||||||
provider: {
|
provider: {
|
||||||
uploadStream: jest.fn(),
|
async uploadStream(file) {
|
||||||
|
file.url = `/uploads/${file.hash}`;
|
||||||
|
},
|
||||||
|
delete: providerDelete,
|
||||||
},
|
},
|
||||||
services: {
|
services: {
|
||||||
upload: {
|
upload: {
|
||||||
@ -45,12 +50,12 @@ global.strapi = {
|
|||||||
get: () => ({
|
get: () => ({
|
||||||
menuLogo: {
|
menuLogo: {
|
||||||
name: 'name',
|
name: 'name',
|
||||||
path: 'path/to/file',
|
|
||||||
url: 'file/url',
|
url: 'file/url',
|
||||||
width: 100,
|
width: 100,
|
||||||
height: 100,
|
height: 100,
|
||||||
ext: 'png',
|
ext: 'png',
|
||||||
size: 123,
|
size: 123,
|
||||||
|
provider: 'local',
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
set: storeSet,
|
set: storeSet,
|
||||||
@ -79,13 +84,12 @@ describe('Project setting', () => {
|
|||||||
hash: 'filename_123',
|
hash: 'filename_123',
|
||||||
ext: '.png',
|
ext: '.png',
|
||||||
mime: 'image/png',
|
mime: 'image/png',
|
||||||
|
provider: 'local',
|
||||||
size: 123,
|
size: 123,
|
||||||
stream: null,
|
stream: null,
|
||||||
width: 100,
|
width: 100,
|
||||||
height: 100,
|
height: 100,
|
||||||
path: 'publicDir/uploads/filename_123.png',
|
|
||||||
tmpPath: '/tmp/filename_123',
|
tmpPath: '/tmp/filename_123',
|
||||||
url: '/uploads/filename_123.png',
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -131,24 +135,25 @@ describe('Project setting', () => {
|
|||||||
const newSettings = {
|
const newSettings = {
|
||||||
menuLogo: {
|
menuLogo: {
|
||||||
size: 24085,
|
size: 24085,
|
||||||
path: '/tmp/filename_123',
|
|
||||||
name: 'file.png',
|
name: 'file.png',
|
||||||
type: 'image/png',
|
type: 'image/png',
|
||||||
|
url: 'file/url',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
await deleteOldFiles({ previousSettings, newSettings });
|
await deleteOldFiles({ previousSettings, newSettings });
|
||||||
|
|
||||||
expect(unlinkSync).not.toBeCalled();
|
expect(providerDelete).not.toBeCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Does not delete when there is no new file uploaded', async () => {
|
it('Does not delete when there is no new file uploaded', async () => {
|
||||||
const previousSettings = {
|
const previousSettings = {
|
||||||
menuLogo: {
|
menuLogo: {
|
||||||
size: 24085,
|
size: 24085,
|
||||||
path: '/tmp/filename_123',
|
|
||||||
name: 'file.png',
|
name: 'file.png',
|
||||||
type: 'image/png',
|
type: 'image/png',
|
||||||
|
provider: 'local',
|
||||||
|
url: 'file/url',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -156,16 +161,17 @@ describe('Project setting', () => {
|
|||||||
|
|
||||||
await deleteOldFiles({ previousSettings, newSettings });
|
await deleteOldFiles({ previousSettings, newSettings });
|
||||||
|
|
||||||
expect(unlinkSync).not.toBeCalled();
|
expect(providerDelete).not.toBeCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Deletes when inputs are explicitely set to null', async () => {
|
it('Deletes when inputs are explicitely set to null', async () => {
|
||||||
const previousSettings = {
|
const previousSettings = {
|
||||||
menuLogo: {
|
menuLogo: {
|
||||||
size: 24085,
|
size: 24085,
|
||||||
path: '/tmp/filename_123',
|
|
||||||
name: 'file.png',
|
name: 'file.png',
|
||||||
type: 'image/png',
|
type: 'image/png',
|
||||||
|
provider: 'local',
|
||||||
|
url: 'file/url',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -173,31 +179,30 @@ describe('Project setting', () => {
|
|||||||
|
|
||||||
await deleteOldFiles({ previousSettings, newSettings });
|
await deleteOldFiles({ previousSettings, newSettings });
|
||||||
|
|
||||||
expect(unlinkSync).toBeCalledTimes(1);
|
expect(providerDelete).toBeCalledTimes(1);
|
||||||
expect(unlinkSync).toBeCalledWith(previousSettings.menuLogo.path);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Deletes when new files are uploaded', async () => {
|
it('Deletes when new files are uploaded', async () => {
|
||||||
const previousSettings = {
|
const previousSettings = {
|
||||||
menuLogo: {
|
menuLogo: {
|
||||||
size: 24085,
|
size: 24085,
|
||||||
path: '/tmp/filename_123',
|
|
||||||
name: 'file.png',
|
name: 'file.png',
|
||||||
type: 'image/png',
|
type: 'image/png',
|
||||||
|
provider: 'local',
|
||||||
|
url: 'file/url',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const newSettings = {
|
const newSettings = {
|
||||||
menuLogo: {
|
menuLogo: {
|
||||||
...previousSettings.menuLogo,
|
...previousSettings.menuLogo,
|
||||||
path: 'another/path',
|
url: 'file/new',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
await deleteOldFiles({ previousSettings, newSettings });
|
await deleteOldFiles({ previousSettings, newSettings });
|
||||||
|
|
||||||
expect(unlinkSync).toBeCalledTimes(1);
|
expect(providerDelete).toBeCalledTimes(1);
|
||||||
expect(unlinkSync).toBeCalledWith(previousSettings.menuLogo.path);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -216,7 +221,6 @@ describe('Project setting', () => {
|
|||||||
stream: null,
|
stream: null,
|
||||||
width: 100,
|
width: 100,
|
||||||
height: 100,
|
height: 100,
|
||||||
path: 'publicDir/uploads/filename_123.png',
|
|
||||||
tmpPath: '/tmp/filename_123',
|
tmpPath: '/tmp/filename_123',
|
||||||
url: '/uploads/filename_123.png',
|
url: '/uploads/filename_123.png',
|
||||||
},
|
},
|
||||||
@ -225,11 +229,11 @@ describe('Project setting', () => {
|
|||||||
const expectedOutput = {
|
const expectedOutput = {
|
||||||
menuLogo: {
|
menuLogo: {
|
||||||
name: 'filename.png',
|
name: 'filename.png',
|
||||||
path: 'publicDir/uploads/filename_123.png',
|
hash: 'filename_123',
|
||||||
url: '/uploads/filename_123.png',
|
url: '/uploads/filename_123',
|
||||||
width: 100,
|
width: 100,
|
||||||
height: 100,
|
height: 100,
|
||||||
ext: 'png',
|
ext: '.png',
|
||||||
size: 123,
|
size: 123,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -267,12 +271,12 @@ describe('Project setting', () => {
|
|||||||
const expectedOutput = {
|
const expectedOutput = {
|
||||||
menuLogo: {
|
menuLogo: {
|
||||||
name: 'name',
|
name: 'name',
|
||||||
path: 'path/to/file',
|
|
||||||
url: 'file/url',
|
url: 'file/url',
|
||||||
width: 100,
|
width: 100,
|
||||||
height: 100,
|
height: 100,
|
||||||
ext: 'png',
|
ext: 'png',
|
||||||
size: 123,
|
size: 123,
|
||||||
|
provider: 'local',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
|
||||||
const { pick } = require('lodash');
|
const { pick } = require('lodash');
|
||||||
|
|
||||||
const PROJECT_SETTINGS_FILE_INPUTS = ['menuLogo'];
|
const PROJECT_SETTINGS_FILE_INPUTS = ['menuLogo'];
|
||||||
@ -37,13 +36,11 @@ const parseFilesData = async files => {
|
|||||||
.getDimensions({ getStream })
|
.getDimensions({ getStream })
|
||||||
);
|
);
|
||||||
|
|
||||||
// Add file path, url and stream
|
// Add file path, and stream
|
||||||
const url = `/uploads/${formatedFilesData[inputName].hash}${formatedFilesData[inputName].ext}`;
|
|
||||||
Object.assign(formatedFilesData[inputName], {
|
Object.assign(formatedFilesData[inputName], {
|
||||||
path: path.join(strapi.dirs.public, url),
|
|
||||||
stream: getStream(),
|
stream: getStream(),
|
||||||
tmpPath: files[inputName].path,
|
tmpPath: files[inputName].path,
|
||||||
url,
|
provider: strapi.config.get('plugin.upload').provider,
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@ -87,14 +84,19 @@ const deleteOldFiles = async ({ previousSettings, newSettings }) => {
|
|||||||
// Skip if the file was not changed
|
// Skip if the file was not changed
|
||||||
if (
|
if (
|
||||||
newSettings[inputName] &&
|
newSettings[inputName] &&
|
||||||
previousSettings[inputName].path === newSettings[inputName].path
|
previousSettings[inputName].url === newSettings[inputName].url
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skip if the file was not uploaded with the current provider
|
||||||
|
if (strapi.config.get('plugin.upload').provider !== previousSettings[inputName].provider) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// There was a previous file and an new file was uploaded
|
// There was a previous file and an new file was uploaded
|
||||||
// Remove the previous file
|
// Remove the previous file
|
||||||
fs.unlinkSync(previousSettings[inputName].path);
|
strapi.plugin('upload').provider.delete(previousSettings[inputName]);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@ -103,6 +105,8 @@ const updateProjectSettings = async ({ body, files }) => {
|
|||||||
const store = await strapi.store({ type: 'core', name: 'admin' });
|
const store = await strapi.store({ type: 'core', name: 'admin' });
|
||||||
const previousSettings = await store.get({ key: 'project-settings' });
|
const previousSettings = await store.get({ key: 'project-settings' });
|
||||||
|
|
||||||
|
await uploadFiles(files);
|
||||||
|
|
||||||
const newSettings = {
|
const newSettings = {
|
||||||
...body,
|
...body,
|
||||||
...files,
|
...files,
|
||||||
@ -119,18 +123,18 @@ const updateProjectSettings = async ({ body, files }) => {
|
|||||||
// Update the file
|
// Update the file
|
||||||
newSettings[inputName] = {
|
newSettings[inputName] = {
|
||||||
name: newSettings[inputName].name,
|
name: newSettings[inputName].name,
|
||||||
path: newSettings[inputName].path,
|
hash: newSettings[inputName].hash,
|
||||||
url: newSettings[inputName].url,
|
url: newSettings[inputName].url,
|
||||||
width: newSettings[inputName].width,
|
width: newSettings[inputName].width,
|
||||||
height: newSettings[inputName].height,
|
height: newSettings[inputName].height,
|
||||||
ext: newSettings[inputName].ext.replace('.', ''),
|
ext: newSettings[inputName].ext,
|
||||||
size: newSettings[inputName].size,
|
size: newSettings[inputName].size,
|
||||||
|
provider: newSettings[inputName].provider,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// No await to proceed asynchronously
|
// No await to proceed asynchronously
|
||||||
uploadFiles(files);
|
|
||||||
deleteOldFiles({ previousSettings, newSettings });
|
deleteOldFiles({ previousSettings, newSettings });
|
||||||
|
|
||||||
await store.set({
|
await store.set({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user