-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadata.js
More file actions
121 lines (100 loc) · 2.72 KB
/
Metadata.js
File metadata and controls
121 lines (100 loc) · 2.72 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import fs from "fs"
import * as logger from "./logger.js";
export default class Metadata {
_url
_filename;
_fileSize = 0;
_downloadedSize = 0;
_prevDownloadedSize = 0;
_metadataFileName;
_lastUpdateTs = 0;
_prevUpdateTs = 0;
_completed;
constructor(fullFileName, url) {
this._url = url
this._filename = fullFileName
this._metadataFileName = fullFileName + ".nldmeta"
}
getUrl() {
return this._url
}
getFilename() {
return this._filename
}
getFileSize() {
return this._fileSize
}
getDownloadedSize() {
return this._downloadedSize
}
getPrevDownloadedSize() {
return this._prevDownloadedSize
}
getMetadataFileName() {
return this._metadataFileName
}
getLastUpdateTs() {
return this._lastUpdateTs
}
getPrevUpdateTs() {
return this._prevUpdateTs
}
getCompleted() {
return this._completed
}
url(url) {
this._url = url
return this;
}
fileSize(fileSize) {
this._fileSize = fileSize
return this;
}
downloadedSize(downloadedSize) {
this._downloadedSize = downloadedSize
return this
}
prevDownloadedSize(prevDownloadedSize) {
this._prevDownloadedSize = prevDownloadedSize
return this
}
lastUpdateTs(lastUpdateTs) {
this._lastUpdateTs = lastUpdateTs
return this
}
prevUpdateTs(prevUpdateTs) {
this._prevUpdateTs = prevUpdateTs
return this
}
completed(completed) {
this._completed = !!completed
return this
}
flush() {
fs.writeFile(this._metadataFileName, JSON.stringify(this), err => {
if (err) logger.warning("Error saving metadata", err)
})
return this
}
load() {
if (fs.existsSync(this._metadataFileName)) {
try {
let metaFile = fs.readFileSync(this._metadataFileName, {encoding: 'utf-8'})
let meta = JSON.parse(metaFile)
this.fileSize(meta._fileSize || 0)
.downloadedSize(meta._downloadedSize || 0)
.completed(!!meta._completed)
.lastUpdateTs(meta._lastUpdateTs || 0)
.prevUpdateTs(meta._prevUpdateTs || 0)
.prevDownloadedSize(meta._prevDownloadedSize)
.url(meta._url)
logger.debug("loaded metadata", this, metaFile)
return this
} catch (error) {
logger.warning("Cannot read meta", this._metadataFileName, error)
}
}
this.downloadedSize(0)
return this
}
}