api.file_list { omit:'c' }

Massimo Melina committed Dec 21, 2021 at 12:38 UTC c3ace46a030220db458d5262704a033e694abb2b
3 files changed +21 -10
frontend/src/BrowseFiles.ts
+5 -4
@@ -40,11 +40,12 @@ function useFetchList() {
40 const API = 'file_list'
41 const snap = useSnapState()
42 const search = snap.remoteSearch
43 - const preload = useApi(path && API, { path, search, limit: PRELOAD_SIZE })
44 - const rest = useApi(!preloading && API, { path, search, offset: PRELOAD_SIZE })
43 + const baseParams = { path, search, omit:'c' }
44 + const preload = useApi(path && API, { ...baseParams, limit: PRELOAD_SIZE })
45 + const rest = useApi(!preloading && API, { ...baseParams, offset: PRELOAD_SIZE })
46 const list = useMemo(() => !preload ? null
46 - : !rest ? (preload.list||preload) // the || is for an Error instance
47 - : [...preload.list, ...rest.list],
47 + : !rest ? (preload.list || preload) // the || is for an Error instance
48 + : [...preload.list, ...rest.list],
49 [preload, rest])
50 const unfinished = preload && !rest && list.length === PRELOAD_SIZE
51 if (unfinished && preloading) // let load it all
src/apis.ts
+15 -5
@@ -39,7 +39,7 @@ interface DirEntry { n:string, s?:number, m?:Date, c?:Date }
39
40 export const frontEndApis: ApiHandlers = {
41
42 - async file_list({ path, offset, limit, search }, ctx) {
42 + async file_list({ path, offset, limit, search, omit }, ctx) {
43 let node = await vfs.urlToNode(path || '/', ctx)
44 if (!node)
45 return
@@ -53,11 +53,21 @@ export const frontEndApis: ApiHandlers = {
53 for await (const sub of walker) {
54 if (!match(sub.name))
55 continue
56 + const entry = await nodeToDirEntry(sub)
57 + if (!entry)
58 + continue
59 if (offset) {
60 --offset
61 continue
62 }
60 - list.push(await nodeToFile(sub))
63 + if (omit) {
64 + if (omit !== 'c')
65 + ctx.throw(400, 'omit')
66 + if (!entry.m)
67 + entry.m = entry.c
68 + delete entry.c
69 + }
70 + list.push(entry)
71 if (limit === list.length)
72 break
73 }
@@ -99,10 +109,10 @@ export const frontEndApis: ApiHandlers = {
109 }
110 }
111
102 -async function nodeToFile(node: VfsNode): Promise<DirEntry | null> {
112 +async function nodeToDirEntry(node: VfsNode): Promise<DirEntry | null> {
113 try {
114 return node.source?.includes('//') ? { n:node.name||'' }
105 - : node.source ? statToFile(node.name, await stat(node.source))
115 + : node.source ? statToDirEntry(node.name, await stat(node.source))
116 : node.name ? { n: node.name + '/' }
117 : null
118 }
@@ -112,7 +122,7 @@ async function nodeToFile(node: VfsNode): Promise<DirEntry | null> {
122 }
123 }
124
115 -function statToFile(name: string | undefined, stat:Stats) {
125 +function statToDirEntry(name: string | undefined, stat:Stats) {
126 const folder = stat.isDirectory()
127 const { ctime, mtime } = stat
128 return {
todo.md
+1 -1
@@ -1,7 +1,7 @@
1 # To do
2 - search: try to use server-sent events for the reply
3 https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
4 -- file_list: option to decide what fields to get. If m && !c but we have no m, then copy from c.
4 +- node.link
5 - api: gzip for >5kb
6 - frontend: dialogs
7 - vfs: serve an html for a folder?