@samitouri / QOSami-HFS / commits / 8cc672a2

plugin/download-counter: count only finished downloads and support download managers

Massimo Melina committed Aug 2, 2023 at 11:31 UTC 8cc672a24b970f91807f1a862715b27aafe786f4
3 files changed +11 -5
plugins/download-counter/plugin.js
+7 -4
@@ -2,7 +2,7 @@
2
3 exports.description = "Counts downloads for each file, and displays the total in the list or file menu"
4 exports.version = 4.1 // fix: different cases and encodings with urls weren't properly counted
5 -exports.apiRequired = 8
5 +exports.apiRequired = 8.3
6
7 exports.config = {
8 where: { frontend: true, type: 'select', defaultValue: 'menu',
@@ -46,9 +46,12 @@ exports.init = async api => {
46 middleware: (ctx) =>
47 () => { // execute after other middlewares are done
48 if (ctx.status >= 300 || !ctx.vfsNode || ctx.state.download_counter_ignore) return
49 - const k = uri2key(ctx.path)
50 - counters[k] = counters[k] + 1 || 1
51 - save()
49 + if (ctx.state.includesLastByte === false) return
50 + ctx.state.completed.then(() => {
51 + const k = uri2key(ctx.path)
52 + counters[k] = counters[k] + 1 || 1
53 + save()
54 + })
55 },
56 onDirEntry: ({ entry, listUri }) => {
57 const k = uri2key(listUri + entry.n)
src/log.ts
+3 -1
@@ -69,7 +69,9 @@ export const logMw: Koa.Middleware = async (ctx, next) => {
69 const now = new Date()
70 await next()
71 console.debug(ctx.status, ctx.method, ctx.originalUrl)
72 - Promise.race([ once(ctx.res, 'finish'), once(ctx.res, 'close') ]).then(() => {
72 + // don't await, as we don't want to hold the middlewares chain
73 + ctx.state.completed = Promise.race([ once(ctx.res, 'finish'), once(ctx.res, 'close') ])
74 + ctx.state.completed.then(() => {
75 if (ctx.state.dont_log) return
76 if (dontLogNet.compiled()(ctx.ip)) return
77 const isError = ctx.status >= 400
src/serveFile.ts
+1
@@ -116,6 +116,7 @@ export function getRange(ctx: Koa.Context, totalSize: number) {
116 ctx.body = 'Requested Range Not Satisfiable'
117 return
118 }
119 + ctx.state.includesLastByte = end === max
120 ctx.status = HTTP_PARTIAL_CONTENT
121 ctx.set('Content-Range', `bytes ${start}-${isNaN(end) ? '' : end}/${isNaN(totalSize) ? '*' : totalSize}`)
122 ctx.response.length = end - start + 1