fox-send/app/ownedFile.js

80 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-01-24 18:23:13 +00:00
import Keychain from './keychain';
import { arrayToB64 } from './utils';
2018-01-30 20:15:09 +00:00
import { del, fileInfo, setParams, setPassword } from './api';
2018-01-24 18:23:13 +00:00
export default class OwnedFile {
constructor(obj, storage) {
this.id = obj.id;
this.url = obj.url;
this.name = obj.name;
this.size = obj.size;
this.type = obj.type;
this.time = obj.time;
this.speed = obj.speed;
this.createdAt = obj.createdAt;
this.expiresAt = obj.expiresAt;
this.ownerToken = obj.ownerToken;
this.dlimit = obj.dlimit || 1;
this.dtotal = obj.dtotal || 0;
this.keychain = new Keychain(obj.secretKey, obj.nonce);
this.keychain.on('nonceChanged', () => storage.writeFile(this));
2018-01-30 20:15:09 +00:00
this._hasPassword = !!obj.hasPassword;
2018-01-24 18:23:13 +00:00
}
async setPassword(password) {
this.password = password;
2018-01-30 20:15:09 +00:00
this._hasPassword = true;
2018-01-24 18:23:13 +00:00
this.keychain.setPassword(password, this.url);
const result = await setPassword(this.id, this.ownerToken, this.keychain);
return result;
}
del() {
return del(this.id, this.ownerToken);
}
changeLimit(dlimit) {
if (this.dlimit !== dlimit) {
this.dlimit = dlimit;
return setParams(this.id, this.ownerToken, { dlimit });
}
return Promise.resolve(true);
}
2018-01-30 20:15:09 +00:00
get hasPassword() {
return !!this._hasPassword;
2018-01-24 18:23:13 +00:00
}
async updateDownloadCount() {
try {
2018-01-30 20:15:09 +00:00
const result = await fileInfo(this.id, this.ownerToken);
2018-01-24 18:23:13 +00:00
this.dtotal = result.dtotal;
2018-01-30 20:15:09 +00:00
this.dlimit = result.dlimit;
2018-01-24 18:23:13 +00:00
} catch (e) {
if (e.message === '404') {
this.dtotal = this.dlimit;
}
}
}
toJSON() {
return {
id: this.id,
url: this.url,
name: this.name,
size: this.size,
type: this.type,
time: this.time,
speed: this.speed,
createdAt: this.createdAt,
expiresAt: this.expiresAt,
secretKey: arrayToB64(this.keychain.rawSecret),
nonce: this.keychain.nonce,
ownerToken: this.ownerToken,
dlimit: this.dlimit,
dtotal: this.dtotal,
2018-01-30 20:15:09 +00:00
hasPassword: this.hasPassword
2018-01-24 18:23:13 +00:00
};
}
}