forked from PieterVeldman/FSFilmDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.helper.js
90 lines (84 loc) · 2.56 KB
/
storage.helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Add and entry to image downloads log
* returns true if current_n_records plus the records already downloaded do not
* exceed maximum download quota
*/
function addLineToChromeStorage(current_n_records, callback ) {
const MAXIMUM_PERMITTED_DOWNLOADS_PER_DAY = 100000;
return chrome.storage.local.get({FSFilmLog : []}, function (result) {
FSFilmLog = result.FSFilmLog;
FSFilmLog = deleteOldRecords(FSFilmLog);
let sum_of_records = countDownloadedRecords(FSFilmLog);
// is the current sum_of_records + current_n_records more than the
// allowed quota?
if (sum_of_records + current_n_records <= MAXIMUM_PERMITTED_DOWNLOADS_PER_DAY) {
FSFilmLog.push({datetime: Date.now() ,n_records: current_n_records });
return chrome.storage.local.set({FSFilmLog: FSFilmLog}, function () {
callback(true);
});
}else{
callback(false);
}
});
}
/**
* Function designed to reduce array of log entries to number of downloaded records
*
* @param {*} accumulator
* @param {*} entry
*/
function records_counter(accumulator, entry) {
return accumulator + entry.n_records;
}
/**
* Counts how many records were downloaded
*/
function countDownloadedRecords(FSFilmLog) {
return FSFilmLog.reduce(records_counter,0)
}
/**
* returns true if the entry is recent enough
* @param {*} entry Log entry
*/
function isRecentEnough(entry) {
const day_period = 86400000; // miliseconds in a day = 24*60*60*1000
const lower_miliseconds_limit = Date.now() - day_period;
return entry.datetime > lower_miliseconds_limit
}
/**
* Sweep through log rows and delete those which are older than 24 hours
*
* @param {*} FSFilmLog Log Rows
*/
function deleteOldRecords(FSFilmLog) {
FSFilmLog = FSFilmLog.filter(isRecentEnough);
return FSFilmLog;
}
/**
* For debugging only. Prints download log
*/
function printChromeStorage() {
chrome.storage.local.get({FSFilmLog : []}, function (result) {
console.log(result.FSFilmLog);
});
}
/**
* toggle showHide status
*/
function toggleShowHide(callback ) {
return chrome.storage.local.get(['showHide'], function (result) {
showHide = !result.showHide;
return chrome.storage.local.set({showHide: showHide}, function () {
callback(showHide);
});
});
}
/**
* get showHide status
*/
function getShowHideStatus(callback ) {
return chrome.storage.local.get(['showHide'], function (result) {
showHide = result.showHide;
return callback(showHide);
});
}