only make file backup when necessary

This commit is contained in:
Ben Irvin 2023-08-23 10:47:39 +02:00
parent ac99900a91
commit f8ceb56bf0
2 changed files with 17 additions and 8 deletions

View File

@ -78,7 +78,7 @@ class LocalStrapiDestinationProvider implements IDestinationProvider {
} }
} }
async #deleteAll() { async #deleteFromRestoreOptions() {
assertValidStrapi(this.strapi); assertValidStrapi(this.strapi);
return restore.deleteRecords(this.strapi, this.options.restore); return restore.deleteRecords(this.strapi, this.options.restore);
} }
@ -117,11 +117,15 @@ class LocalStrapiDestinationProvider implements IDestinationProvider {
} }
await this.transaction?.attach(async (trx) => { await this.transaction?.attach(async (trx) => {
await this.#handleAssetsBackup();
try { try {
if (this.options.strategy === 'restore') { if (this.options.strategy === 'restore') {
await this.#deleteAllAssets(trx); if (this.options.restore?.assets) {
await this.#deleteAll(); // Note: when other strategies are added, they will need to add their own backup logic
await this.#handleAssetsBackup();
await this.#deleteAllAssets(trx);
}
// TODO: do we need to add trx?
await this.#deleteFromRestoreOptions();
} }
} catch (error) { } catch (error) {
throw new Error(`restore failed ${error}`); throw new Error(`restore failed ${error}`);
@ -215,6 +219,7 @@ class LocalStrapiDestinationProvider implements IDestinationProvider {
} }
async #removeAssetsBackup() { async #removeAssetsBackup() {
// TODO: this should catch all thrown errors and bubble it up to engine so it can be reported as a non-fatal diagnostic message telling the user they may need to manually delete assets
if (strapi.config.get('plugin.upload').provider === 'local') { if (strapi.config.get('plugin.upload').provider === 'local') {
assertValidStrapi(this.strapi); assertValidStrapi(this.strapi);
const backupDirectory = path.join( const backupDirectory = path.join(
@ -233,11 +238,15 @@ class LocalStrapiDestinationProvider implements IDestinationProvider {
const strapi = this.strapi; const strapi = this.strapi;
const transaction = this.transaction; const transaction = this.transaction;
const backupDirectory = this.uploadsBackupDirectoryName; const backupDirectory = this.uploadsBackupDirectoryName;
const restoreAssets = this.options?.restore?.assets;
return new Writable({ return new Writable({
objectMode: true, objectMode: true,
async final(next) { async final(next) {
// Deletes the backup folder if (restoreAssets) {
removeAssetsBackup(); // Delete the backup folder
removeAssetsBackup();
}
next(); next();
}, },
async write(chunk: IAsset, _encoding, callback) { async write(chunk: IAsset, _encoding, callback) {

View File

@ -22,7 +22,7 @@ interface IDeleteResults {
} }
export const deleteRecords = async (strapi: Strapi.Strapi, options?: IRestoreOptions) => { export const deleteRecords = async (strapi: Strapi.Strapi, options?: IRestoreOptions) => {
const entities = await deleteEntitiesRecord(strapi, options); const entities = await deleteEntitiesRecords(strapi, options);
const configuration = await deleteConfigurationRecords(strapi, options); const configuration = await deleteConfigurationRecords(strapi, options);
return { return {
@ -32,7 +32,7 @@ export const deleteRecords = async (strapi: Strapi.Strapi, options?: IRestoreOpt
}; };
}; };
const deleteEntitiesRecord = async ( const deleteEntitiesRecords = async (
strapi: Strapi.Strapi, strapi: Strapi.Strapi,
options: IRestoreOptions = {} options: IRestoreOptions = {}
): Promise<IDeleteResults> => { ): Promise<IDeleteResults> => {