Skip to content
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

Merged
merged 6 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions components/IscnRegisterForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
83 changes: 82 additions & 1 deletion components/IscnUploadForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -629,7 +636,7 @@
}
this.epubMetadataList.push(epubMetadata)
} catch (err) {
console.error(err)

Check warning on line 639 in components/IscnUploadForm.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 18)

Unexpected console statement
}
}

Expand Down Expand Up @@ -759,7 +766,7 @@
// eslint-disable-next-line no-console
console.error(err);
} finally {
this.uploadStatus = '';
this.uploadStatus = 'uploading';
}
return '';
}
Expand Down Expand Up @@ -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;
}
}
Copy link
Member

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 async

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return hasPDF;
}


addToEpubMetadataList(ipfsHash: string, arweaveId: string) {
this.epubMetadataList.push({
thumbnailIpfsHash: ipfsHash,
thumbnailArweaveId: arweaveId,
})
}
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean if any thumbnailArweaveId exists for this.epubMetadataList we should return? If yes use .find

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer simple for loop if iterable is not async

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer explicit if block if we are calling a function in the fallback case

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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);
Expand All @@ -837,6 +913,11 @@

try {
this.uploadStatus = 'uploading';

if (this.checkUploadFileTypeIsPDF()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the function does not simply check is PDF, but PDF and not EPUB, may be split 2 logic?
Sth like this.checkUploadFileTypeContainsPDF() && !this.checkUploadFileTypeContainsEPUB() (actually simple .find would do, we might not even need a function`

Copy link
Collaborator Author

@AuroraHuang22 AuroraHuang22 Apr 9, 2024

Choose a reason for hiding this comment

The 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;
Expand Down
Loading