Skip to content

Commit a47b744

Browse files
committed
[FIX] vault: Error when try to download file
Right now the data of a bynary field is not saving the real value but is saving the size of the file. So we were trying to decrypt a string like "300 kB". With this changes, we get the crypted value from the db so it will be decrypted correctly.
1 parent 19c60a5 commit a47b744

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

vault/static/src/backend/fields/vault_export_file.esm.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,38 @@ export class VaultExportFile extends VaultMixin(BinaryField) {
1515
this.exporter = useService("vault_export");
1616
this.vault_utils = useService("vault_utils");
1717
}
18+
19+
/**
20+
* Get the value saved on db because the system is showing just the
21+
* size on the props.
22+
*
23+
* @param {Object} ev
24+
*/
25+
async _getCryptedValue() {
26+
const crypted = await this.orm.read(
27+
this.props.record.resModel,
28+
[this.props.record.resId],
29+
[this.props.name]
30+
);
31+
return crypted[0][this.props.name];
32+
}
33+
1834
/**
1935
* Call the exporter and download the finalized file
2036
*/
2137
async onFileDownload() {
22-
if (!this.props.value) {
23-
this.do_warn(
24-
_t("Save As..."),
25-
_t("The field is empty, there's nothing to save!")
26-
);
38+
const cryptedValue = await this._getCryptedValue();
39+
if (!cryptedValue) {
40+
this.notification.add(_t("The field is empty, there's nothing to save!"), {
41+
title: _t("Save As..."),
42+
type: "danger",
43+
});
2744
} else if (this.vault_utils.supported()) {
2845
const content = JSON.stringify(
2946
await this.exporter.export(
3047
await this._getMasterKey(),
31-
this.state.fileName,
32-
this.props.value
48+
this.fileName,
49+
cryptedValue
3350
)
3451
);
3552

@@ -38,7 +55,7 @@ export class VaultExportFile extends VaultMixin(BinaryField) {
3855
for (let i = 0; i < content.length; i++) arr[i] = content.charCodeAt(i);
3956

4057
const blob = new Blob([arr]);
41-
await downloadFile(blob, this.state.fileName || "");
58+
await downloadFile(blob, this.fileName || "");
4259
}
4360
}
4461
}

vault/static/src/backend/fields/vault_file.esm.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export class VaultFile extends VaultMixin(BinaryField) {
1212
static template = "vault.FileVault";
1313
setup() {
1414
super.setup();
15-
15+
this.orm = useService("orm");
16+
this.notification = useService("notification");
1617
this.action = useService("action");
1718
}
1819

@@ -21,6 +22,21 @@ export class VaultFile extends VaultMixin(BinaryField) {
2122
return await super.update({data: encrypted, name: name});
2223
}
2324

25+
/**
26+
* Get the value saved on db because the system is showing just the
27+
* size on the props.
28+
*
29+
* @param {Object} ev
30+
*/
31+
async _getCryptedValue() {
32+
const crypted = await this.orm.read(
33+
this.props.record.resModel,
34+
[this.props.record.resId],
35+
[this.props.name]
36+
);
37+
return crypted[0][this.props.name];
38+
}
39+
2440
/**
2541
* Send the secret to an inbox of an user
2642
*
@@ -29,27 +45,28 @@ export class VaultFile extends VaultMixin(BinaryField) {
2945
async _onSendValue(ev) {
3046
ev.stopPropagation();
3147

32-
await this.sendValue("", this.props.value, this.state.fileName);
48+
await this.sendValue("", await this._getCryptedValue(), this.fileName);
3349
}
3450

3551
/**
3652
* Decrypt the file and download it
3753
*/
3854
async onFileDownload() {
39-
if (!this.props.value) {
40-
this.do_warn(
41-
_t("Save As..."),
42-
_t("The field is empty, there's nothing to save!")
43-
);
55+
const cryptedValue = await this._getCryptedValue();
56+
if (!cryptedValue) {
57+
this.notification.add(_t("The field is empty, there's nothing to save!"), {
58+
title: _t("Save As..."),
59+
type: "danger",
60+
});
4461
} else if (this.vault_utils.supported()) {
45-
const decrypted = await this._decrypt(this.props.value);
62+
const decrypted = await this._decrypt(cryptedValue);
4663
const base64 = atob(decrypted);
4764
const buffer = new ArrayBuffer(base64.length);
4865
const arr = new Uint8Array(buffer);
4966
for (let i = 0; i < base64.length; i++) arr[i] = base64.charCodeAt(i);
5067

5168
const blob = new Blob([arr]);
52-
await downloadFile(blob, this.state.fileName || "");
69+
await downloadFile(blob, this.fileName || "");
5370
}
5471
}
5572
}

0 commit comments

Comments
 (0)