feat(medusa): Add file size calculation for product export (#1726)
This commit is contained in:
committed by
GitHub
parent
c6dc9086cf
commit
fb7abbf407
@@ -133,6 +133,9 @@ describe("Batch job of product-export type", () => {
|
||||
|
||||
expect(isFileExists).toBeTruthy()
|
||||
|
||||
const fileSize = (await fs.stat(exportFilePath)).size
|
||||
expect(batchJob.result?.file_size).toBe(fileSize)
|
||||
|
||||
const data = (await fs.readFile(exportFilePath)).toString()
|
||||
const [, ...lines] = data.split("\r\n").filter(l => l)
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ export class BatchJob extends SoftDeletableEntity {
|
||||
errors?: BatchJobResultError[]
|
||||
stat_descriptors?: BatchJobResultStatDescriptor[]
|
||||
file_key?: string
|
||||
file_size?: number
|
||||
} & Record<string, unknown>
|
||||
|
||||
@Column({ type: "boolean", nullable: false, default: false })
|
||||
|
||||
@@ -220,6 +220,7 @@ export default class ProductExportStrategy extends AbstractBatchJobStrategy<
|
||||
let limit = this.DEFAULT_LIMIT
|
||||
let advancementCount = 0
|
||||
let productCount = 0
|
||||
let approximateFileSize = 0
|
||||
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager) => {
|
||||
@@ -235,6 +236,8 @@ export default class ProductExportStrategy extends AbstractBatchJobStrategy<
|
||||
})
|
||||
|
||||
const header = await this.buildHeader(batchJob)
|
||||
|
||||
approximateFileSize += Buffer.from(header).byteLength
|
||||
writeStream.write(header)
|
||||
|
||||
advancementCount =
|
||||
@@ -267,7 +270,10 @@ export default class ProductExportStrategy extends AbstractBatchJobStrategy<
|
||||
|
||||
products.forEach((product: Product) => {
|
||||
const lines = this.buildProductVariantLines(product)
|
||||
lines.forEach((line) => writeStream.write(line))
|
||||
lines.forEach((line) => {
|
||||
approximateFileSize += Buffer.from(line).byteLength
|
||||
writeStream.write(line)
|
||||
})
|
||||
})
|
||||
|
||||
advancementCount += products.length
|
||||
@@ -279,6 +285,7 @@ export default class ProductExportStrategy extends AbstractBatchJobStrategy<
|
||||
.update(batchJobId, {
|
||||
result: {
|
||||
file_key: fileKey,
|
||||
file_size: approximateFileSize,
|
||||
count: productCount,
|
||||
advancement_count: advancementCount,
|
||||
progress: advancementCount / productCount,
|
||||
|
||||
Reference in New Issue
Block a user