@@ -278,44 +278,71 @@ export const runAPI = async (express, app, __dirname, isPrimary = true) => {
278278
279279 app . post ( '/metadata' , apiLimiter ) ;
280280 app . post ( '/metadata' , async ( req , res ) => {
281- const request = req . body ;
282- if ( ! request . url ) {
283- return fail ( res , "error.api.link.missing" ) ;
284- }
285- const { success, data : normalizedRequest } = await normalizeRequest ( request ) ;
286- if ( ! success ) {
287- return fail ( res , "error.api.invalid_body" ) ;
288- }
289- const parsed = extract (
290- normalizedRequest . url ,
291- APIKeys . getAllowedServices ( req . rateLimitKey ) ,
292- ) ;
293- if ( ! parsed ) {
294- return fail ( res , "error.api.link.invalid" ) ;
295- }
296- if ( "error" in parsed ) {
297- let context ;
298- if ( parsed ?. context ) {
299- context = parsed . context ;
281+ try {
282+ const request = req . body ;
283+
284+ if ( ! request . url ) {
285+ return fail ( res , "error.api.link.missing" ) ;
286+ }
287+
288+ const { success, data : normalizedRequest } = await normalizeRequest ( request ) ;
289+ if ( ! success ) {
290+ return fail ( res , "error.api.invalid_body" ) ;
291+ }
292+
293+ const parsed = extract (
294+ normalizedRequest . url ,
295+ APIKeys . getAllowedServices ( req . rateLimitKey ) ,
296+ ) ;
297+
298+ if ( ! parsed ) {
299+ return fail ( res , "error.api.link.invalid" ) ;
300+ }
301+
302+ if ( "error" in parsed ) {
303+ let context ;
304+ if ( parsed ?. context ) {
305+ context = parsed . context ;
306+ }
307+ return fail ( res , `error.api.${ parsed . error } ` , context ) ;
308+ }
309+
310+ if ( parsed . host !== "youtube" ) {
311+ return res . status ( 501 ) . json ( {
312+ status : "error" ,
313+ code : "not_implemented" ,
314+ message : "Metadata endpoint is only implemented for YouTube."
315+ } ) ;
300316 }
301- return fail ( res , `error.api.${ parsed . error } ` , context ) ;
302- }
303317
304- if ( parsed . host === "youtube" ) {
305318 const youtube = ( await import ( "../processing/services/youtube.js" ) ) . default ;
306- const info = await youtube ( { id : parsed . patternMatch . id } ) ;
307- if ( info . error ) {
308- return fail ( res , info . error ) ;
319+
320+ const fetchInfo = {
321+ id : parsed . patternMatch . id . slice ( 0 , 11 ) ,
322+ metadataOnly : true ,
323+ } ;
324+
325+ const result = await youtube ( fetchInfo ) ;
326+
327+ if ( result . error ) {
328+ return fail ( res , `error.api.${ result . error } ` ) ;
309329 }
310- const meta = {
311- title : info . fileMetadata ?. title ,
312- duration : info . duration || null ,
313- thumbnail : info . cover || null ,
314- author : info . fileMetadata ?. artist || null ,
330+
331+ const metadata = {
332+ title : result . fileMetadata ?. title || null ,
333+ author : result . fileMetadata ?. artist || null ,
334+ duration : result . duration || null ,
335+ thumbnail : result . cover || null ,
315336 } ;
316- return res . json ( { status : "success" , metadata : meta } ) ;
317- } else {
318- return res . status ( 501 ) . json ( { status : "error" , code : "not_implemented" , message : "Metadata endpoint is only implemented for YouTube." } ) ;
337+
338+ return res . json ( {
339+ status : "success" ,
340+ metadata : metadata
341+ } ) ;
342+
343+ } catch ( error ) {
344+ console . error ( 'Metadata endpoint error:' , error ) ;
345+ return fail ( res , "error.api.generic" ) ;
319346 }
320347 } ) ;
321348
0 commit comments