feat(medusa/product-export-strategy): Implement the Product export strategy (#1688)

This commit is contained in:
Adrien de Peretti
2022-06-22 23:42:31 +02:00
committed by GitHub
parent 0e34800573
commit 7b09b8c36c
26 changed files with 2017 additions and 106 deletions

View File

@@ -0,0 +1,61 @@
import { AbstractFileService } from "@medusajs/medusa"
import stream from "stream"
import { resolve } from "path"
import * as fs from "fs"
export default class LocalFileService extends AbstractFileService {
constructor({}, options) {
super({});
this.upload_dir_ = process.env.UPLOAD_DIR ?? options.upload_dir ?? "uploads/images";
if (!fs.existsSync(this.upload_dir_)) {
fs.mkdirSync(this.upload_dir_);
}
}
upload(file) {
return new Promise((resolve, reject) => {
const path = resolve(this.upload_dir_, file.originalname)
fs.writeFile(path, "", err => {
if (err) {
reject(err);
}
resolve({ url: path });
});
});
}
delete({ name }) {
return new Promise((resolve, _) => {
const path = resolve(this.upload_dir_, name)
fs.unlink(path, err => {
if (err) {
throw err;
}
resolve("file unlinked");
});
});
}
async getUploadStreamDescriptor({ name, ext }) {
const fileKey = `${name}.${ext}`
const path = resolve(this.upload_dir_, fileKey)
const isFileExists = fs.existsSync(path)
if (!isFileExists) {
await this.upload({ originalname: fileKey })
}
const pass = new stream.PassThrough()
pass.pipe(fs.createWriteStream(path))
return {
writeStream: pass,
promise: Promise.resolve(),
url: `${this.upload_dir_}/${fileKey}`,
fileKey,
}
}
}