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