feat(api): add file upload endpoint for story content
This commit is contained in:
		
							parent
							
								
									d337642ed0
								
							
						
					
					
						commit
						63dd3d063a
					
				
							
								
								
									
										36
									
								
								server/api/upload/content.post.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								server/api/upload/content.post.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,36 @@ | ||||
| import { writeFileSync } from "fs"; | ||||
| import { extname } from "path"; | ||||
| import { v4 } from "uuid"; | ||||
| import { ContentFilenameRegex } from "~/lib/server/constants"; | ||||
| import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; | ||||
| 
 | ||||
| export default eventHandler(async (ev) => { | ||||
| 	const noMultipart = "no multipart data found!???!?"; | ||||
| 	isLoggedIn(ev); | ||||
| 	let mpd = await readMultipartFormData(ev); | ||||
| 	if (!mpd) | ||||
| 		throw createError({ | ||||
| 			statusCode: 400, | ||||
| 			message: noMultipart, | ||||
| 		}); | ||||
| 
 | ||||
| 	let file = mpd[0]; | ||||
| 	if (!file) { | ||||
| 		throw createError({ | ||||
| 			statusCode: 400, | ||||
| 			message: noMultipart, | ||||
| 		}); | ||||
| 	} | ||||
| 	if (!ContentFilenameRegex.test(file.filename!)) { | ||||
| 		throw createError({ | ||||
| 			statusCode: 400, | ||||
| 			message: "unsupported file type!", | ||||
| 		}); | ||||
| 	} | ||||
| 	let b = `${v4()}.${extname(file.filename!)}`; | ||||
| 	writeFileSync(`tmp/${b}`, file.data); | ||||
| 	return { | ||||
| 		success: true, | ||||
| 		fileName: b, | ||||
| 	}; | ||||
| }); | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user