2018-01-24 19:23:13 +01:00
|
|
|
import Keychain from './keychain';
|
|
|
|
import { arrayToB64 } from './utils';
|
2018-01-30 21:15:09 +01:00
|
|
|
import { del, fileInfo, setParams, setPassword } from './api';
|
2018-01-24 19:23:13 +01:00
|
|
|
|
|
|
|
export default class OwnedFile {
|
2018-01-31 20:12:36 +01:00
|
|
|
constructor(obj) {
|
2019-03-12 15:39:50 +01:00
|
|
|
if (!obj.manifest) {
|
|
|
|
throw new Error('invalid file object');
|
|
|
|
}
|
2018-01-24 19:23:13 +01:00
|
|
|
this.id = obj.id;
|
|
|
|
this.url = obj.url;
|
|
|
|
this.name = obj.name;
|
|
|
|
this.size = obj.size;
|
2018-07-31 20:09:18 +02:00
|
|
|
this.manifest = obj.manifest;
|
2018-01-24 19:23:13 +01:00
|
|
|
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;
|
2018-02-21 05:31:27 +01:00
|
|
|
this.keychain = new Keychain(obj.secretKey, obj.nonce);
|
2018-01-30 21:15:09 +01:00
|
|
|
this._hasPassword = !!obj.hasPassword;
|
2018-08-08 20:07:09 +02:00
|
|
|
this.timeLimit = obj.timeLimit;
|
2018-01-24 19:23:13 +01:00
|
|
|
}
|
|
|
|
|
2018-08-08 00:40:17 +02:00
|
|
|
get hasPassword() {
|
|
|
|
return !!this._hasPassword;
|
|
|
|
}
|
|
|
|
|
|
|
|
get expired() {
|
|
|
|
return this.dlimit === this.dtotal || Date.now() > this.expiresAt;
|
|
|
|
}
|
|
|
|
|
2018-01-24 19:23:13 +01:00
|
|
|
async setPassword(password) {
|
2018-02-21 21:35:52 +01:00
|
|
|
try {
|
|
|
|
this.password = password;
|
|
|
|
this._hasPassword = true;
|
|
|
|
this.keychain.setPassword(password, this.url);
|
|
|
|
const result = await setPassword(this.id, this.ownerToken, this.keychain);
|
|
|
|
return result;
|
|
|
|
} catch (e) {
|
|
|
|
this.password = null;
|
|
|
|
this._hasPassword = false;
|
|
|
|
throw e;
|
|
|
|
}
|
2018-01-24 19:23:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
del() {
|
|
|
|
return del(this.id, this.ownerToken);
|
|
|
|
}
|
|
|
|
|
2018-08-31 19:59:26 +02:00
|
|
|
changeLimit(dlimit, user = {}) {
|
2018-01-24 19:23:13 +01:00
|
|
|
if (this.dlimit !== dlimit) {
|
|
|
|
this.dlimit = dlimit;
|
2018-08-31 19:59:26 +02:00
|
|
|
return setParams(this.id, this.ownerToken, user.bearerToken, { dlimit });
|
2018-01-24 19:23:13 +01:00
|
|
|
}
|
|
|
|
return Promise.resolve(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateDownloadCount() {
|
2018-08-08 00:40:17 +02:00
|
|
|
const oldTotal = this.dtotal;
|
|
|
|
const oldLimit = this.dlimit;
|
2018-01-24 19:23:13 +01:00
|
|
|
try {
|
2018-01-30 21:15:09 +01:00
|
|
|
const result = await fileInfo(this.id, this.ownerToken);
|
2018-01-24 19:23:13 +01:00
|
|
|
this.dtotal = result.dtotal;
|
2018-01-30 21:15:09 +01:00
|
|
|
this.dlimit = result.dlimit;
|
2018-01-24 19:23:13 +01:00
|
|
|
} catch (e) {
|
|
|
|
if (e.message === '404') {
|
|
|
|
this.dtotal = this.dlimit;
|
|
|
|
}
|
2018-02-21 05:31:27 +01:00
|
|
|
// ignore other errors
|
2018-01-24 19:23:13 +01:00
|
|
|
}
|
2018-08-08 00:40:17 +02:00
|
|
|
return oldTotal !== this.dtotal || oldLimit !== this.dlimit;
|
2018-01-24 19:23:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
url: this.url,
|
|
|
|
name: this.name,
|
|
|
|
size: this.size,
|
2018-07-31 20:09:18 +02:00
|
|
|
manifest: this.manifest,
|
2018-01-24 19:23:13 +01:00
|
|
|
time: this.time,
|
|
|
|
speed: this.speed,
|
|
|
|
createdAt: this.createdAt,
|
|
|
|
expiresAt: this.expiresAt,
|
|
|
|
secretKey: arrayToB64(this.keychain.rawSecret),
|
|
|
|
ownerToken: this.ownerToken,
|
|
|
|
dlimit: this.dlimit,
|
|
|
|
dtotal: this.dtotal,
|
2018-08-08 20:07:09 +02:00
|
|
|
hasPassword: this.hasPassword,
|
|
|
|
timeLimit: this.timeLimit
|
2018-01-24 19:23:13 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|