feat(api): add avatar upload route
This commit is contained in:
		
							parent
							
								
									da700b90d5
								
							
						
					
					
						commit
						c9723e644f
					
				
							
								
								
									
										29
									
								
								server/api/upload/avatar.post.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								server/api/upload/avatar.post.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,29 @@ | |||||||
|  | import { v4 } from "uuid"; | ||||||
|  | import { resolve } from "path"; | ||||||
|  | import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; | ||||||
|  | import * as sharp from "sharp"; | ||||||
|  | 
 | ||||||
|  | export default eventHandler(async (ev) => { | ||||||
|  | 	isLoggedIn(ev); | ||||||
|  | 	let mpd = await readMultipartFormData(ev); | ||||||
|  | 	if (!mpd) { | ||||||
|  | 		throw createError({ | ||||||
|  | 			statusCode: 400, | ||||||
|  | 		}); | ||||||
|  | 	} | ||||||
|  | 	let si = sharp(mpd[0].data!); | ||||||
|  | 	let nn = v4(); | ||||||
|  | 	await si | ||||||
|  | 		.resize({ | ||||||
|  | 			height: 500, | ||||||
|  | 			width: 500, | ||||||
|  | 			fit: "outside", | ||||||
|  | 			background: { r: 0, g: 0, b: 0, alpha: 0 }, | ||||||
|  | 		}) | ||||||
|  | 		.toFormat("png") | ||||||
|  | 		.toFile(resolve(`public/${nn}.png`)); | ||||||
|  | 	return { | ||||||
|  | 		success: true, | ||||||
|  | 		file: `${nn}.png`, | ||||||
|  | 	}; | ||||||
|  | }); | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user