fix: handle chunkSize

This commit is contained in:
thecodrr
2021-10-30 13:49:41 +05:00
parent 148b88f10d
commit 36597616cc
4 changed files with 38 additions and 13 deletions

View File

@@ -28,6 +28,7 @@ export default class Attachments extends Collection {
* filename: string, * filename: string,
* type: string, * type: string,
* salt: string, * salt: string,
* chunkSize: number,
* key: {} * key: {}
* }} attachment * }} attachment
* @param {string} noteId Optional as attachments will be parsed at extraction time * @param {string} noteId Optional as attachments will be parsed at extraction time
@@ -49,8 +50,18 @@ export default class Attachments extends Collection {
return this._collection.updateItem(oldAttachment); return this._collection.updateItem(oldAttachment);
} }
const { iv, length, alg, hash, hashType, filename, salt, type, key } = const {
attachment; iv,
length,
alg,
hash,
hashType,
filename,
salt,
type,
key,
chunkSize,
} = attachment;
if ( if (
!iv || !iv ||
@@ -60,6 +71,7 @@ export default class Attachments extends Collection {
!hashType || !hashType ||
!filename || !filename ||
!salt || !salt ||
!chunkSize ||
!key !key
) { ) {
console.error("Attachment invalid:", attachment); console.error("Attachment invalid:", attachment);
@@ -76,6 +88,7 @@ export default class Attachments extends Collection {
length, length,
alg, alg,
key: encryptedKey, key: encryptedKey,
chunkSize,
metadata: { metadata: {
hash, hash,
hashType, hashType,
@@ -160,6 +173,7 @@ export default class Attachments extends Collection {
attachment.metadata.hash, attachment.metadata.hash,
key, key,
{ {
chunkSize: attachment.chunkSize,
iv: attachment.iv, iv: attachment.iv,
salt: attachment.salt, salt: attachment.salt,
length: attachment.length, length: attachment.length,
@@ -195,8 +209,11 @@ export default class Attachments extends Collection {
); );
try { try {
for (let i = 0; i < attachments.length; i++) { for (let i = 0; i < attachments.length; i++) {
const { hash } = attachments[i].metadata; const {
await this._downloadMedia(hash, { metadata: { hash },
chunkSize,
} = attachments[i];
await this._downloadMedia(hash, chunkSize, {
total: attachments.length, total: attachments.length,
current: i, current: i,
groupId: noteId, groupId: noteId,
@@ -207,9 +224,18 @@ export default class Attachments extends Collection {
} }
} }
async _downloadMedia(hash, { total, current, groupId }, notify = true) { async _downloadMedia(
hash,
chunkSize,
{ total, current, groupId },
notify = true
) {
sendAttachmentsProgressEvent("download", groupId, total, current + 1); sendAttachmentsProgressEvent("download", groupId, total, current + 1);
const isDownloaded = await this._db.fs.downloadFile(groupId, hash); const isDownloaded = await this._db.fs.downloadFile(
groupId,
hash,
chunkSize
);
if (!isDownloaded) return; if (!isDownloaded) return;
const src = await this.read(hash); const src = await this.read(hash);
@@ -281,7 +307,7 @@ export default class Attachments extends Collection {
* @private * @private
*/ */
async _getEncryptionKey() { async _getEncryptionKey() {
if (!this.key) this.key = await this._db.user.getAttachmentsKey(); this.key = await this._db.user.getAttachmentsKey();
if (!this.key) if (!this.key)
throw new Error( throw new Error(
"Failed to get user encryption key. Cannot cache attachments." "Failed to get user encryption key. Cannot cache attachments."

View File

@@ -115,10 +115,8 @@ export default class Content extends Collection {
return attachments.every((a) => a.hash !== attachment.metadata.hash); return attachments.every((a) => a.hash !== attachment.metadata.hash);
}); });
const toAdd = attachments.filter((a) => { const toAdd = attachments.filter((attachment) => {
return noteAttachments.every( return noteAttachments.every((a) => attachment.hash !== a.metadata.hash);
(attachment) => a.hash !== attachment.metadata.hash
);
}); });
for (let attachment of toDelete) { for (let attachment of toDelete) {

View File

@@ -120,7 +120,7 @@ class Tiny {
break; break;
} }
default: { default: {
if (!getDatasetAttribute(attachment, "hash")) return; if (!getDatasetAttribute(attachment, "hash")) continue;
attachments.push({ attachments.push({
hash: getDatasetAttribute(attachment, "hash"), hash: getDatasetAttribute(attachment, "hash"),
}); });

View File

@@ -8,11 +8,12 @@ export default class FileStorage {
this._queue = []; this._queue = [];
} }
async downloadFile(groupId, hash) { async downloadFile(groupId, hash, chunkSize) {
const url = `${hosts.API_HOST}/s3?name=${hash}`; const url = `${hosts.API_HOST}/s3?name=${hash}`;
const token = await this.tokenManager.getAccessToken(); const token = await this.tokenManager.getAccessToken();
const { execute, cancel } = this.fs.downloadFile(hash, { const { execute, cancel } = this.fs.downloadFile(hash, {
url, url,
chunkSize,
headers: { Authorization: `Bearer ${token}` }, headers: { Authorization: `Bearer ${token}` },
}); });
this._queue.push({ groupId, hash, cancel, type: "download" }); this._queue.push({ groupId, hash, cancel, type: "download" });