2017-08-10 19:02:13 +02:00
|
|
|
import { isFile } from './utils';
|
2018-01-24 19:23:13 +01:00
|
|
|
import OwnedFile from './ownedFile';
|
2017-07-21 21:44:55 +02:00
|
|
|
|
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-08-24 23:54:02 +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-08-24 23:54:02 +02:00
|
|
|
this._files = this.loadFiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
loadFiles() {
|
|
|
|
const fs = [];
|
|
|
|
for (let i = 0; i < this.engine.length; i++) {
|
|
|
|
const k = this.engine.key(i);
|
|
|
|
if (isFile(k)) {
|
|
|
|
try {
|
2018-01-31 20:12:36 +01:00
|
|
|
const f = new OwnedFile(JSON.parse(this.engine.getItem(k)));
|
2017-09-12 02:09:29 +02:00
|
|
|
if (!f.id) {
|
|
|
|
f.id = f.fileId;
|
|
|
|
}
|
2018-07-31 20:09:18 +02:00
|
|
|
|
2017-09-12 02:09:29 +02:00
|
|
|
fs.push(f);
|
2017-08-24 23:54:02 +02:00
|
|
|
} catch (err) {
|
|
|
|
// obviously you're not a golfer
|
|
|
|
this.engine.removeItem(k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fs.sort((a, b) => a.createdAt - b.createdAt);
|
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-09-12 02:09:29 +02:00
|
|
|
get enrolled() {
|
|
|
|
return JSON.parse(this.engine.getItem('experiments') || '[]');
|
|
|
|
}
|
|
|
|
|
|
|
|
enroll(id, variant) {
|
|
|
|
const enrolled = this.enrolled;
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
if (!enrolled.find(([i, v]) => i === id)) {
|
|
|
|
enrolled.push([id, variant]);
|
|
|
|
this.engine.setItem('experiments', JSON.stringify(enrolled));
|
|
|
|
}
|
|
|
|
}
|
2017-07-22 02:01:26 +02:00
|
|
|
|
2017-07-21 21:44:55 +02:00
|
|
|
get files() {
|
2017-08-24 23:54:02 +02:00
|
|
|
return this._files;
|
2017-07-21 21:44:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getFileById(id) {
|
2017-08-31 18:43:36 +02:00
|
|
|
return this._files.find(f => f.id === id);
|
2017-07-21 21:44:55 +02:00
|
|
|
}
|
2017-07-22 02:01:26 +02:00
|
|
|
|
2017-09-12 02:09:29 +02:00
|
|
|
get(id) {
|
|
|
|
return this.engine.getItem(id);
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:44:55 +02:00
|
|
|
remove(property) {
|
2017-08-24 23:54:02 +02:00
|
|
|
if (isFile(property)) {
|
|
|
|
this._files.splice(this._files.findIndex(f => f.id === property), 1);
|
|
|
|
}
|
2017-07-21 21:44:55 +02:00
|
|
|
this.engine.removeItem(property);
|
|
|
|
}
|
2017-07-22 02:01:26 +02:00
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
addFile(file) {
|
|
|
|
this._files.push(file);
|
2018-01-24 19:23:13 +01:00
|
|
|
this.writeFile(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeFile(file) {
|
2017-08-24 23:54:02 +02:00
|
|
|
this.engine.setItem(file.id, JSON.stringify(file));
|
2017-07-21 21:44:55 +02:00
|
|
|
}
|
2017-08-31 18:43:36 +02:00
|
|
|
|
|
|
|
writeFiles() {
|
2018-01-24 19:23:13 +01:00
|
|
|
this._files.forEach(f => this.writeFile(f));
|
2017-08-31 18:43:36 +02:00
|
|
|
}
|
2017-07-21 21:44:55 +02:00
|
|
|
}
|
2017-08-24 23:54:02 +02:00
|
|
|
|
|
|
|
export default new Storage();
|