fix: download-counter plugin wasn't exposing counter in frontend

Massimo Melina committed Feb 10, 2023 at 14:51 UTC d3e896eeea4f2762a21872ed614b2c99a5d6764b
2 files changed +8 -9
frontend/index.html
+1 -1
@@ -8,9 +8,9 @@
8 <title>File Server</title>
9 </head>
10 <body>
11 - <div hidden>_HFS_PLUGINS_</div>
11 <noscript>You need to enable JavaScript to run this app.</noscript>
12 <script type="module" src="/src/index.ts"></script>
13 <div id="root"></div>
14 + <div hidden>_HFS_PLUGINS_</div>
15 </body>
16 </html>
src/plugins.ts
+7 -8
@@ -3,7 +3,6 @@
3 import glob from 'fast-glob'
4 import { watchLoad } from './watchLoad'
5 import _ from 'lodash'
6 -import pathLib from 'path'
6 import { API_VERSION, APP_PATH, COMPATIBLE_API_VERSION, PLUGINS_PUB_URI } from './const'
7 import * as Const from './const'
8 import Koa from 'koa'
@@ -27,6 +26,7 @@ import events from './events'
26 import { readFile } from 'fs/promises'
27 import { existsSync, mkdirSync } from 'fs'
28 import { getConnections } from './connections'
29 +import { dirname, resolve } from 'path'
30
31 export const PATH = 'plugins'
32 export const DISABLING_POSTFIX = '-disabled'
@@ -95,10 +95,9 @@ export function pluginsMiddleware(): Koa.Middleware {
95 if (!ctx.pluginStopped) {
96 if (path.startsWith(PLUGINS_PUB_URI)) {
97 const a = path.substring(PLUGINS_PUB_URI.length).split('/')
98 - if (plugins.hasOwnProperty(a[0]!)) { // do it only if the plugin is loaded
99 - a.splice(1, 0, 'public')
100 - await serveFile(PATH + '/' + a.join('/'), 'auto')(ctx, next)
101 - }
98 + const name = a.shift()!
99 + if (plugins.hasOwnProperty(name)) // do it only if the plugin is loaded
100 + await serveFile(plugins[name]!.folder + '/public/' + a.join('/'), 'auto')(ctx, next)
101 }
102 await next()
103 }
@@ -114,7 +113,7 @@ type OnDirEntry = (params:OnDirEntryParams) => void | false
113 export class Plugin {
114 started = new Date()
115
117 - constructor(readonly id:string, private readonly data:any, private unwatch:()=>void){
116 + constructor(readonly id:string, readonly folder:string, private readonly data:any, private unwatch:()=>void){
117 if (!data) throw 'invalid data'
118 if (plugins[id])
119 throw "unload first: " + id
@@ -218,7 +217,7 @@ export async function rescan() {
217 found.push(id)
218 if (plugins[id]) // already loaded
219 continue
221 - const module = pathLib.resolve(f)
220 + const module = resolve(f)
221 const { unwatch } = watchLoad(f, async () => {
222 try {
223 const alreadyRunning = plugins[id]
@@ -260,7 +259,7 @@ export async function rescan() {
259 getHfsConfig: getConfig,
260 })
261 Object.assign(data, res)
263 - const plugin = new Plugin(id, data, unwatch)
262 + const plugin = new Plugin(id, dirname(module), data, unwatch)
263 if (alreadyRunning)
264 events.emit('pluginUpdated', Object.assign(_.pick(plugin, 'started'), getPluginInfo(id)))
265 else {