Skip to content

Commit

Permalink
✨ Add epubMetadata to thumbnail field
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraHuang22 committed Nov 1, 2023
1 parent 342923f commit 6614425
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 76 deletions.
4 changes: 3 additions & 1 deletion components/IscnRegisterForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,8 @@ export default class IscnRegisterForm extends Vue {
likerIdsAddresses: this.likerIdsAddresses,
authorDescriptions: this.authorDescriptions,
contentFingerprints: this.customContentFingerprints,
inLanguage: this.language,
thumbnail: this.epubMetadata.thumbnail,
}
}
Expand Down Expand Up @@ -1084,11 +1086,11 @@ export default class IscnRegisterForm extends Vue {
}
if (this.epubMetadata) {
console.log('epubMetadata', 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
if (this.author.name) { this.authors.push(this.author) }
}
Expand Down
188 changes: 113 additions & 75 deletions components/IscnUploadForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ import ePub from 'epubjs';
import { OfflineSigner } from '@cosmjs/proto-signing'
import { IS_CHAIN_UPGRADING, UPLOAD_FILESIZE_MAX } from '~/constant'
import { IS_CHAIN_UPGRADING, UPLOAD_FILESIZE_MAX, IPFS_VIEW_GATEWAY_URL } from '~/constant'
import { logTrackerEvent } from '~/utils/logger'
import { estimateBundlrFilePrice, uploadSingleFileToBundlr } from '~/utils/arweave/v2'
import {
Expand Down Expand Up @@ -485,7 +485,7 @@ export default class UploadForm extends Vue {
}
if (file.type === 'application/epub+zip') {
// eslint-disable-next-line no-await-in-loop
await this.processEPub({ buffer:fileBytes })
await this.processEPub({ buffer: fileBytes, file })
}
}
} else {
Expand All @@ -496,83 +496,111 @@ export default class UploadForm extends Vue {
}
}
// eslint-disable-next-line class-methods-use-this
async processEPub({ buffer }: { buffer: ArrayBuffer }) {
try {
const Book = ePub(buffer);
await Book.ready;
const epubMetadata: any = {};
// Get metadata
const { metadata } = Book.packaging;
if (metadata) {
epubMetadata.title = metadata.title;
epubMetadata.author = metadata.creator;
epubMetadata.language = metadata.language;
epubMetadata.description = metadata.description;
}
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 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
}
// 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,
}
const blobData = await Book.archive.getBlob(coverUrl);
if (blobData) {
const coverFile = new File([blobData], `${metadata.title}_cover.jpeg`, {
type: 'image/jpeg',
});
epubMetadata.thumbnail = {
"@type": "ImageObject",
url: `${IPFS_VIEW_GATEWAY_URL}${ipfsHash}`,
name: `${file.name}_cover`,
description: `${file.name}_cover`,
encodingFormat: "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,
fileData: coverFile,
ipfsHash,
fileSHA256,
isFileImage: !!imageType,
};
epubMetadata.fileSHA256 = fileSHA256;
this.epubMetadataList.push(epubMetadata);
this.fileRecords.push(fileRecord);
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)
}
}
} else {
this.epubMetadataList.push(epubMetadata);
} catch (err) {
console.error(err)
}
} 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 @@ -595,7 +623,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 @@ -633,6 +667,10 @@ export default class UploadForm extends Vue {
}
if (arweaveId) {
this.sentArweaveTransactionHashes.set(ipfsHash, { transactionHash: '', arweaveId });
const metadata = this.epubMetadataList.find((data: any) => data.thumbnail?.url === ipfsHash)
if(metadata){
metadata.thumbnail.contentUrl = `https://arweave.net/${arweaveId}`
}
}
if (!this.arweaveFeeTargetAddress) {
this.arweaveFeeTargetAddress = address;
Expand Down Expand Up @@ -709,8 +747,8 @@ export default class UploadForm extends Vue {
const uploadedData = this.sentArweaveTransactionHashes.get(records.ipfsHash) || {};
this.sentArweaveTransactionHashes.set(records.ipfsHash, { ...uploadedData, arweaveId });
if (tempRecord.fileName === 'cover.jpeg') {
const metadata = this.epubMetadataList.find((file: any) => file.ipfsHash === records.ipfsHash)
metadata.arweaveId = arweaveId
const metadata = this.epubMetadataList.find((file: any) => file.ipfsHash === records.thumbnail.url)
metadata.thumbnail.contentUrl = `https://arweave.net/${arweaveId}`
}
this.$emit('arweaveUploaded', { arweaveId })
this.isOpenSignDialog = false
Expand Down
2 changes: 2 additions & 0 deletions utils/cosmos/iscn/iscn.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface ISCNRegisterPayload {
stakeholders?: any[];
recordNotes?: string;
memo?: string;
inLanguage?: string;
thumbnail?: any;
}
export interface ISCNRecordWithID extends ISCNRecord {
id: string;
Expand Down

0 comments on commit 6614425

Please sign in to comment.