From b7bd31aa74b147c79dc64a7a76d88760fca1dfdc Mon Sep 17 00:00:00 2001 From: Christian Capeans Date: Wed, 5 Jul 2023 11:35:42 +0200 Subject: [PATCH] Add backwards compatibility to the assets transfer --- .../src/file/providers/source/index.ts | 8 +++-- .../providers/local-destination/index.ts | 34 +++++++++++++++++++ .../data-transfer/types/common-entities.d.ts | 2 +- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/core/data-transfer/src/file/providers/source/index.ts b/packages/core/data-transfer/src/file/providers/source/index.ts index 3c6f181a67..d84222aae6 100644 --- a/packages/core/data-transfer/src/file/providers/source/index.ts +++ b/packages/core/data-transfer/src/file/providers/source/index.ts @@ -155,8 +155,12 @@ class LocalFileSourceProvider implements ISourceProvider { const { path: filePath, size = 0 } = entry; const normalizedPath = unknownPathToPosix(filePath); const file = path.basename(normalizedPath); - const metadata = await loadAssetMetadata(`assets/metadata/${file}.json`); - + let metadata; + try { + metadata = await loadAssetMetadata(`assets/metadata/${file}.json`); + } catch (error) { + // do nothing + } const asset: IAsset = { metadata, filename: file, diff --git a/packages/core/data-transfer/src/strapi/providers/local-destination/index.ts b/packages/core/data-transfer/src/strapi/providers/local-destination/index.ts index fe9c69eb69..b9e05c09c1 100644 --- a/packages/core/data-transfer/src/strapi/providers/local-destination/index.ts +++ b/packages/core/data-transfer/src/strapi/providers/local-destination/index.ts @@ -234,6 +234,7 @@ class LocalStrapiDestinationProvider implements IDestinationProvider { const removeAssetsBackup = this.#removeAssetsBackup.bind(this); const strapi = this.strapi; const transaction = this.transaction; + const backupDirectory = this.uploadsBackupDirectoryName; return new Writable({ objectMode: true, async final(next) { @@ -243,6 +244,39 @@ class LocalStrapiDestinationProvider implements IDestinationProvider { }, async write(chunk: IAsset, _encoding, callback) { await transaction?.attach(async () => { + // TODO: Remove this logic in V5 + if (!chunk.metadata) { + const assetsDirectory = path.join(strapi.dirs.static.public, 'uploads'); + const entryPath = path.join(assetsDirectory, chunk.filename); + const writableStream = fse.createWriteStream(entryPath); + chunk.stream + .pipe(writableStream) + .on('close', () => { + callback(null); + }) + .on('error', async (error: NodeJS.ErrnoException) => { + const errorMessage = + error.code === 'ENOSPC' + ? " Your server doesn't have space to proceed with the import. " + : ' '; + + try { + await fse.rm(assetsDirectory, { recursive: true, force: true }); + this.destroy( + new ProviderTransferError( + `There was an error during the transfer process.${errorMessage}The original files have been restored to ${assetsDirectory}` + ) + ); + } catch (err) { + throw new ProviderTransferError( + `There was an error doing the rollback process. The original files are in ${backupDirectory}, but we failed to restore them to ${assetsDirectory}` + ); + } finally { + callback(error); + } + }); + return; + } const uploadData = { ...chunk.metadata, stream: Readable.from(chunk.stream), diff --git a/packages/core/data-transfer/types/common-entities.d.ts b/packages/core/data-transfer/types/common-entities.d.ts index a465543cd2..5879cbd98b 100644 --- a/packages/core/data-transfer/types/common-entities.d.ts +++ b/packages/core/data-transfer/types/common-entities.d.ts @@ -154,7 +154,7 @@ export interface IAsset { stream: Readable; stats: IAssetStats; buffer?: Buffer; - metadata: IFile; + metadata?: IFile; } export interface IAssetStats {