feat(medusa): Add batch strategy for order exports (#1603)
This commit is contained in:
@@ -1,55 +1,63 @@
|
||||
import { AbstractFileService } from "@medusajs/medusa"
|
||||
import stream from "stream"
|
||||
import { resolve } from "path"
|
||||
import * as fs from "fs"
|
||||
import * as path from "path"
|
||||
|
||||
export default class LocalFileService extends AbstractFileService {
|
||||
// eslint-disable-next-line no-empty-pattern
|
||||
constructor({}, options) {
|
||||
super({});
|
||||
this.upload_dir_ = process.env.UPLOAD_DIR ?? options.upload_dir ?? "uploads/images";
|
||||
super({})
|
||||
this.upload_dir_ =
|
||||
process.env.UPLOAD_DIR ?? options.upload_dir ?? "uploads/images"
|
||||
|
||||
if (!fs.existsSync(this.upload_dir_)) {
|
||||
fs.mkdirSync(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);
|
||||
}
|
||||
async upload(file) {
|
||||
const uploadPath = path.join(
|
||||
this.upload_dir_,
|
||||
path.dirname(file.originalname)
|
||||
)
|
||||
|
||||
resolve({ url: path });
|
||||
});
|
||||
});
|
||||
if (!fs.existsSync(uploadPath)) {
|
||||
fs.mkdirSync(uploadPath, { recursive: true })
|
||||
}
|
||||
|
||||
const filePath = path.resolve(this.upload_dir_, file.originalname)
|
||||
fs.writeFile(filePath, "", (error) => {
|
||||
if (error) {
|
||||
throw error
|
||||
}
|
||||
})
|
||||
return { url: filePath }
|
||||
}
|
||||
|
||||
delete({ name }) {
|
||||
async delete({ name }) {
|
||||
return new Promise((resolve, _) => {
|
||||
const path = resolve(this.upload_dir_, name)
|
||||
fs.unlink(path, err => {
|
||||
fs.unlink(path, (err) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
throw err
|
||||
}
|
||||
|
||||
resolve("file unlinked");
|
||||
});
|
||||
});
|
||||
resolve("file unlinked")
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async getUploadStreamDescriptor({ name, ext }) {
|
||||
const fileKey = `${name}.${ext}`
|
||||
const path = resolve(this.upload_dir_, fileKey)
|
||||
const fileKey = `${name}-${Date.now()}.${ext}`
|
||||
const filePath = path.resolve(this.upload_dir_, fileKey)
|
||||
|
||||
const isFileExists = fs.existsSync(path)
|
||||
const isFileExists = fs.existsSync(filePath)
|
||||
if (!isFileExists) {
|
||||
await this.upload({ originalname: fileKey })
|
||||
}
|
||||
|
||||
const pass = new stream.PassThrough()
|
||||
pass.pipe(fs.createWriteStream(path))
|
||||
pass.pipe(fs.createWriteStream(filePath))
|
||||
|
||||
return {
|
||||
writeStream: pass,
|
||||
|
||||
Reference in New Issue
Block a user