mirror of
https://github.com/strapi/strapi.git
synced 2025-09-22 06:50:51 +00:00
Add media export capabilities for strapi source & fil file destination provider
This commit is contained in:
parent
1f5ff646b9
commit
a60467889d
@ -237,21 +237,38 @@ class LocalFileDestinationProvider implements IDestinationProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getMediaStream(): NodeJS.WritableStream {
|
getMediaStream(): NodeJS.WritableStream {
|
||||||
return chain([
|
const { stream: archiveStream } = this.#archive;
|
||||||
(data) => {
|
|
||||||
console.log(data.file);
|
if (!archiveStream) {
|
||||||
return data;
|
throw new Error('Archive stream is unavailable');
|
||||||
},
|
}
|
||||||
(data) => {
|
|
||||||
const fsStream = fs.createWriteStream(path.join(this.options.file.path, data.file));
|
return new Writable({
|
||||||
data.stream.pipe(fsStream);
|
objectMode: true,
|
||||||
fsStream.on('close', () => {
|
write(data, _encoding, callback) {
|
||||||
console.log('closed', data.file);
|
const entryPath = path.join('media', 'uploads', data.file);
|
||||||
data.stream.destroy();
|
|
||||||
|
const entry = archiveStream.entry({
|
||||||
|
name: entryPath,
|
||||||
|
size: data.stats.size,
|
||||||
});
|
});
|
||||||
return data;
|
|
||||||
|
if (!entry) {
|
||||||
|
callback(new Error(`Failed to created a tar entry for ${entryPath}`));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
data.stream.pipe(entry);
|
||||||
|
|
||||||
|
entry
|
||||||
|
.on('finish', () => {
|
||||||
|
callback(null);
|
||||||
|
})
|
||||||
|
.on('error', (error) => {
|
||||||
|
callback(error);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
]);
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,15 +10,17 @@ const IGNORED_FILES = ['.gitkeep'];
|
|||||||
export const createMediaStream = (strapi: Strapi.Strapi): Duplex => {
|
export const createMediaStream = (strapi: Strapi.Strapi): Duplex => {
|
||||||
const mediaDirectory = path.join(strapi.dirs.static.public, 'uploads');
|
const mediaDirectory = path.join(strapi.dirs.static.public, 'uploads');
|
||||||
|
|
||||||
return Duplex.from(function* () {
|
return Duplex.from(
|
||||||
const files = fs.readdirSync(mediaDirectory).filter((file) => !IGNORED_FILES.includes(file));
|
(function* () {
|
||||||
|
const files = fs.readdirSync(mediaDirectory).filter((file) => !IGNORED_FILES.includes(file));
|
||||||
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const filePath = path.join(mediaDirectory, file);
|
const filePath = path.join(mediaDirectory, file);
|
||||||
const stats = fs.statSync(filePath);
|
const stats = fs.statSync(filePath);
|
||||||
const stream = fs.createReadStream(filePath);
|
const stream = fs.createReadStream(filePath);
|
||||||
|
|
||||||
yield { file, path: filePath, stats, stream };
|
yield { file, path: filePath, stats, stream };
|
||||||
}
|
}
|
||||||
});
|
})()
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user