@samitouri / QOSami-HFS / commits / 5bbbc723

plugins: ctx.state.considerAsGui to skip max-downloads limits (thumbnails)

Massimo Melina committed Dec 25, 2023 at 18:56 UTC 5bbbc7232d34137481de0f00996c51408b7d81e2
6 files changed +10 -8
dev-plugins.md
+2 -1
@@ -363,9 +363,10 @@ Where `h` is just `import { createElement as h } from 'react'`.
363
364 ## API version history
365
366 -- 8.61 (v0.51.0)
366 +- 8.63 (v0.51.0)
367 - plugin's own hfs-lang files
368 - props.can_overwrite
369 + - ctx.state.considerAsGui
370 - 8.5 (v0.49.0)
371 - new event: entry
372 - exports.onDirEntry: entry.icon
plugins/download-counter/plugin.js
+2 -2
@@ -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 = 5 // count files in archive
4 +exports.version = 5.1 // count files in archive
5 exports.apiRequired = 8.3
6
7 exports.config = {
@@ -45,7 +45,7 @@ exports.init = async api => {
45 frontend_css: 'style.css',
46 unload: () => save.flush(), // we may have pending savings
47 middleware: ctx => () => { // callback = execute after other middlewares are done
48 - if (ctx.status >= 300 || ctx.state.download_counter_ignore || ctx.state.includesLastByte === false) return
48 + if (ctx.status >= 300 || ctx.state.download_counter_ignore || ctx.state.considerAsGui || ctx.state.includesLastByte === false) return
49 if (!(ctx.vfsNode || api.getConfig('archives') && ctx.state.archive)) return
50 ctx.state.completed.then(() => {
51 const key = uri2key(ctx.path)
src/const.ts
+1 -1
@@ -7,7 +7,7 @@ import { mkdirSync } from 'fs'
7 import { basename, dirname, join } from 'path'
8 export * from './cross-const'
9
10 -export const API_VERSION = 8.62
10 +export const API_VERSION = 8.63
11 export const COMPATIBLE_API_VERSION = 1 // while changes in the api are not breaking, this number stays the same, otherwise it is made equal to API_VERSION
12 export const HFS_REPO = 'rejetto/hfs'
13
src/log.ts
+2 -1
@@ -13,6 +13,7 @@ import { DAY, makeNetMatcher, tryJson, Dict, Falsy } from './misc'
13 import events from './events'
14 import { getConnection } from './connections'
15 import { app } from './index'
16 +import { logGui } from './serveGuiFiles'
17
18 class Logger {
19 stream?: Writable
@@ -75,7 +76,7 @@ export const logMw: Koa.Middleware = async (ctx, next) => {
76 // don't await, as we don't want to hold the middlewares chain
77 ctx.state.completed = Promise.race([ once(ctx.res, 'finish'), once(ctx.res, 'close') ])
78 ctx.state.completed.then(() => {
78 - if (ctx.state.dontLog) return
79 + if (ctx.state.dontLog || ctx.state.considerAsGui && !logGui.get()) return
80 if (dontLogNet.compiled()(ctx.ip)) return
81 const isError = ctx.status >= 400
82 const logger = isError && accessErrorLog || accessLogger
src/serveFile.ts
+2 -2
@@ -136,7 +136,7 @@ declare module "koa" {
136 function downloadLimiter<T>(configMax: { get: () => number | undefined }, cbKey: (ctx: Koa.Context) => T | undefined) {
137 const map = new Map<T, number>()
138 return (ctx: Koa.Context) => {
139 - if (!ctx.body) return // no file sent, cache hit
139 + if (!ctx.body || ctx.state.considerAsGui) return // no file sent, cache hit
140 const k = cbKey(ctx)
141 if (k === undefined) return // undefined = skip limit
142 const max = configMax.get()
@@ -154,7 +154,7 @@ function downloadLimiter<T>(configMax: { get: () => number | undefined }, cbKey:
154 return false // limit is enforced but passed
155
156 async function tooMany() {
157 - ctx.set('retry-after', '3600')
157 + ctx.set('retry-after', '60')
158 await sendErrorPage(ctx, HTTP_TOO_MANY_REQUESTS)
159 return true
160 }
src/serveGuiFiles.ts
+1 -1
@@ -17,7 +17,7 @@ import _ from 'lodash'
17 import { defineConfig, getConfig } from './config'
18 import { getLangData } from './lang'
19
20 -const logGui = defineConfig('log_gui', false)
20 +export const logGui = defineConfig('log_gui', false)
21 _.each(FRONTEND_OPTIONS, (v,k) => defineConfig(k, v)) // define default values
22
23 // in case of dev env we have our static files within the 'dist' folder'