@@ -7,7 +7,9 @@ import { exec } from 'node:child_process'
77import * as fs from 'node:fs/promises'
88import path from 'node:path'
99
10- import { ExecParams , FileList , FilesWrite } from '../shared/schema.ts'
10+ import type { FileList } from '../shared/schema.ts'
11+ import { ExecParams , FilesWrite } from '../shared/schema.ts'
12+ import { get_file_name_from_path } from './fileUtils.ts'
1113
1214process . chdir ( 'workdir' )
1315
@@ -60,8 +62,7 @@ app.get('/files/ls', async (c) => {
6062 * Get the contents of a file or directory
6163 */
6264app . get ( '/files/contents/*' , async ( c ) => {
63- let reqPath = c . req . path . replace ( '/files/contents' , '' )
64- reqPath = reqPath . endsWith ( '/' ) ? reqPath . substring ( 0 , reqPath . length - 1 ) : reqPath
65+ const reqPath = await get_file_name_from_path ( c . req . path )
6566 try {
6667 const mimeType = mime . getType ( reqPath )
6768 const headers = mimeType ? { 'Content-Type' : mimeType } : undefined
@@ -105,7 +106,8 @@ app.get('/files/contents/*', async (c) => {
105106 */
106107app . post ( '/files/contents' , zValidator ( 'json' , FilesWrite ) , async ( c ) => {
107108 const file = c . req . valid ( 'json' )
108- const reqPath = file . path . endsWith ( '/' ) ? file . path . substring ( 0 , file . path . length - 1 ) : file . path
109+ const reqPath = await get_file_name_from_path ( file . path )
110+
109111 try {
110112 await fs . writeFile ( reqPath , file . text )
111113 return c . newResponse ( null , 200 )
@@ -120,8 +122,8 @@ app.post('/files/contents', zValidator('json', FilesWrite), async (c) => {
120122 * Delete a file or directory
121123 */
122124app . delete ( '/files/contents/*' , async ( c ) => {
123- let reqPath = c . req . path . replace ( '/files/contents' , '' )
124- reqPath = reqPath . endsWith ( '/' ) ? reqPath . substring ( 0 , reqPath . length - 1 ) : reqPath
125+ const reqPath = await get_file_name_from_path ( c . req . path )
126+
125127 try {
126128 await fs . rm ( path . join ( process . cwd ( ) , reqPath ) , { recursive : true } )
127129 return c . newResponse ( 'ok' , 200 )
0 commit comments