From 290c47df305edc56ac08ff1e593cc85f247b1a2c Mon Sep 17 00:00:00 2001 From: AuroraHuang22 Date: Mon, 18 Mar 2024 21:22:40 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E2=9C=A8=20Define=20book=20cover=20for=20P?= =?UTF-8?q?DF=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/IscnRegisterForm.vue | 8 ++-- components/IscnUploadForm.vue | 75 +++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/components/IscnRegisterForm.vue b/components/IscnRegisterForm.vue index e8dfb03e..d2203c6b 100644 --- a/components/IscnRegisterForm.vue +++ b/components/IscnRegisterForm.vue @@ -1067,12 +1067,12 @@ export default class IscnRegisterForm extends Vue { this.uploadStatus = 'loading' if (this.epubMetadata) { - this.name = this.epubMetadata.title + this.name = this.epubMetadata.title || '' this.description = this.extractText(this.epubMetadata.description) - this.author.name = this.epubMetadata.author + this.author.name = this.epubMetadata.author || '' this.author.authorDescription = 'Author' - this.language = this.epubMetadata.language - this.tags = this.epubMetadata.tags + this.language = this.epubMetadata.language || '' + this.tags = this.epubMetadata.tags || [] this.thumbnailUrl = this.formatArweave( this.epubMetadata.thumbnailArweaveId, ) as string diff --git a/components/IscnUploadForm.vue b/components/IscnUploadForm.vue index c8c88ac8..8040809b 100644 --- a/components/IscnUploadForm.vue +++ b/components/IscnUploadForm.vue @@ -266,6 +266,13 @@ const MODE = { EDIT: 'edit', } +const IMAGE_MIME_TYPES = [ + 'image/jpeg', + 'image/png', + 'image/gif', + 'image/webp', +] + @Component export default class IscnUploadForm extends Vue { @Prop(Number) readonly step: number | undefined @@ -813,8 +820,76 @@ 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() + const buffer = Buffer.from(arrayBuffer) + return uploadSingleFileToBundlr(buffer, { + fileSize: file.fileBlob?.size || 0, + ipfsHash: file.ipfsHash, + fileType: file.fileType, + txHash, + }) + } + + async setEbookCoverFromImages() { + if ( + this.epubMetadataList[0] && + this.epubMetadataList[0].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) || {} + const { transactionHash, arweaveId: uploadArweaveId } = existingData + if (uploadArweaveId) { + this.addToEpubMetadataList(transactionHash as string, uploadArweaveId) + return + } + // eslint-disable-next-line no-await-in-loop + const arweaveId = await this.uploadFileAndGetArweaveId( + file, + transactionHash as string, + ) + if (arweaveId) { + this.addToEpubMetadataList(file.ipfsHash, arweaveId) + return + } + return + } + } + } + async onSubmit() { if (IS_CHAIN_UPGRADING) return + if (this.checkUploadFileTypeIsPDF()) { + await this.setEbookCoverFromImages() + } logTrackerEvent(this, 'ISCNCreate', 'ClickUpload', '', 1); this.uploadStatus = 'uploading' this.error = '' From a9fc7d990650194167ee47c8f396c4125ecf6e8b Mon Sep 17 00:00:00 2001 From: AuroraHuang22 Date: Tue, 19 Mar 2024 10:55:17 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=90=9B=20Fix=20the=20missing=20txHash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/IscnUploadForm.vue | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/components/IscnUploadForm.vue b/components/IscnUploadForm.vue index 8040809b..10e1b50f 100644 --- a/components/IscnUploadForm.vue +++ b/components/IscnUploadForm.vue @@ -765,8 +765,6 @@ export default class IscnUploadForm extends Vue { // TODO: Handle error // eslint-disable-next-line no-console console.error(err); - } finally { - this.uploadStatus = ''; } return ''; } @@ -866,18 +864,22 @@ export default class IscnUploadForm extends Vue { if (IMAGE_MIME_TYPES.includes(file.fileType)) { const existingData = this.sentArweaveTransactionInfo.get(file.ipfsHash) || {} - const { transactionHash, arweaveId: uploadArweaveId } = existingData - if (uploadArweaveId) { - this.addToEpubMetadataList(transactionHash as string, uploadArweaveId) + if (existingData.arweaveId) { + this.addToEpubMetadataList(file.ipfsHash, existingData.arweaveId) return } + const transactionHash = + // eslint-disable-next-line no-await-in-loop + existingData.transactionHash || (await this.sendArweaveFeeTx(file)) // eslint-disable-next-line no-await-in-loop const arweaveId = await this.uploadFileAndGetArweaveId( file, - transactionHash as string, + transactionHash, ) + if (arweaveId) { this.addToEpubMetadataList(file.ipfsHash, arweaveId) + this.sentArweaveTransactionInfo.set(file.ipfsHash, { transactionHash, arweaveId }); return } return @@ -887,9 +889,6 @@ export default class IscnUploadForm extends Vue { async onSubmit() { if (IS_CHAIN_UPGRADING) return - if (this.checkUploadFileTypeIsPDF()) { - await this.setEbookCoverFromImages() - } logTrackerEvent(this, 'ISCNCreate', 'ClickUpload', '', 1); this.uploadStatus = 'uploading' this.error = '' @@ -912,6 +911,11 @@ export default class IscnUploadForm extends Vue { try { this.uploadStatus = 'uploading'; + + if (this.checkUploadFileTypeIsPDF()) { + await this.setEbookCoverFromImages() + } + // eslint-disable-next-line no-restricted-syntax this.numberOfSignNeeded = this.modifiedFileRecords.length; this.signProgress = 0; From 99fa57390210a7017e94ceb275b7bda54e106924 Mon Sep 17 00:00:00 2001 From: AuroraHuang22 Date: Tue, 19 Mar 2024 22:31:19 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=9A=B8=20Improve=20the=20loading=20te?= =?UTF-8?q?xt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/IscnUploadForm.vue | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/IscnUploadForm.vue b/components/IscnUploadForm.vue index 10e1b50f..aa69ba5e 100644 --- a/components/IscnUploadForm.vue +++ b/components/IscnUploadForm.vue @@ -765,6 +765,8 @@ export default class IscnUploadForm extends Vue { // TODO: Handle error // eslint-disable-next-line no-console console.error(err); + } finally { + this.uploadStatus = 'uploading'; } return ''; } From 79cd883bbdabd207ea9772326f58a5c90d271592 Mon Sep 17 00:00:00 2001 From: AuroraHuang22 Date: Tue, 9 Apr 2024 09:41:07 +0800 Subject: [PATCH 4/6] =?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 bc8ddb71d0c95fc978de84c76fa735d04663cd5d Mon Sep 17 00:00:00 2001 From: AuroraHuang22 Date: Tue, 9 Apr 2024 13:14:40 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=8E=A8=20Use=20traditional=20for=20lo?= =?UTF-8?q?op=20to=20get=20arweaveId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/IscnUploadForm.vue | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/components/IscnUploadForm.vue b/components/IscnUploadForm.vue index 5f211519..44d8fd68 100644 --- a/components/IscnUploadForm.vue +++ b/components/IscnUploadForm.vue @@ -836,8 +836,9 @@ 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) { + + for (let i = 0; i < this.fileRecords.length; i += 1) { + const file = this.fileRecords[i] if (IMAGE_MIME_TYPES.includes(file.fileType)) { const existingData = this.sentArweaveTransactionInfo.get(file.ipfsHash) || {} @@ -848,10 +849,10 @@ export default class IscnUploadForm extends Vue { }) return } - let {transactionHash} = existingData; + let { transactionHash } = existingData if (!transactionHash) { // eslint-disable-next-line no-await-in-loop - transactionHash = await this.sendArweaveFeeTx(file); + transactionHash = await this.sendArweaveFeeTx(file) } // eslint-disable-next-line no-await-in-loop const arweaveId = await this.uploadFileAndGetArweaveId( @@ -864,7 +865,10 @@ export default class IscnUploadForm extends Vue { thumbnailIpfsHash: file.ipfsHash, thumbnailArweaveId: arweaveId, }) - this.sentArweaveTransactionInfo.set(file.ipfsHash, { transactionHash, arweaveId }); + this.sentArweaveTransactionInfo.set(file.ipfsHash, { + transactionHash, + arweaveId, + }) return } return From 83190c931e846d9485fa6a28980839422c5b4961 Mon Sep 17 00:00:00 2001 From: AuroraHuang22 Date: Tue, 9 Apr 2024 15:10:13 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=8E=A8=20Switch=20from=20return=20to?= =?UTF-8?q?=20break=20in=20setEbookCoverFromImages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/IscnUploadForm.vue | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/components/IscnUploadForm.vue b/components/IscnUploadForm.vue index 44d8fd68..a0aeff3d 100644 --- a/components/IscnUploadForm.vue +++ b/components/IscnUploadForm.vue @@ -847,7 +847,7 @@ export default class IscnUploadForm extends Vue { thumbnailIpfsHash: file.ipfsHash, thumbnailArweaveId: existingData.arweaveId, }) - return + break } let { transactionHash } = existingData if (!transactionHash) { @@ -869,9 +869,8 @@ export default class IscnUploadForm extends Vue { transactionHash, arweaveId, }) - return + break } - return } } }