-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TAS-1216] ✨ Define book cover for PDF file #457
Changes from 3 commits
290c47d
a9fc7d9
99fa573
79cd883
bc8ddb7
83190c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -266,6 +266,13 @@ | |
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 | ||
|
@@ -629,7 +636,7 @@ | |
} | ||
this.epubMetadataList.push(epubMetadata) | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
|
||
|
@@ -759,7 +766,7 @@ | |
// eslint-disable-next-line no-console | ||
console.error(err); | ||
} finally { | ||
this.uploadStatus = ''; | ||
this.uploadStatus = 'uploading'; | ||
} | ||
return ''; | ||
} | ||
|
@@ -813,6 +820,75 @@ | |
} | ||
} | ||
|
||
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, | ||
}) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function doesn't seems necessary if it only has one line of content There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
// 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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean if any There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
// eslint-disable-next-line no-restricted-syntax | ||
for (const file of this.fileRecords) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer simple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (IMAGE_MIME_TYPES.includes(file.fileType)) { | ||
const existingData = | ||
this.sentArweaveTransactionInfo.get(file.ipfsHash) || {} | ||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer explicit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
// eslint-disable-next-line no-await-in-loop | ||
const arweaveId = await this.uploadFileAndGetArweaveId( | ||
file, | ||
transactionHash, | ||
) | ||
|
||
if (arweaveId) { | ||
this.addToEpubMetadataList(file.ipfsHash, arweaveId) | ||
this.sentArweaveTransactionInfo.set(file.ipfsHash, { transactionHash, arweaveId }); | ||
return | ||
} | ||
return | ||
} | ||
} | ||
} | ||
|
||
async onSubmit() { | ||
if (IS_CHAIN_UPGRADING) return | ||
logTrackerEvent(this, 'ISCNCreate', 'ClickUpload', '', 1); | ||
|
@@ -837,6 +913,11 @@ | |
|
||
try { | ||
this.uploadStatus = 'uploading'; | ||
|
||
if (this.checkUploadFileTypeIsPDF()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems the function does not simply check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
await this.setEbookCoverFromImages() | ||
} | ||
|
||
// eslint-disable-next-line no-restricted-syntax | ||
this.numberOfSignNeeded = this.modifiedFileRecords.length; | ||
this.signProgress = 0; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
.find
seems to be simpler,for of
is required when the iterable is asyncThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎨 Apply simplification suggestion