@@ -3,6 +3,7 @@ import { streamSSE } from "hono/streaming";
33import { existsSync } from "node:fs" ;
44import { basename , dirname , extname , join , posix } from "node:path" ;
55import type { MediaProcessingJobState , StudioApiAdapter } from "../types.js" ;
6+ import { requestedProjectPath } from "../helpers/requestSubPath.js" ;
67import { mkdirWithinProject , pinWithinProject , resolveWithinProject } from "../helpers/safePath.js" ;
78import { probeMediaMetadata } from "../helpers/mediaMetadata.js" ;
89
@@ -45,12 +46,6 @@ function isImagePath(path: string): boolean {
4546 return IMAGE_EXTENSIONS . has ( extname ( path ) . toLowerCase ( ) ) ;
4647}
4748
48- function normalizeProjectAssetPath ( path : string , projectDir : string ) : string {
49- const trimmed = path . trim ( ) . replace ( / ^ [ . ] \/ / , "" ) ;
50- const named = resolveWithinProject ( projectDir , trimmed ) ;
51- return named && existsSync ( named ) ? trimmed : trimmed . replace ( / [ ? # ] .* $ / , "" ) ;
52- }
53-
5449function containsNullByte ( path : string ) : boolean {
5550 return path . includes ( "\0" ) ;
5651}
@@ -148,7 +143,7 @@ export function registerMediaRoutes(
148143 const project = await adapter . resolveProject ( c . req . param ( "id" ) ) ;
149144 if ( ! project ) return c . json ( { error : "not found" } , 404 ) ;
150145
151- const assetPath = normalizeProjectAssetPath ( c . req . query ( "path" ) ?? "" , project . dir ) ;
146+ const assetPath = requestedProjectPath ( project . dir , c . req . query ( "path" ) ?? "" ) ;
152147 if ( ! assetPath ) return c . json ( { error : "path required" } , 400 ) ;
153148 if ( containsNullByte ( assetPath ) ) return c . json ( { error : "forbidden" } , 403 ) ;
154149 if ( / ^ (?: h t t p s ? : | d a t a : | b l o b : ) / i. test ( assetPath ) ) {
@@ -177,7 +172,7 @@ export function registerMediaRoutes(
177172
178173 const body = ( await c . req . json ( ) . catch ( ( ) => ( { } ) ) ) as BackgroundRemovalBody ;
179174 const inputAssetPath = body . inputPath
180- ? normalizeProjectAssetPath ( body . inputPath , project . dir )
175+ ? requestedProjectPath ( project . dir , body . inputPath )
181176 : "" ;
182177 if ( ! inputAssetPath ) return c . json ( { error : "inputPath required" } , 400 ) ;
183178 if ( containsNullByte ( inputAssetPath ) ) return c . json ( { error : "forbidden" } , 403 ) ;
@@ -196,7 +191,7 @@ export function registerMediaRoutes(
196191 }
197192
198193 const requestedOutput = body . outputPath
199- ? posix . normalize ( normalizeProjectAssetPath ( body . outputPath , project . dir ) )
194+ ? posix . normalize ( requestedProjectPath ( project . dir , body . outputPath ) )
200195 : "" ;
201196 if ( requestedOutput && containsNullByte ( requestedOutput ) ) {
202197 return c . json ( { error : "forbidden" } , 403 ) ;
0 commit comments