Skip to content

Commit

Permalink
solved assets management with dyn route
Browse files Browse the repository at this point in the history
  • Loading branch information
wassfila committed Dec 9, 2023
1 parent d84a21b commit 9c84b9c
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/pages/asset/[...path].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createReadStream } from 'fs';
import {resolve,join} from 'path'
import { config } from "@/config";

export async function GET({params}){
const imagePath = resolve(join(config.rootdir,config.content,params.path));

try {
const stream = createReadStream(imagePath);

// Identify the correct MIME type based on the file extension, or default to 'application/octet-stream'
//const contentType = 'image/png'
const contentType = 'image/svg+xml'

return new Response(stream, {
status: 200,
headers: {
'Content-Type': contentType,
}
});
} catch (error) {
// Handle error, such as file not found
return new Response('File not found', { status: 404 });
}
}

//TODO serve all assets - to check if in dev mode only
export function getStaticPaths(){
if(import.meta.env.DEV){
const files = [
"panzoom/tree.svg"
]
return files.map((file)=>({params:{path:file}}))
}else{
return []
}
}

0 comments on commit 9c84b9c

Please sign in to comment.