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

View File

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

View File

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