11import bodyParser from "body-parser" ;
22import express from "express" ;
3+ import { safeParseInt } from "../common/lib/result/safeParseInt" ;
4+ import * as chat from "../database/chat" ;
5+ import * as relation from "../database/matches" ;
36import * as storage from "../database/picture" ;
7+ import { safeGetUserId } from "../firebase/auth/db" ;
48import { safeGetGUID } from "../firebase/auth/lib" ;
59import { compressImage } from "../functions/img/compress" ;
10+ import * as hashing from "../lib/hash" ;
611
712const parseLargeBuffer = bodyParser . raw ( {
813 type : "image/png" ,
@@ -12,11 +17,59 @@ const router = express.Router();
1217
1318/* General Pictures in chat */
1419
20+ router . post ( "/to/:userId" , parseLargeBuffer , async ( req , res ) => {
21+ if ( ! Buffer . isBuffer ( req . body ) ) return res . status ( 400 ) . send ( "not buffer" ) ;
22+ const buf = req . body ;
23+
24+ const sender = await safeGetUserId ( req ) ;
25+ if ( ! sender . ok ) return res . status ( 401 ) . end ( ) ;
26+ const recv = safeParseInt ( req . params . userId ) ;
27+ if ( ! recv . ok ) return res . status ( 400 ) . end ( ) ;
28+
29+ const rel = await relation . getRelation ( sender . value , recv . value ) ;
30+ if ( ! rel . ok ) return res . status ( 401 ) . send ( ) ;
31+ if ( rel . value . status !== "MATCHED" ) return res . status ( 401 ) . send ( ) ;
32+
33+ const hash = hashing . sha256 ( buf . toString ( "base64" ) ) ;
34+ const passkey = hashing . sha256 ( crypto . randomUUID ( ) ) ;
35+
36+ return storage
37+ . uploadPic ( hash , buf , passkey )
38+ . then ( async ( url ) => {
39+ await chat . createImageMessage ( sender . value , rel . value . id , url ) ;
40+ res . status ( 201 ) . send ( url ) . end ( ) ;
41+ } )
42+ . catch ( ( err ) => {
43+ console . log ( err ) ;
44+ res . status ( 500 ) . send ( "Failed to upload image to database" ) . end ( ) ;
45+ } ) ;
46+ } ) ;
47+
48+ router . get ( "/:id" , async ( req , res ) => {
49+ const hash = req . params . id ;
50+ const key = req . query . key ;
51+ if ( ! key ) return res . status ( 400 ) . send ( "key is required" ) ;
52+
53+ return storage
54+ . getPic ( hash , String ( key ) )
55+ . then ( ( buf ) => {
56+ if ( buf ) {
57+ res . status ( 200 ) . send ( buf ) . end ( ) ;
58+ } else {
59+ res . status ( 404 ) . send ( "not found" ) . end ( ) ;
60+ }
61+ } )
62+ . catch ( ( err ) => {
63+ console . error ( err ) ;
64+ res . status ( 500 ) . send ( "Failed to get image from database" ) . end ( ) ;
65+ } ) ;
66+ } ) ;
67+
1568/* Profile Pictures */
1669
1770router . get ( "/profile/:guid" , async ( req , res ) => {
1871 const guid = req . params . guid ;
19- const result = await storage . get ( guid ) ;
72+ const result = await storage . getProf ( guid ) ;
2073 switch ( result . ok ) {
2174 case true :
2275 return res . send ( result . value ) ;
@@ -29,12 +82,12 @@ router.post("/profile", parseLargeBuffer, async (req, res) => {
2982 const guid = await safeGetGUID ( req ) ;
3083 if ( ! guid . ok ) return res . status ( 401 ) . send ( ) ;
3184
32- if ( ! Buffer . isBuffer ( req . body ) ) return res . status ( 404 ) . send ( "not buffer" ) ;
85+ if ( ! Buffer . isBuffer ( req . body ) ) return res . status ( 400 ) . send ( "not buffer" ) ;
3386
3487 const buf = await compressImage ( req . body ) ;
3588 if ( ! buf . ok ) return res . status ( 500 ) . send ( "failed to compress image" ) ;
3689
37- const url = await storage . set ( guid . value , buf . value ) ;
90+ const url = await storage . setProf ( guid . value , buf . value ) ;
3891 if ( ! url . ok ) return res . status ( 500 ) . send ( "failed to upload image" ) ;
3992
4093 return res . status ( 201 ) . type ( "text/plain" ) . send ( url . value ) ;
0 commit comments