| 
									
										
										
										
											2022-04-04 14:21:33 +00:00
										 |  |  | import fs from "fs"; | 
					
						
							|  |  |  | import mime from "mime-types"; | 
					
						
							|  |  |  | import type { NextApiRequest, NextApiResponse } from "next"; | 
					
						
							|  |  |  | import path from "path"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * This endpoint should allow us to access to the private files in the static | 
					
						
							|  |  |  |  * folder of each individual app in the App Store. | 
					
						
							|  |  |  |  * @example | 
					
						
							|  |  |  |  * ```text
 | 
					
						
							|  |  |  |  * Requesting: `/api/app-store/zoomvideo/icon.svg` from a public URL should | 
					
						
							|  |  |  |  * serve us the file located at: `/packages/app-store/zoomvideo/static/icon.svg` | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  * This will allow us to keep all app-specific static assets in the same directory. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export default async function handler(req: NextApiRequest, res: NextApiResponse) { | 
					
						
							|  |  |  |   const [appName, fileName] = Array.isArray(req.query.static) ? req.query.static : [req.query.static]; | 
					
						
							|  |  |  |   const fileNameParts = fileName.split("."); | 
					
						
							| 
									
										
										
										
											2022-04-04 15:53:17 +00:00
										 |  |  |   const { [fileNameParts.length - 1]: fileExtension } = fileNameParts; | 
					
						
							| 
									
										
										
										
											2022-04-04 14:21:33 +00:00
										 |  |  |   const STATIC_PATH = path.join(process.cwd(), "..", "..", "packages/app-store", appName, "static", fileName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     const imageBuffer = fs.readFileSync(STATIC_PATH); | 
					
						
							| 
									
										
										
										
											2022-04-04 15:53:17 +00:00
										 |  |  |     const mimeType = mime.lookup(fileExtension); | 
					
						
							| 
									
										
										
										
											2022-04-04 14:21:33 +00:00
										 |  |  |     if (mimeType) res.setHeader("Content-Type", mimeType); | 
					
						
							|  |  |  |     res.send(imageBuffer); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							| 
									
										
										
										
											2022-04-04 15:53:17 +00:00
										 |  |  |     res.status(400).json({ error: true, message: "Resource not found" }); | 
					
						
							| 
									
										
										
										
											2022-04-04 14:21:33 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } |