2017-07-21 19:44:55 +00:00
|
|
|
const { isFile } = require('./utils');
|
|
|
|
|
|
|
|
class Storage {
|
|
|
|
constructor(engine) {
|
2017-07-22 00:01:26 +00:00
|
|
|
this.engine = engine;
|
2017-07-21 19:44:55 +00:00
|
|
|
}
|
2017-07-22 00:01:26 +00:00
|
|
|
|
2017-07-21 19:44:55 +00:00
|
|
|
get totalDownloads() {
|
|
|
|
return Number(this.engine.getItem('totalDownloads'));
|
|
|
|
}
|
|
|
|
set totalDownloads(n) {
|
|
|
|
this.engine.setItem('totalDownloads', n);
|
|
|
|
}
|
|
|
|
get totalUploads() {
|
|
|
|
return Number(this.engine.getItem('totalUploads'));
|
|
|
|
}
|
|
|
|
set totalUploads(n) {
|
|
|
|
this.engine.setItem('totalUploads', n);
|
|
|
|
}
|
|
|
|
get referrer() {
|
|
|
|
return this.engine.getItem('referrer');
|
|
|
|
}
|
|
|
|
set referrer(str) {
|
|
|
|
this.engine.setItem('referrer', str);
|
|
|
|
}
|
2017-07-22 00:01:26 +00:00
|
|
|
|
2017-07-21 19:44:55 +00:00
|
|
|
get files() {
|
|
|
|
const fs = [];
|
|
|
|
for (let i = 0; i < this.engine.length; i++) {
|
|
|
|
const k = this.engine.key(i);
|
|
|
|
if (isFile(k)) {
|
|
|
|
fs.push(JSON.parse(this.engine.getItem(k))); // parse or whatever else
|
|
|
|
}
|
|
|
|
}
|
2017-07-29 06:43:57 +00:00
|
|
|
return fs.sort((file1, file2) => {
|
|
|
|
const creationDate1 = new Date(file1.creationDate);
|
|
|
|
const creationDate2 = new Date(file2.creationDate);
|
|
|
|
return creationDate1 - creationDate2;
|
|
|
|
});
|
2017-07-21 19:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get numFiles() {
|
|
|
|
let length = 0;
|
|
|
|
for (let i = 0; i < this.engine.length; i++) {
|
|
|
|
const k = this.engine.key(i);
|
|
|
|
if (isFile(k)) {
|
|
|
|
length += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
getFileById(id) {
|
|
|
|
return this.engine.getItem(id);
|
|
|
|
}
|
2017-07-22 00:01:26 +00:00
|
|
|
|
2017-07-21 19:44:55 +00:00
|
|
|
has(property) {
|
|
|
|
return this.engine.hasOwnProperty(property);
|
|
|
|
}
|
|
|
|
|
|
|
|
remove(property) {
|
|
|
|
this.engine.removeItem(property);
|
|
|
|
}
|
2017-07-22 00:01:26 +00:00
|
|
|
|
2017-07-21 19:44:55 +00:00
|
|
|
addFile(id, file) {
|
|
|
|
this.engine.setItem(id, JSON.stringify(file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:01:26 +00:00
|
|
|
module.exports = Storage;
|