@@ -45,35 +45,35 @@ registerFont(__dirname + '/../../fonts/Roboto-Black.ttf', { family: 'RobotoBlack
4545router . get ( '/' , ( req , res ) => {
4646 res . json ( { hi : 'hi world' } ) ;
4747} ) ;
48+ router . get (
49+ '/album-art/:artist/years/:year/:show_date/{:source}/:size.png' ,
50+ async ( req , res ) : any => {
51+ const size = parseInt ( req . params [ 'size' ] || '500' , 10 ) ; // Provide default or handle NaN
4852
49- router . get ( '/album-art/:artist/years/:year/:show_date/{:source}/:size.png' , ( req , res ) => {
50- const size = parseInt ( req . params [ 'size' ] || '500' , 10 ) ; // Provide default or handle NaN
53+ if ( isNaN ( size ) || ! ( size > 0 && size <= 1500 ) ) {
54+ return res . status ( 400 ) . send ( 'Invalid size parameter' ) ;
55+ }
5156
52- if ( isNaN ( size ) || ! ( size > 0 && size <= 1500 ) ) {
53- res . status ( 400 ) . send ( 'Invalid size parameter' ) ;
54- }
57+ const canvas : Canvas = createCanvas ( size , size ) ;
5558
56- const canvas : Canvas = createCanvas ( size , size ) ;
59+ const slug : string = req . params [ 'artist' ] ;
60+ const artist : ArtistInfo | undefined = typedArtistsCache [ slug ] ;
61+ const artistName : string = artist ? artist . name : '' ;
5762
58- const slug : string = req . params [ 'artist ' ] ;
59- const artist : ArtistInfo | undefined = typedArtistsCache [ slug ] ;
60- const artistName : string = artist ? artist . name : '' ;
63+ const year : string = req . params [ 'year ' ] ;
64+ const date : string = req . params [ 'show_date' ] ;
65+ const sourceId : string | undefined = req . params [ 'source' ] ; // source is optional
6166
62- const year : string = req . params [ 'year' ] ;
63- const date : string = req . params [ 'show_date' ] ;
64- const sourceId : string | undefined = req . params [ 'source' ] ; // source is optional
65-
66- fetch ( `${ API_ROOT } /artists/${ slug } /years/${ year } /${ date } ` )
67- . then ( ( apiRes ) => {
67+ try {
68+ const apiRes = await fetch ( `${ API_ROOT } /artists/${ slug } /years/${ year } /${ date } ` ) ;
6869 if ( ! apiRes . ok ) {
6970 throw new Error ( `API request failed with status ${ apiRes . status } ` ) ;
7071 }
71- return apiRes . json ( ) ;
72- } )
73- . then ( ( json : ShowApiResponse ) => {
72+ const json : ShowApiResponse = await apiRes . json ( ) ;
73+
7474 if ( ! json || ! json . sources || json . sources . length === 0 ) {
7575 winston . error ( 'no json sources found (v2 api)' , { slug, year, date, sourceId } ) ;
76- res . status ( 404 ) . send ( 'Show or sources not found' ) ;
76+ return res . status ( 404 ) . send ( 'Show or sources not found' ) ;
7777 }
7878
7979 // Find source by ID if provided, otherwise use the first source
@@ -88,7 +88,7 @@ router.get('/album-art/:artist/years/:year/:show_date/{:source}/:size.png', (req
8888 date,
8989 sourceId,
9090 } ) ;
91- res . status ( 404 ) . send ( 'Source not found or invalid' ) ;
91+ return res . status ( 404 ) . send ( 'Source not found or invalid' ) ;
9292 }
9393
9494 let venue : Venue = {
@@ -130,15 +130,14 @@ router.get('/album-art/:artist/years/:year/:show_date/{:source}/:size.png', (req
130130 date,
131131 sourceId,
132132 } ) ;
133- res . status ( 500 ) . send ( 'Error generating image buffer' ) ;
133+ return res . status ( 500 ) . send ( 'Error generating image buffer' ) ;
134134 }
135- res . send ( buf ) ;
135+ return res . send ( buf ) ;
136136 } ,
137137 'image/png' ,
138138 { compressionLevel : 3 , filters : canvas . PNG_FILTER_NONE }
139139 ) ;
140- } )
141- . catch ( ( error : any ) => {
140+ } catch ( error ) {
142141 winston . error ( 'Error fetching or processing show data (v2 api)' , {
143142 error : error . message || error ,
144143 slug,
@@ -147,16 +146,17 @@ router.get('/album-art/:artist/years/:year/:show_date/{:source}/:size.png', (req
147146 sourceId,
148147 } ) ;
149148 // Avoid sending detailed error messages to the client
150- res . status ( 500 ) . send ( 'Error fetching show data' ) ;
151- } ) ;
152- } ) ;
149+ return res . status ( 500 ) . send ( 'Error fetching show data' ) ;
150+ }
151+ }
152+ ) ;
153153
154- router . get ( '/ios-album-art/:artist/:source_uuid/:size.png' , ( req , res ) => {
154+ router . get ( '/ios-album-art/:artist/:source_uuid/:size.png' , async ( req , res ) : any => {
155155 // Note: source_uuid is now mandatory based on the path, removed '?'
156156 const size = parseInt ( req . params [ 'size' ] || '500' , 10 ) ; // Provide default or handle NaN
157157
158158 if ( isNaN ( size ) || ! ( size > 0 && size <= 1500 ) ) {
159- res . status ( 400 ) . send ( 'Invalid size parameter' ) ;
159+ return res . status ( 400 ) . send ( 'Invalid size parameter' ) ;
160160 }
161161
162162 const canvas : Canvas = createCanvas ( size , size ) ;
@@ -165,85 +165,83 @@ router.get('/ios-album-art/:artist/:source_uuid/:size.png', (req, res) => {
165165 const sourceUuid : string = req . params [ 'source_uuid' ] ;
166166
167167 // Using v3 API endpoint as in the original code
168- fetch ( `https://api.relisten.net/api/v3/shows/${ sourceUuid } ` )
169- . then ( ( apiRes ) => {
170- if ( ! apiRes . ok ) {
171- throw new Error ( `API request failed with status ${ apiRes . status } ` ) ;
172- }
173- return apiRes . json ( ) ;
174- } )
175- . then ( ( json : ShowApiResponse ) => {
176- // Assuming v3 response structure is similar enough
177- // V3 show endpoint returns a single show, sources are within it.
178- if ( ! json || ! json . sources || json . sources . length === 0 ) {
179- winston . error ( 'no json sources found (v3 api)' , { sourceUuid } ) ;
180- res . status ( 404 ) . send ( 'Show or sources not found' ) ;
181- }
182-
183- // In v3 /shows/{uuid}, the response is the show containing the source.
184- // We might still want to find the specific source if multiple exist, though unlikely for this endpoint.
185- const source : Source | undefined =
186- json . sources . find ( ( s ) => s . uuid === sourceUuid ) || json . sources [ 0 ] ;
187-
188- if ( ! source || ! source . sets ) {
189- // Use relevant variables for logging
190- winston . error ( 'no matching source found or source has no sets (v3 api)' , {
191- artistParam,
192- sourceUuid,
193- } ) ;
194- res . status ( 404 ) . send ( 'Source not found or invalid' ) ;
195- }
196-
197- let venue : Venue = {
198- name : 'Unknown Venue' ,
199- location : 'Unknown Location' ,
200- } ;
201-
202- // Prioritize show venue, then source venue (if available on source in v3)
203- if ( json . venue ) {
204- venue = json . venue ;
205- } else if ( source . venue ) {
206- // Check if source.venue exists in v3 API response
207- venue = source . venue ;
208- }
209-
210- // Assuming drawRelistenAlbumArt and makeRect types are correctly inferred or defined elsewhere
211- drawRelistenAlbumArt (
212- canvas ,
213- {
214- artist : artistParam , // Using the artist param directly, might need lookup if it's a slug
215- showDate : json . display_date ,
216- venue : venue . name ,
217- location : venue . location ,
218- } ,
219- makeRect ( 0 , 0 , size , size ) ,
220- 'aspectfill'
221- ) ;
222-
223- res . type ( 'png' ) ;
224-
225- // PNG Buffer, zlib compression level 3 (from 0-9): faster but bigger
226- // Use async version
227- canvas . toBuffer (
228- ( err , buf ) => {
229- if ( err ) {
230- winston . error ( 'Failed to create PNG buffer' , { error : err , artistParam, sourceUuid } ) ;
231- res . status ( 500 ) . send ( 'Error generating image buffer' ) ;
232- }
233- res . send ( buf ) ;
234- } ,
235- 'image/png' ,
236- { compressionLevel : 3 , filters : canvas . PNG_FILTER_NONE }
237- ) ;
238- } )
239- . catch ( ( error : any ) => {
240- winston . error ( 'Error fetching or processing show data (v3 api)' , {
241- error : error . message || error ,
168+ try {
169+ const apiRes = await fetch ( `https://api.relisten.net/api/v3/shows/${ sourceUuid } ` ) ;
170+ if ( ! apiRes . ok ) {
171+ throw new Error ( `API request failed with status ${ apiRes . status } ` ) ;
172+ }
173+ const json : ShowApiResponse = await apiRes . json ( ) ;
174+
175+ // Assuming v3 response structure is similar enough
176+ // V3 show endpoint returns a single show, sources are within it.
177+ if ( ! json || ! json . sources || json . sources . length === 0 ) {
178+ winston . error ( 'no json sources found (v3 api)' , { sourceUuid } ) ;
179+ return res . status ( 404 ) . send ( 'Show or sources not found' ) ;
180+ }
181+
182+ // In v3 /shows/{uuid}, the response is the show containing the source.
183+ // We might still want to find the specific source if multiple exist, though unlikely for this endpoint.
184+ const source : Source | undefined =
185+ json . sources . find ( ( s ) => s . uuid === sourceUuid ) || json . sources [ 0 ] ;
186+
187+ if ( ! source || ! source . sets ) {
188+ // Use relevant variables for logging
189+ winston . error ( 'no matching source found or source has no sets (v3 api)' , {
242190 artistParam,
243191 sourceUuid,
244192 } ) ;
245- res . status ( 500 ) . send ( 'Error fetching show data' ) ;
193+ return res . status ( 404 ) . send ( 'Source not found or invalid' ) ;
194+ }
195+
196+ let venue : Venue = {
197+ name : 'Unknown Venue' ,
198+ location : 'Unknown Location' ,
199+ } ;
200+
201+ // Prioritize show venue, then source venue (if available on source in v3)
202+ if ( json . venue ) {
203+ venue = json . venue ;
204+ } else if ( source . venue ) {
205+ // Check if source.venue exists in v3 API response
206+ venue = source . venue ;
207+ }
208+
209+ // Assuming drawRelistenAlbumArt and makeRect types are correctly inferred or defined elsewhere
210+ drawRelistenAlbumArt (
211+ canvas ,
212+ {
213+ artist : artistParam , // Using the artist param directly, might need lookup if it's a slug
214+ showDate : json . display_date ,
215+ venue : venue . name ,
216+ location : venue . location ,
217+ } ,
218+ makeRect ( 0 , 0 , size , size ) ,
219+ 'aspectfill'
220+ ) ;
221+
222+ res . type ( 'png' ) ;
223+
224+ // PNG Buffer, zlib compression level 3 (from 0-9): faster but bigger
225+ // Use async version
226+ canvas . toBuffer (
227+ ( err , buf ) => {
228+ if ( err ) {
229+ winston . error ( 'Failed to create PNG buffer' , { error : err , artistParam, sourceUuid } ) ;
230+ return res . status ( 500 ) . send ( 'Error generating image buffer' ) ;
231+ }
232+ return res . send ( buf ) ;
233+ } ,
234+ 'image/png' ,
235+ { compressionLevel : 3 , filters : canvas . PNG_FILTER_NONE }
236+ ) ;
237+ } catch ( error ) {
238+ winston . error ( 'Error fetching or processing show data (v3 api)' , {
239+ error : error . message || error ,
240+ artistParam,
241+ sourceUuid,
246242 } ) ;
243+ return res . status ( 500 ) . send ( 'Error fetching show data' ) ;
244+ }
247245} ) ;
248246
249247export default router ; // Use ES6 export default
0 commit comments