forked from dvmarinoff/Auuki
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.js
More file actions
67 lines (61 loc) · 1.95 KB
/
file.js
File metadata and controls
67 lines (61 loc) · 1.95 KB
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
import { dateToDashString } from './functions.js';
import { xf } from './xf.js';
class FileHandler {
constructor(args) {}
async readTextFile(file) {
let self = this;
let reader = new FileReader();
reader.readAsText(file);
reader.onload = _ => {
let res = reader.result;
console.log(res);
xf.dispatch('file:upload:workout', res);
};
reader.onerror = _ => {
let err = reader.error;
console.error(`Error reading local file: `);
console.error(reader.error);
};
}
async readBinaryFile() {
self.unsupportedFormat();
}
unsupporedFormat() {
console.warn(`.fit workout files and other binary formats are not yet supported!`);
}
readFile(file) {
let self = this;
let ext = file.name.split('.').pop();
switch(ext) {
case 'zwo': self.readTextFile(file); break;
case 'erg': self.readTextFile(file); break;
case 'mrc': self.readTextFile(file); break;
case 'fit': self.readBinaryfile(file); break;
default: self.unsupportedFormat(); break;
}
}
saveFile() {
let self = this;
let a = document.createElement('a');
document.body.appendChild(a);
a.style = 'display: none';
return function (blob, name) {
let url = window.URL.createObjectURL(blob);
a.href = url;
a.download = name;
a.click();
window.URL.revokeObjectURL(url);
};
};
downloadActivity(activity) {
let self = this;
let blob = new Blob([activity], {type: 'application/octet-stream'});
self.saveFile()(blob, WorkoutFileName());
xf.dispatch('file:download:activity');
}
}
function WorkoutFileName () {
let now = new Date();
return `workout-${dateToDashString(now)}.fit`;
}
export { FileHandler, WorkoutFileName };