2017-07-21 21:44:55 +02:00
|
|
|
const { isFile } = require('./utils');
|
|
|
|
|
2017-08-10 03:16:10 +02:00
|
|
|
class Mem {
|
|
|
|
constructor() {
|
|
|
|
this.items = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
get length() {
|
|
|
|
return this.items.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
getItem(key) {
|
|
|
|
return this.items.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
setItem(key, value) {
|
|
|
|
return this.items.set(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeItem(key) {
|
|
|
|
return this.items.delete(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
key(i) {
|
|
|
|
return this.items.keys()[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:44:55 +02:00
|
|
|
class Storage {
|
2017-08-10 03:16:10 +02:00
|
|
|
constructor() {
|
|
|
|
try {
|
|
|
|
this.engine = localStorage || new Mem();
|
|
|
|
} catch (e) {
|
|
|
|
this.engine = new Mem();
|
|
|
|
}
|
2017-07-21 21:44:55 +02:00
|
|
|
}
|
2017-07-22 02:01:26 +02:00
|
|
|
|
2017-07-21 21:44:55 +02: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 02:01:26 +02:00
|
|
|
|
2017-07-21 21:44:55 +02:00
|
|
|
get files() {
|
|
|
|
const fs = [];
|
|
|
|
for (let i = 0; i < this.engine.length; i++) {
|
|
|
|
const k = this.engine.key(i);
|
|
|
|
if (isFile(k)) {
|
2017-08-02 20:17:23 +02:00
|
|
|
try {
|
|
|
|
fs.push(JSON.parse(this.engine.getItem(k)));
|
|
|
|
} catch (err) {
|
|
|
|
// obviously you're not a golfer
|
|
|
|
this.engine.removeItem(k);
|
|
|
|
}
|
2017-07-21 21:44:55 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-29 08:43:57 +02:00
|
|
|
return fs.sort((file1, file2) => {
|
|
|
|
const creationDate1 = new Date(file1.creationDate);
|
|
|
|
const creationDate2 = new Date(file2.creationDate);
|
|
|
|
return creationDate1 - creationDate2;
|
|
|
|
});
|
2017-07-21 21:44:55 +02: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 02:01:26 +02:00
|
|
|
|
2017-07-21 21:44:55 +02:00
|
|
|
remove(property) {
|
|
|
|
this.engine.removeItem(property);
|
|
|
|
}
|
2017-07-22 02:01:26 +02:00
|
|
|
|
2017-07-21 21:44:55 +02:00
|
|
|
addFile(id, file) {
|
|
|
|
this.engine.setItem(id, JSON.stringify(file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 02:01:26 +02:00
|
|
|
module.exports = Storage;
|