fix: admin/fs: bad name for home item when no config is provided

Massimo Melina committed Feb 10, 2022 at 00:11 UTC 1c59d3bbc663dba93d39df65bb52b59d526bc7b5
2 files changed +18 -19
src/api.vfs.ts
+11 -5
@@ -1,4 +1,4 @@
1 -import { getNodeName, nodeIsDirectory, vfs, VfsNode, VfsNodeType } from './vfs'
1 +import { getNodeName, nodeIsDirectory, vfs, VfsNode } from './vfs'
2 import _ from 'lodash'
3 import { stat } from 'fs/promises'
4 import { ApiError, ApiHandlers } from './apis'
@@ -9,7 +9,13 @@ import { enforceFinal, isWindows, isWindowsDrive } from './misc'
9 import { exec } from 'child_process'
10 import { promisify } from 'util'
11
12 -type VfsAdmin = { type?: string, size?: number, ctime?: Date, mtime?: Date, children?: VfsAdmin[] } & Omit<VfsNode,'type' | 'children'>
12 +type VfsAdmin = {
13 + type?: string,
14 + size?: number,
15 + ctime?: Date,
16 + mtime?: Date,
17 + children?: VfsAdmin[]
18 +} & Omit<VfsNode, 'type' | 'children'>
19
20 function saveVfs() {
21 saveConfigAsap()
@@ -20,7 +26,7 @@ const apis: ApiHandlers = {
26 async get_vfs() {
27 return { root: vfs.root && await recur(vfs.root) }
28
23 - async function recur(n: VfsNode): Promise<VfsAdmin> {
29 + async function recur(n: typeof vfs.root): Promise<VfsAdmin> {
30 const dir = await nodeIsDirectory(n)
31 const stats: Pick<VfsAdmin, 'size' | 'ctime' | 'mtime'> = {}
32 try {
@@ -57,7 +63,7 @@ const apis: ApiHandlers = {
63 const n = under ? await vfs.urlToNode(under) : vfs.root
64 if (!n)
65 return new ApiError(404, 'invalid under')
60 - if (n.type === VfsNodeType.temp || !await nodeIsDirectory(n))
66 + if (n.isTemp || !await nodeIsDirectory(n))
67 return new ApiError(403, 'invalid under')
68 const a = n.children || (n.children = [])
69 a.unshift({ source, name })
@@ -73,7 +79,7 @@ const apis: ApiHandlers = {
79 if (typeof uri !== 'string')
80 return 400
81 const node = await vfs.urlToNode(uri)
76 - if (!node || node.type === VfsNodeType.temp)
82 + if (!node || node.isTemp)
83 return 404
84 const parent = dirname(uri)
85 const parentNode = await vfs.urlToNode(parent)
src/vfs.ts
+7 -14
@@ -7,13 +7,8 @@ import glob from 'fast-glob'
7 import _ from 'lodash'
8 import { subscribeConfig } from './config'
9
10 -export enum VfsNodeType {
11 - root,
12 - temp,
13 -}
14 -
10 export interface VfsNode {
16 - type?: VfsNodeType,
11 + isTemp?: true, // this node was spawned by a source-d node and is not part of the vfs tree
12 name?: string,
13 source?: string,
14 children?: VfsNode[],
@@ -24,20 +19,18 @@ export interface VfsNode {
19 rename?: Record<string,string>,
20 perm?: Record<string, SinglePerm>,
21 default?: string,
27 - mime?: string
22 + mime?: string,
23 }
24
25 type SinglePerm = 'r' | 'w'
26
32 -const EMPTY = { type: VfsNodeType.root }
33 -
27 export const MIME_AUTO = 'auto'
28
29 export class Vfs {
37 - root: VfsNode = { ...EMPTY }
30 + root: VfsNode = {}
31
32 reset(){
40 - this.root = { ...EMPTY }
33 + this.root = {}
34 }
35
36 async urlToNode(url: string, ctx?: Koa.Context, root?: VfsNode) : Promise<VfsNode | undefined> {
@@ -68,7 +61,7 @@ export class Vfs {
61 try { await fs.stat(source) } // check existence
62 catch { return }
63 return {
71 - type: VfsNodeType.temp,
64 + isTemp: true,
65 source,
66 mime: run.mime || (run.default && MIME_AUTO)
67 }
@@ -96,7 +89,7 @@ export function getNodeName(node: VfsNode) {
89 || basename(node.source)
90 || node.source
91 )
99 - || '(invalid)'
92 + || '' // should happen only for root
93 }
94
95 export async function nodeIsDirectory(node: VfsNode) {
@@ -140,7 +133,7 @@ export async function* walkNode(parent:VfsNode, ctx: Koa.Context, depth:number=0
133 if (path instanceof Buffer)
134 path = path.toString('utf8')
135 yield {
143 - type: VfsNodeType.temp,
136 + isTemp: true,
137 source: base + path,
138 name: prefixPath + (parent!.rename?.[path] || path)
139 }