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,
* type: string,
* salt: string,
* chunkSize: number,
* key: {}
* }} attachment
* @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);
}
const { iv, length, alg, hash, hashType, filename, salt, type, key } =
attachment;
const {
iv,
length,
alg,
hash,
hashType,
filename,
salt,
type,
key,
chunkSize,
} = attachment;
if (
!iv ||
@@ -60,6 +71,7 @@ export default class Attachments extends Collection {
!hashType ||
!filename ||
!salt ||
!chunkSize ||
!key
) {
console.error("Attachment invalid:", attachment);
@@ -76,6 +88,7 @@ export default class Attachments extends Collection {
length,
alg,
key: encryptedKey,
chunkSize,
metadata: {
hash,
hashType,
@@ -160,6 +173,7 @@ export default class Attachments extends Collection {
attachment.metadata.hash,
key,
{
chunkSize: attachment.chunkSize,
iv: attachment.iv,
salt: attachment.salt,
length: attachment.length,
@@ -195,8 +209,11 @@ export default class Attachments extends Collection {
);
try {
for (let i = 0; i < attachments.length; i++) {
const { hash } = attachments[i].metadata;
await this._downloadMedia(hash, {
const {
metadata: { hash },
chunkSize,
} = attachments[i];
await this._downloadMedia(hash, chunkSize, {
total: attachments.length,
current: i,
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);
const isDownloaded = await this._db.fs.downloadFile(groupId, hash);
const isDownloaded = await this._db.fs.downloadFile(
groupId,
hash,
chunkSize
);
if (!isDownloaded) return;
const src = await this.read(hash);
@@ -281,7 +307,7 @@ export default class Attachments extends Collection {
* @private
*/
async _getEncryptionKey() {
if (!this.key) this.key = await this._db.user.getAttachmentsKey();
this.key = await this._db.user.getAttachmentsKey();
if (!this.key)
throw new Error(
"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);
});
const toAdd = attachments.filter((a) => {
return noteAttachments.every(
(attachment) => a.hash !== attachment.metadata.hash
);
const toAdd = attachments.filter((attachment) => {
return noteAttachments.every((a) => attachment.hash !== a.metadata.hash);
});
for (let attachment of toDelete) {

View File

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

View File

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