mirror of
https://github.com/strapi/strapi.git
synced 2025-09-20 14:00:48 +00:00
Add backwards compatibility to the assets transfer
This commit is contained in:
parent
c57b411703
commit
b7bd31aa74
@ -155,8 +155,12 @@ class LocalFileSourceProvider implements ISourceProvider {
|
|||||||
const { path: filePath, size = 0 } = entry;
|
const { path: filePath, size = 0 } = entry;
|
||||||
const normalizedPath = unknownPathToPosix(filePath);
|
const normalizedPath = unknownPathToPosix(filePath);
|
||||||
const file = path.basename(normalizedPath);
|
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 = {
|
const asset: IAsset = {
|
||||||
metadata,
|
metadata,
|
||||||
filename: file,
|
filename: file,
|
||||||
|
@ -234,6 +234,7 @@ class LocalStrapiDestinationProvider implements IDestinationProvider {
|
|||||||
const removeAssetsBackup = this.#removeAssetsBackup.bind(this);
|
const removeAssetsBackup = this.#removeAssetsBackup.bind(this);
|
||||||
const strapi = this.strapi;
|
const strapi = this.strapi;
|
||||||
const transaction = this.transaction;
|
const transaction = this.transaction;
|
||||||
|
const backupDirectory = this.uploadsBackupDirectoryName;
|
||||||
return new Writable({
|
return new Writable({
|
||||||
objectMode: true,
|
objectMode: true,
|
||||||
async final(next) {
|
async final(next) {
|
||||||
@ -243,6 +244,39 @@ class LocalStrapiDestinationProvider implements IDestinationProvider {
|
|||||||
},
|
},
|
||||||
async write(chunk: IAsset, _encoding, callback) {
|
async write(chunk: IAsset, _encoding, callback) {
|
||||||
await transaction?.attach(async () => {
|
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 = {
|
const uploadData = {
|
||||||
...chunk.metadata,
|
...chunk.metadata,
|
||||||
stream: Readable.from(chunk.stream),
|
stream: Readable.from(chunk.stream),
|
||||||
|
@ -154,7 +154,7 @@ export interface IAsset {
|
|||||||
stream: Readable;
|
stream: Readable;
|
||||||
stats: IAssetStats;
|
stats: IAssetStats;
|
||||||
buffer?: Buffer;
|
buffer?: Buffer;
|
||||||
metadata: IFile;
|
metadata?: IFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IAssetStats {
|
export interface IAssetStats {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user