mirror of
https://github.com/strapi/strapi.git
synced 2025-09-21 14:31:16 +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 {
|
||||
return chain([
|
||||
(data) => {
|
||||
console.log(data.file);
|
||||
return data;
|
||||
},
|
||||
(data) => {
|
||||
const fsStream = fs.createWriteStream(path.join(this.options.file.path, data.file));
|
||||
data.stream.pipe(fsStream);
|
||||
fsStream.on('close', () => {
|
||||
console.log('closed', data.file);
|
||||
data.stream.destroy();
|
||||
const { stream: archiveStream } = this.#archive;
|
||||
|
||||
if (!archiveStream) {
|
||||
throw new Error('Archive stream is unavailable');
|
||||
}
|
||||
|
||||
return new Writable({
|
||||
objectMode: true,
|
||||
write(data, _encoding, callback) {
|
||||
const entryPath = path.join('media', 'uploads', data.file);
|
||||
|
||||
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 => {
|
||||
const mediaDirectory = path.join(strapi.dirs.static.public, 'uploads');
|
||||
|
||||
return Duplex.from(function* () {
|
||||
const files = fs.readdirSync(mediaDirectory).filter((file) => !IGNORED_FILES.includes(file));
|
||||
return Duplex.from(
|
||||
(function* () {
|
||||
const files = fs.readdirSync(mediaDirectory).filter((file) => !IGNORED_FILES.includes(file));
|
||||
|
||||
for (const file of files) {
|
||||
const filePath = path.join(mediaDirectory, file);
|
||||
const stats = fs.statSync(filePath);
|
||||
const stream = fs.createReadStream(filePath);
|
||||
for (const file of files) {
|
||||
const filePath = path.join(mediaDirectory, file);
|
||||
const stats = fs.statSync(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