From 79cd883bbdabd207ea9772326f58a5c90d271592 Mon Sep 17 00:00:00 2001 From: AuroraHuang22 Date: Tue, 9 Apr 2024 09:41:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=8E=A8=20Apply=20simplification=20sug?= =?UTF-8?q?gestion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/IscnUploadForm.vue | 52 +++++++++++++---------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/components/IscnUploadForm.vue b/components/IscnUploadForm.vue index aa69ba5e..5f211519 100644 --- a/components/IscnUploadForm.vue +++ b/components/IscnUploadForm.vue @@ -820,28 +820,6 @@ export default class IscnUploadForm extends Vue { } } - checkUploadFileTypeIsPDF() { - let hasPDF = false; - // eslint-disable-next-line no-restricted-syntax - for (const file of this.fileRecords) { - if (file.fileType === 'application/epub+zip') { - return false; - } - if (file.fileType === 'application/pdf') { - hasPDF = true; - } - } - return hasPDF; - } - - - addToEpubMetadataList(ipfsHash: string, arweaveId: string) { - this.epubMetadataList.push({ - thumbnailIpfsHash: ipfsHash, - thumbnailArweaveId: arweaveId, - }) - } - // eslint-disable-next-line class-methods-use-this async uploadFileAndGetArweaveId(file: any, txHash: string) { const arrayBuffer = await file.fileBlob.arrayBuffer() @@ -855,11 +833,8 @@ export default class IscnUploadForm extends Vue { } async setEbookCoverFromImages() { - if ( - this.epubMetadataList[0] && - this.epubMetadataList[0].thumbnailArweaveId - ) { - return + if (this.epubMetadataList?.find(epubMetadata => epubMetadata.thumbnailArweaveId)) { + return; } // eslint-disable-next-line no-restricted-syntax for (const file of this.fileRecords) { @@ -867,12 +842,17 @@ export default class IscnUploadForm extends Vue { const existingData = this.sentArweaveTransactionInfo.get(file.ipfsHash) || {} if (existingData.arweaveId) { - this.addToEpubMetadataList(file.ipfsHash, existingData.arweaveId) + this.epubMetadataList.push({ + thumbnailIpfsHash: file.ipfsHash, + thumbnailArweaveId: existingData.arweaveId, + }) return } - const transactionHash = - // eslint-disable-next-line no-await-in-loop - existingData.transactionHash || (await this.sendArweaveFeeTx(file)) + let {transactionHash} = existingData; + if (!transactionHash) { + // eslint-disable-next-line no-await-in-loop + transactionHash = await this.sendArweaveFeeTx(file); + } // eslint-disable-next-line no-await-in-loop const arweaveId = await this.uploadFileAndGetArweaveId( file, @@ -880,7 +860,10 @@ export default class IscnUploadForm extends Vue { ) if (arweaveId) { - this.addToEpubMetadataList(file.ipfsHash, arweaveId) + this.epubMetadataList.push({ + thumbnailIpfsHash: file.ipfsHash, + thumbnailArweaveId: arweaveId, + }) this.sentArweaveTransactionInfo.set(file.ipfsHash, { transactionHash, arweaveId }); return } @@ -914,7 +897,10 @@ export default class IscnUploadForm extends Vue { try { this.uploadStatus = 'uploading'; - if (this.checkUploadFileTypeIsPDF()) { + if ( + this.fileRecords.find((file) => file.fileType === 'application/pdf') && + !this.fileRecords.find((file) => file.fileType === 'application/epub+zip') + ) { await this.setEbookCoverFromImages() } From 9cfa8a3306d3caeb1e797fcd14042a65b704c0de Mon Sep 17 00:00:00 2001 From: AuroraHuang22 Date: Tue, 9 Apr 2024 09:57:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20Use=20promise.all=20to=20get?= =?UTF-8?q?=20arweaveId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/IscnUploadForm.vue | 62 +++++++++++++++++------------------ 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/components/IscnUploadForm.vue b/components/IscnUploadForm.vue index 5f211519..7467870c 100644 --- a/components/IscnUploadForm.vue +++ b/components/IscnUploadForm.vue @@ -836,40 +836,38 @@ export default class IscnUploadForm extends Vue { if (this.epubMetadataList?.find(epubMetadata => epubMetadata.thumbnailArweaveId)) { return; } - // eslint-disable-next-line no-restricted-syntax - for (const file of this.fileRecords) { - if (IMAGE_MIME_TYPES.includes(file.fileType)) { - const existingData = - this.sentArweaveTransactionInfo.get(file.ipfsHash) || {} - if (existingData.arweaveId) { - this.epubMetadataList.push({ - thumbnailIpfsHash: file.ipfsHash, - thumbnailArweaveId: existingData.arweaveId, - }) - return - } - let {transactionHash} = existingData; - if (!transactionHash) { - // eslint-disable-next-line no-await-in-loop - transactionHash = await this.sendArweaveFeeTx(file); - } - // eslint-disable-next-line no-await-in-loop - const arweaveId = await this.uploadFileAndGetArweaveId( - file, - transactionHash, - ) - if (arweaveId) { - this.epubMetadataList.push({ - thumbnailIpfsHash: file.ipfsHash, - thumbnailArweaveId: arweaveId, - }) - this.sentArweaveTransactionInfo.set(file.ipfsHash, { transactionHash, arweaveId }); - return - } - return + const uploadPromises = this.fileRecords + .filter(file => IMAGE_MIME_TYPES.includes(file.fileType)) + .map(async (file) => { + const existingData = this.sentArweaveTransactionInfo.get(file.ipfsHash) || {}; + + if (existingData.arweaveId) { + return { + thumbnailIpfsHash: file.ipfsHash, + thumbnailArweaveId: existingData.arweaveId, + }; } - } + + let { transactionHash } = existingData; + if (!transactionHash) { + transactionHash = await this.sendArweaveFeeTx(file); + } + + const arweaveId = await this.uploadFileAndGetArweaveId(file, transactionHash); + + if (arweaveId) { + this.sentArweaveTransactionInfo.set(file.ipfsHash, { transactionHash, arweaveId }); + return { + thumbnailIpfsHash: file.ipfsHash, + thumbnailArweaveId: arweaveId, + }; + } + + return false + }); + const results = await Promise.all(uploadPromises); + this.epubMetadataList.push(...results.filter(Boolean)); } async onSubmit() {