plugin/download-counter: reintroduced conversion of legacy data

Massimo Melina committed Jan 31, 2025 at 13:06 UTC 6861fbef78e6e64cc13bfa0f098aff9302e8f635
1 file changed +17 -1
plugins/download-counter/plugin.js
+17 -1
@@ -1,7 +1,7 @@
1 // other plugins can use ctx.state.download_counter_ignore to mark downloads that shouldn't be counted
2
3 exports.description = "Counts downloads for each file, and displays the total in the list or file menu"
4 -exports.version = 6.1 // fix
4 +exports.version = 6.2 // reintroduced conversion of legacy data
5 exports.apiRequired = 8.89 // openDb
6
7 exports.config = {
@@ -16,6 +16,22 @@ exports.configDialog = {
16
17 exports.init = async api => {
18 const db = await api.openDb('counters.kv', { defaultPutDelay: 5_000, maxPutDelay: 30_000 })
19 +
20 + try { // load legacy file
21 + const countersFile = 'counters.yaml'
22 + const yaml = api.require('yaml')
23 + const { readFile, rename } = api.require('fs/promises')
24 + const data = await readFile(countersFile, 'utf8')
25 + for (const [k,v] of Object.entries(yaml.parse(data)))
26 + db.put(uri2key(k), v)
27 + rename(countersFile, countersFile + ' - old format, now converted, you can delete')
28 + api.log("data converted")
29 + }
30 + catch(err) {
31 + if (err.code !== 'ENOENT')
32 + api.log(err)
33 + }
34 +
35 return {
36 frontend_js: 'main.js',
37 frontend_css: 'style.css',