Skip to content

Commit

Permalink
🔀 Merge #408 to deploy/rinkeby
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraHuang22 committed Nov 3, 2023
2 parents bfc65c3 + 45d8c5e commit dc4dd5d
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 22 deletions.
36 changes: 36 additions & 0 deletions components/IscnRegisterForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@
:placeholder="$t('IscnRegisterForm.placeholder.url')"
/>
</FormField>
<FormField
v-if="type === 'Book'"
:label="$t('IscnRegisterForm.label.isbn')"
>
<TextField
v-model="isbn"
:placeholder="$t('IscnRegisterForm.placeholder.isbn')"
/>
</FormField>
<Divider class="my-[12px]" />
<FormField
:label="$t('IscnRegisterForm.label.type')"
Expand Down Expand Up @@ -594,6 +603,7 @@
<SameAsFieldList
:name="name"
:url-options="contentFingerprintLinks"
:file-records="fileRecords"
:current-list="sameAsList"
@onConfirm="confirmSameAsChange"
/>
Expand Down Expand Up @@ -740,6 +750,7 @@ export enum AuthorDialogType {
export default class IscnRegisterForm extends Vue {
@Prop({ default: [] }) readonly fileRecords!: any[]
@Prop({ default: [] }) readonly uploadArweaveList!: string[]
@Prop() readonly epubMetadata!: any | null
@Prop(String) readonly ipfsHash!: string
@Prop(String) readonly arweaveId!: string
Expand Down Expand Up @@ -784,8 +795,10 @@ export default class IscnRegisterForm extends Vue {
tags: string[] = []
sameAs: string[] = []
url: string = ''
isbn: string = ''
license: string = this.licenseOptions[0]
customLicense: string = ''
thumbnailUrl: string = ''
authorName: string = ''
authorUrl: string[] = []
authorWalletAddress: string[] = []
Expand Down Expand Up @@ -834,6 +847,7 @@ export default class IscnRegisterForm extends Vue {
currentAuthorDialogType: AuthorDialogType = AuthorDialogType.stakeholder
sameAsList: any = []
language: string = ''
get ipfsHashList() {
const list = []
Expand Down Expand Up @@ -987,6 +1001,7 @@ export default class IscnRegisterForm extends Vue {
tagsString: this.tagsString,
sameAs: this.formattedSameAsList,
url: this.url,
isbn: this.isbn,
exifInfo: this.exif.filter(file => file),
license: this.formattedLicense,
ipfsHash: this.ipfsHashList,
Expand All @@ -1001,6 +1016,8 @@ export default class IscnRegisterForm extends Vue {
likerIdsAddresses: this.likerIdsAddresses,
authorDescriptions: this.authorDescriptions,
contentFingerprints: this.customContentFingerprints,
inLanguage: this.language,
thumbnailUrl: this.thumbnailUrl,
}
}
Expand Down Expand Up @@ -1084,6 +1101,16 @@ export default class IscnRegisterForm extends Vue {
}
async mounted() {
if (this.epubMetadata) {
this.name = this.epubMetadata.title;
this.description = this.extractText(this.epubMetadata.description);
this.author.name = this.epubMetadata.author;
this.language = this.epubMetadata.language
this.tags = this.epubMetadata.tags
this.thumbnailUrl = this.epubMetadata.thumbnailUrl
if (this.author.name) { this.authors.push(this.author) }
}
this.uploadStatus = 'loading'
// ISCN Fee needs Arweave fee to calculate
await this.calculateISCNFee()
Expand Down Expand Up @@ -1433,5 +1460,14 @@ export default class IscnRegisterForm extends Vue {
this.displayImageSrc = this.fileRecords[index].fileData
this.displayExifInfo = this.fileRecords[index].exifInfo
}
// eslint-disable-next-line class-methods-use-this
extractText(htmlString: string) {
if (!htmlString) return ''
const div = document.createElement('div');
div.innerHTML = htmlString;
div.innerHTML = div.innerHTML.replace(/<br\s*[/]?>/gi, "\n");
return div.textContent || div.innerText;
}
}
</script>
153 changes: 142 additions & 11 deletions components/IscnUploadForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ import { namespace } from 'vuex-class'
import exifr from 'exifr'
import Hash from 'ipfs-only-hash'
import BigNumber from 'bignumber.js'
import ePub from 'epubjs';
import { OfflineSigner } from '@cosmjs/proto-signing'
Expand Down Expand Up @@ -282,8 +283,6 @@ export default class UploadForm extends Vue {
string, { transactionHash?: string, arweaveId?: string }
>()
likerId: string = ''
error: string = ''
shouldShowAlert = false
Expand All @@ -292,6 +291,8 @@ export default class UploadForm extends Vue {
signDialogError = ''
balance = new BigNumber(0)
epubMetadataList: any[] = []
get formClasses() {
return [
'flex',
Expand Down Expand Up @@ -356,7 +357,7 @@ export default class UploadForm extends Vue {
case 'MISSING_SIGNER':
return this.$t('IscnRegisterForm.error.missingSigner')
default:
return ''
return this.error
}
}
Expand Down Expand Up @@ -442,6 +443,10 @@ export default class UploadForm extends Vue {
console.error(err)
}
}
if (file.type === 'application/epub+zip') {
// eslint-disable-next-line no-await-in-loop
await this.processEPub({ buffer: fileBytes, file })
}
}
} else {
this.isSizeExceeded = true
Expand All @@ -451,6 +456,106 @@ export default class UploadForm extends Vue {
}
}
async processEPub({ buffer, file }: { buffer: ArrayBuffer; file: File }) {
try {
const Book = ePub(buffer)
await Book.ready
const epubMetadata: any = {}
// Get metadata
const { metadata } = Book.packaging
if (metadata) {
epubMetadata.epubFileName = file.name
epubMetadata.title = metadata.title
epubMetadata.author = metadata.creator
epubMetadata.language = this.formatLanguage(metadata.language)
epubMetadata.description = metadata.description
}
// Get tags
const opfFilePath = await (Book.path as any).path
const opfContent = await Book.archive.getText(opfFilePath)
const parser = new DOMParser()
const opfDocument = parser.parseFromString(opfContent, 'application/xml')
const dcSubjectElements = opfDocument.querySelectorAll(
'dc\\:subject, subject',
)
const subjects: string[] = []
dcSubjectElements.forEach((element) => {
const subject = element.textContent
subject && subjects.push(subject)
})
epubMetadata.tags = subjects
// Get cover file
const coverUrl = (Book as any).cover
if (!coverUrl) {
this.epubMetadataList.push(epubMetadata)
return
}
const blobData = await Book.archive.getBlob(coverUrl)
if (blobData) {
const coverFile = new File([blobData], `${metadata.title}_cover.jpeg`, {
type: 'image/jpeg',
})
const fileBytes = (await fileToArrayBuffer(
coverFile,
)) as unknown as ArrayBuffer
if (fileBytes) {
const [
fileSHA256,
imageType,
ipfsHash,
// eslint-disable-next-line no-await-in-loop
] = await Promise.all([
digestFileSHA256(fileBytes),
readImageType(fileBytes),
Hash.of(Buffer.from(fileBytes)),
])
const fileRecord: any = {
fileName: coverFile.name,
fileSize: coverFile.size,
fileType: coverFile.type,
fileBlob: coverFile,
ipfsHash,
fileSHA256,
isFileImage: !!imageType,
}
epubMetadata.ipfsHash = ipfsHash
const reader = new FileReader()
reader.onload = (e) => {
if (!e.target) return
fileRecord.fileData = e.target.result as string
}
reader.readAsDataURL(coverFile)
this.epubMetadataList = [
...this.epubMetadataList,
epubMetadata,
]
this.fileRecords.push(fileRecord)
}
}
} catch (err) {
console.error(err)
}
}
// eslint-disable-next-line class-methods-use-this
formatLanguage(language: string) {
if (language && language.toLowerCase().startsWith('en')) {
return 'en'
}
if (language && language.toLowerCase().startsWith('zh')) {
return 'zh'
}
return language
}
onEnterURL() {
if (
!(
Expand All @@ -472,7 +577,13 @@ export default class UploadForm extends Vue {
}
handleDeleteFile(index: number) {
this.fileRecords.splice(index, 1)
const deletedFile = this.fileRecords[index];
this.fileRecords.splice(index, 1);
const indexToDelete = this.epubMetadataList.findIndex(item => item.epubFileName === deletedFile.fileName);
if (indexToDelete !== -1) {
this.epubMetadataList.splice(indexToDelete, 1);
}
}
handleClickExifInfo(index: number) {
Expand Down Expand Up @@ -514,6 +625,10 @@ export default class UploadForm extends Vue {
}
if (arweaveId) {
this.sentArweaveTransactionInfo.set(ipfsHash, { transactionHash: '', arweaveId });
const metadata = this.epubMetadataList.find((data: any) => data.ipfsHash === ipfsHash)
if (metadata) {
metadata.thumbnailUrl = `ar://${arweaveId}`;
}
}
if (!this.arweaveFeeTargetAddress) {
this.arweaveFeeTargetAddress = address;
Expand Down Expand Up @@ -589,19 +704,21 @@ export default class UploadForm extends Vue {
if (arweaveId) {
const uploadedData = this.sentArweaveTransactionInfo.get(records.ipfsHash) || {};
this.sentArweaveTransactionInfo.set(records.ipfsHash, { ...uploadedData, arweaveId });
if (tempRecord.fileName.includes('cover.jpeg')) {
const metadata = this.epubMetadataList.find((file: any) => file.ipfsHash === records.ipfsHash)
metadata.thumbnailUrl = `ar://${arweaveId}`
}
this.$emit('arweaveUploaded', { arweaveId })
this.isOpenSignDialog = false
} else {
this.shouldShowAlert = true
this.errorMessage = this.$t('IscnRegisterForm.error.arweave') as string
this.$emit('handleContinue')
this.isOpenWarningSnackbar = true
this.error = this.$t('IscnRegisterForm.error.arweave') as string
throw new Error(this.error)
}
} catch (err) {
// TODO: Handle error
// eslint-disable-next-line no-console
console.error(err)
this.shouldShowAlert = true
this.errorMessage = (err as Error).toString()
throw new Error(err as string)
}
}
Expand Down Expand Up @@ -637,12 +754,26 @@ export default class UploadForm extends Vue {
} catch (error) {
// eslint-disable-next-line no-console
console.error(error)
this.isOpenWarningSnackbar = true
this.error = (error as Error).toString()
this.uploadStatus = '';
return
} finally {
this.uploadStatus = '';
}
const uploadArweaveIdList = Array.from(this.sentArweaveTransactionInfo.values()).map(entry => entry.arweaveId);
this.$emit('submit', { fileRecords: this.fileRecords, arweaveIds: uploadArweaveIdList })
this.fileRecords.forEach((record: any, index:number) => {
if (this.sentArweaveTransactionInfo.has(record.ipfsHash)) {
const arweaveId = this.sentArweaveTransactionInfo.get(
record.ipfsHash,
)?.arweaveId
if (arweaveId) {
this.fileRecords[index].arweaveId = `ar://${arweaveId}`
}
}
})
this.$emit('submit', { fileRecords: this.fileRecords, arweaveIds: uploadArweaveIdList, epubMetadata: this.epubMetadataList[0] })
}
handleSignDialogClose() {
Expand Down
Loading

0 comments on commit dc4dd5d

Please sign in to comment.