Skip to content

Commit

Permalink
✨ Add AES encrypt for ebook files
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Jan 23, 2025
1 parent 4bff741 commit 9d6e8bf
Show file tree
Hide file tree
Showing 6 changed files with 1,313 additions and 39 deletions.
31 changes: 27 additions & 4 deletions components/IscnUploadForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
</table>
</div>
</div>
<FormField v-if="hasEbookInRecords">
<CheckBox v-model="isEncryptEBookData">{{ $t('UploadForm.label.encryptEBookData') }}</CheckBox>
</FormField>
<FormField v-if="showAddISCNPageOption">
<CheckBox v-model="isAddISCNPageToEbook">{{ $t('UploadForm.label.insertISCNPage') }}</CheckBox>
</FormField>
Expand Down Expand Up @@ -244,6 +247,7 @@ import exifr from 'exifr'
import Hash from 'ipfs-only-hash'
import BigNumber from 'bignumber.js'
import ePub from 'epubjs';
import { encryptDataWithAES } from 'arweavekit/dist/lib/encryption';
import { OfflineSigner } from '@cosmjs/proto-signing'
Expand Down Expand Up @@ -310,6 +314,7 @@ export default class IscnUploadForm extends Vue {
isOpenSignDialog = false
isOpenWarningSnackbar = false
isSizeExceeded = false
isEncryptEBookData = true
isAddISCNPageToEbook = false
uploadSizeLimit: number = UPLOAD_FILESIZE_MAX
Expand Down Expand Up @@ -402,14 +407,18 @@ export default class IscnUploadForm extends Vue {
}
}
get showAddISCNPageOption() {
return this.mode === MODE.EDIT
&& this.fileRecords.some(file => [
get hasEbookInRecords() {
return this.fileRecords.some(file => [
'application/epub+zip',
'application/pdf',
].includes(file.fileType))
}
get showAddISCNPageOption() {
return this.mode === MODE.EDIT
&& this.hasEbookInRecords
}
get modifiedFileRecords() {
if (!this.isAddISCNPageToEbook || this.mode !== MODE.EDIT) return this.fileRecords
return this.fileRecords.map((record) => {
Expand Down Expand Up @@ -844,15 +853,29 @@ export default class IscnUploadForm extends Vue {
}
}
let key;
try {
const arrayBuffer = await tempRecord.fileBlob.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
let buffer = Buffer.from(arrayBuffer);
if ([
'application/epub+zip',
'application/pdf',
].includes(record.fileType)
&& this.isEncryptEBookData) {
const {
rawEncryptedKeyAsBase64,
combinedArrayBuffer,
} = await encryptDataWithAES({ data: arrayBuffer });
buffer = Buffer.from(combinedArrayBuffer);
key = rawEncryptedKeyAsBase64
}
const { arweaveId, arweaveLink } = await uploadSingleFileToBundlr(buffer, {
fileSize: tempRecord.fileBlob?.size || 0,
ipfsHash: tempRecord.ipfsHash,
fileType: tempRecord.fileType as string,
txHash: tempRecord.transactionHash,
token: this.getToken,
key,
});
if (arweaveId) {
const uploadedData = this.sentArweaveTransactionInfo.get(record.ipfsHash) || {};
Expand Down
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@
"UploadForm.button.size.limit": "(Recommended file size: < 20 MB)",
"UploadForm.button.skip": "Skip Upload",
"UploadForm.button": "Start Upload",
"UploadForm.label.encryptEBookData": "Encrypt EPUB & PDF on upload",
"UploadForm.label.insertISCNPage": "Add ISCN Page to EPUB & PDF",
"UploadForm.guide.dropFile": "Drop your file here, or",
"UploadForm.guide.selectFile": "Select your file and publish to IPFS",
Expand Down
1 change: 1 addition & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export default {
'@bundlr-network',
'@noble/curves',
'arbundle',
'arweavekit',
({ isLegacy }) => (isLegacy ? 'axios' : undefined),
],
extend(config, ctx) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@walletconnect/client": "^1.6.6",
"@walletconnect/qrcode-modal": "^1.6.6",
"@walletconnect/utils": "^1.6.6",
"arweavekit": "^1.5.1",
"axios": "^1.7.4",
"bignumber.js": "^9.0.1",
"cheerio": "^1.0.0-rc.11",
Expand Down
4 changes: 3 additions & 1 deletion utils/arweave/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ export async function uploadSingleFileToBundlr(
ipfsHash,
txHash,
token,
}: { fileSize: number; fileType?: string, ipfsHash: string; txHash: string, token: string },
key,
}: { fileSize: number; fileType?: string, ipfsHash: string; txHash: string, token: string, key?: string },
) {
const bundler = await getBundler({ fileSize, ipfsHash, txHash, token })
const tags = [
Expand All @@ -183,6 +184,7 @@ export async function uploadSingleFileToBundlr(
ipfsHash,
txHash,
arweaveId,
key,
},{
headers: { Authorization: token ? `Bearer ${token}` : '' },
});
Expand Down
Loading

0 comments on commit 9d6e8bf

Please sign in to comment.