feat: send metadata along with download request

This commit is contained in:
thecodrr
2021-11-18 15:17:02 +05:00
parent 9ccb0fee12
commit 2c927a5dd7
3 changed files with 37 additions and 26 deletions

View File

@@ -209,15 +209,17 @@ export default class Attachments extends Collection {
);
try {
for (let i = 0; i < attachments.length; i++) {
const {
metadata: { hash },
const { metadata, chunkSize } = attachments[i];
await this._downloadMedia(
metadata.hash,
chunkSize,
} = attachments[i];
await this._downloadMedia(hash, chunkSize, {
total: attachments.length,
current: i,
groupId: noteId,
});
{
total: attachments.length,
current: i,
groupId: noteId,
},
metadata
);
}
} finally {
sendAttachmentsProgressEvent("download", noteId, attachments.length);
@@ -225,16 +227,18 @@ export default class Attachments extends Collection {
}
async _downloadMedia(
hash,
filename,
chunkSize,
{ total, current, groupId },
metadata,
notify = true
) {
sendAttachmentsProgressEvent("download", groupId, total, current);
const isDownloaded = await this._db.fs.downloadFile(
groupId,
hash,
chunkSize
filename,
chunkSize,
metadata
);
if (!isDownloaded) return;

View File

@@ -73,13 +73,19 @@ export default class Content extends Collection {
async downloadMedia(groupId, contentItem, notify = true) {
const content = getContentFromData(contentItem.type, contentItem.data);
contentItem.data = await content.insertMedia((hash, { total, current }) => {
const attachment = this._db.attachments.attachment(hash);
if (!attachment) return;
const metadata = attachment.metadata;
const progressData = {
total,
current,
groupId,
};
return this._db.attachments._downloadMedia(
hash,
{
total,
current,
groupId,
},
metadata.hash,
progressData,
metadata,
notify
);
});

View File

@@ -8,28 +8,29 @@ export default class FileStorage {
this._queue = [];
}
async downloadFile(groupId, hash, chunkSize) {
const url = `${hosts.API_HOST}/s3?name=${hash}`;
async downloadFile(groupId, filename, chunkSize, metadata) {
const url = `${hosts.API_HOST}/s3?name=${filename}`;
const token = await this.tokenManager.getAccessToken();
const { execute, cancel } = this.fs.downloadFile(hash, {
const { execute, cancel } = this.fs.downloadFile(filename, {
metadata,
url,
chunkSize,
headers: { Authorization: `Bearer ${token}` },
});
this._queue.push({ groupId, hash, cancel, type: "download" });
this._queue.push({ groupId, filename, cancel, type: "download" });
const result = await execute();
this._deleteOp(groupId, "download");
return result;
}
async uploadFile(groupId, hash) {
async uploadFile(groupId, filename) {
const token = await this.tokenManager.getAccessToken();
const url = `${hosts.API_HOST}/s3?name=${hash}`;
const { execute, cancel } = this.fs.uploadFile(hash, {
const url = `${hosts.API_HOST}/s3?name=${filename}`;
const { execute, cancel } = this.fs.uploadFile(filename, {
url,
headers: { Authorization: `Bearer ${token}` },
});
this._queue.push({ groupId, hash, cancel, type: "upload" });
this._queue.push({ groupId, filename, cancel, type: "upload" });
const result = await execute();
this._deleteOp(groupId, "upload");
return result;
@@ -65,7 +66,7 @@ export default class FileStorage {
if (localOnly) return await this.fs.deleteFile(filename);
const token = await this.tokenManager.getToken();
const url = `${hosts.API_HOST}/s3?name=${hash}`;
const url = `${hosts.API_HOST}/s3?name=${filename}`;
return await this.fs.deleteFile(filename, {
url,
headers: { Authorization: `Bearer ${token}` },