-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.ts
242 lines (204 loc) · 5.96 KB
/
cache.ts
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs'
import { deepClone } from './utils'
type CacheUrl = {
title: string
artist: string
duration: number
url: string
access_key: string,
cacheUntil: null | number
}
type CacheFile = {
title: string
artist: string
duration: number
file: string
cacheUntil: null | number
}
type CacheTelegram = {
title: string
artist: string
duration: number
file_id: string
audioInfo: {
size: number
kbps: number
khz: number
mode: string
}
cacheUntil: null | number
}
export type JSONSchema = {
telegram: {
[audio: string]: CacheTelegram
},
file: {
[audio: string]: CacheFile
},
url: {
[audio: string]: CacheUrl
},
session: string
}
function loadJSON() {
const file = readFileSync('./cached.json').toString()
const json: JSONSchema = JSON.parse(file)
return json
}
export function saveJSON() {
const data = JSON.stringify(json, null, 4)
writeFileSync('./cached.json', data)
}
if(!existsSync('./downloaded/')) mkdirSync('./downloaded/')
const json: JSONSchema = existsSync('./cached.json') ? loadJSON() : {
telegram: {},
file: {},
url: {},
session: ''
}
clearCache()
setInterval(clearCache, 1 * 60 * 60 * 1e3)
export function clearCache() {
const time = new Date().getTime()
saveJSON()
}
function getNewTime(type: false | 'url' | 'file' | 'telegram'): number {
const time = new Date().getTime()
switch (type) {
case false:
return time
case 'url':
return time + (1 * 60 * 60 * 1e3)
case 'file':
return time + (24 * 60 * 60 * 1e3)
case 'telegram':
return time + (7 * 24 * 60 * 60 * 1e3)
}
}
export function getCacheAudio(audio: string) {
if (json.telegram[audio] !== undefined) {
json.telegram[audio].cacheUntil = getNewTime('telegram')
saveJSON()
return json.telegram[audio];
}
if (json.file[audio] !== undefined) {
json.file[audio].cacheUntil = getNewTime('file')
saveJSON()
return Object.assign({}, [
json.file[audio],
{ buffer: readFileSync(`./downloaded/${json.file[audio].file}`) }
])
}
if (json.url[audio] !== undefined) {
json.url[audio].cacheUntil = getNewTime('url')
saveJSON()
return json.url[audio];
}
return undefined;
}
export function cacheAudio(type: 'url' | 'file' | 'telegram', audioId: string, audio: {
title: string,
artist: string,
duration: number,
audioInfo?: {
size: number
kbps: number
khz: number
mode: string
},
url?: string,
access_key?: string
}, until: null | number, file?: string): boolean {
if (type === 'telegram') {
if (file === undefined) throw new Error('file_id is undefined')
if(audio.audioInfo === undefined) throw new Error('audioInfo is undefined')
if (json.file[audioId] !== undefined) {
unlinkSync(`./downloaded/${json.file[audioId].file}`)
delete json.file[audioId]
}
if (json.url[audioId] !== undefined) delete json.url[audioId]
json.telegram[audioId] = {
title: audio.title,
artist: audio.artist,
duration: audio.duration,
file_id: file,
audioInfo: audio.audioInfo,
cacheUntil: until
}
saveJSON()
return true;
}
if (type === 'file') {
if (json.telegram[audioId] !== undefined) return false;
if (json.url[audioId] !== undefined) delete json.url[audioId]
if (file === undefined) throw new Error('file is undefined')
json.file[audioId] = {
title: audio.title,
artist: audio.artist,
duration: audio.duration,
file: file,
cacheUntil: until
}
saveJSON()
return true;
}
if (type === 'url') {
if (json.file[audioId] !== undefined || json.telegram[audioId] !== undefined) return false;
if (audio.url === undefined) throw new Error('url is undefined')
if (audio.url === '') throw new Error('url is empty')
if (audio.access_key === undefined) throw new Error('access_key is undefined')
if (audio.access_key === '') throw new Error('access_key is empty')
json.url[audioId] = {
title: audio.title,
artist: audio.artist,
duration: audio.duration,
url: audio.url,
access_key: audio.access_key,
cacheUntil: until
}
saveJSON()
return true;
}
throw new Error('unknown type')
}
export function deleteAudioAllCache(audio: string): boolean {
let res: boolean = false
if (json.telegram[audio] !== undefined) {
delete json.telegram[audio]
res = true
}
if (json.file[audio] !== undefined) {
unlinkSync(`./downloaded/${json.file[audio].file}`)
delete json.file[audio]
res = true
}
if (json.url[audio] !== undefined) {
delete json.url[audio]
res = true
}
if(res) saveJSON()
return res;
}
export function copyCache(from: string, to: string) {
if(json.telegram[from] !== undefined) {
if(json.telegram[to] !== undefined) return false
json.telegram[to] = deepClone(json.telegram[from]) as CacheTelegram
saveJSON()
return true
}
if(json.file[from] !== undefined) {
if(json.file[to] !== undefined) return false
json.file[to] = deepClone(json.file[from]) as CacheFile
saveJSON()
return true
}
if(json.url[from] !== undefined) {
if(json.url[to] !== undefined) return false
json.url[to] = deepClone(json.url[from]) as CacheUrl
saveJSON()
return true
}
return false
}
export function startCacheAudios() { }
export { json as cache }