@@ -3,72 +3,64 @@ import { type RedisValue } from "https://deno.land/x/redis@v0.32.1/mod.ts";
33import { redis , _R_fileUploadAuth } from "../db/database.ts" ;
44
55import { io } from "./websockets.ts" ;
6- import { Hono } from "https://deno.land/x/hono@v3.12 .4/mod.ts" ;
6+ import { XebecServer } from "https://deno.land/x/xebec@v0.0 .4/mod.ts" ;
77
8- const app = new Hono ( ) ;
8+ const app = new XebecServer ( ) ;
99
1010const MAX_SIZE = 50 * 1024 * 1024 ;
1111
1212//file upload
13- app . post ( '/upload/:key/:uid/:messageId' , async ( ctx ) => {
13+ app . POST ( '/upload/:key/:uid/:messageId' , async ( req ) => {
1414
1515 try {
1616 console . log ( 'Upload request received' ) ;
1717
18- const { key, uid, messageId } = ctx . req . param ( ) ;
18+ const { key, uid, messageId } = req . params ;
1919
2020 const res = await _R_fileUploadAuth ( key , uid ) ;
2121
2222 const [ exists , activeUsers ] = res as [ number , number ] ;
2323
2424 if ( ! exists ) {
25- ctx . status ( 401 ) ;
26- return ctx . json ( { message : 'Unauthorized' } ) ;
25+ return new Response ( 'Unauthorized' , { status : 401 } ) ;
2726 }
2827
2928
3029 if ( Number ( activeUsers ) < 2 ) {
31- ctx . status ( 400 ) ;
32- return ctx . json ( { message : 'Not enough users' } ) ;
30+ return new Response ( 'Not enough users' , { status : 400 } ) ;
3331 }
3432
3533 //check file size before parsing form
36- const contentLength = ctx . req . header ( 'content-length' ) ;
34+ const contentLength = req . headers . get ( 'content-length' ) ;
3735
3836 if ( ! contentLength ) {
39- ctx . status ( 400 ) ;
40- return ctx . json ( { message : 'No content length' } ) ;
37+ return new Response ( 'No content length' , { status : 400 } ) ;
4138 }
4239
4340 if ( + contentLength > MAX_SIZE ) {
44- ctx . status ( 400 ) ;
45- return ctx . json ( { message : `File size should be within ${ MAX_SIZE } bytes.` } ) ;
41+ return new Response ( `File size should be within ${ MAX_SIZE } bytes.` , { status : 400 } ) ;
4642 }
4743
48- const form = await ctx . req . formData ( ) ;
44+ const form = await req . formData ( ) ;
4945
5046
5147 if ( ! form ) {
52- ctx . status ( 400 ) ;
53- return ctx . json ( { message : 'No data found' } ) ;
48+ return new Response ( 'No data found' , { status : 400 } ) ;
5449 }
5550
5651 //file size
5752 const files = form . getAll ( 'file' ) as File [ ] ;
5853
5954 if ( ! files . length ) {
60- ctx . status ( 400 ) ;
61- return ctx . json ( { message : 'No file found. Check field avatar.' } ) ;
55+ return new Response ( 'No file found. Check field avatar.' , { status : 400 } ) ;
6256 }
6357
6458 if ( files . length > 1 ) {
65- ctx . status ( 400 ) ;
66- return ctx . json ( { message : 'Multiple files found. Only one file allowed.' } ) ;
59+ return new Response ( 'Multiple files found. Only one file allowed.' , { status : 400 } ) ;
6760 }
6861
6962 if ( files [ 0 ] . size > MAX_SIZE ) {
70- ctx . status ( 400 ) ;
71- return ctx . json ( { message : `File size should be within ${ MAX_SIZE } bytes.` } ) ;
63+ return new Response ( `File size should be within ${ MAX_SIZE } bytes.` , { status : 400 } ) ;
7264 }
7365
7466 const maxUser = await redis . hget ( `chat:${ key } ` , 'maxUsers' ) as unknown as number ;
@@ -106,28 +98,26 @@ app.post('/upload/:key/:uid/:messageId', async (ctx) => {
10698 } ) ;
10799 } ) ;
108100
109- return ctx . json ( { message : 'File uploaded' } ) ;
101+ return new Response ( 'File uploaded' , { status : 200 } ) ;
110102 } catch ( _ ) {
111103 console . log ( "Error while receiving" ) ;
112104 console . log ( _ ) ;
113- ctx . status ( 400 ) ;
114- return ctx . json ( { message : 'Error while receiving' } ) ;
105+ return new Response ( 'Error while receiving' , { status : 400 } ) ;
115106 }
116107} ) ;
117108
118109//file download
119- app . get ( '/download/:key/:userId/:messageId' , async ( ctx ) => {
110+ app . GET ( '/download/:key/:userId/:messageId' , async ( req ) => {
120111
121- const { key, userId, messageId } = ctx . req . param ( ) ;
112+ const { key, userId, messageId } = req . params ;
122113
123114 try {
124115
125116 const res = await redis . exists ( `chat:${ key } ` , `uid:${ userId } ` , `chat:${ key } :file:${ messageId } ` ) ;
126117
127118 if ( res !== 3 ) {
128119 console . log ( 'Unauthorized' ) ;
129- ctx . status ( 401 ) ;
130- return ctx . json ( { message : 'Unauthorized' } ) ;
120+ return new Response ( 'Unauthorized' , { status : 401 } ) ;
131121 }
132122
133123 //check if this user has not downloaded this file before
@@ -146,34 +136,45 @@ app.get('/download/:key/:userId/:messageId', async (ctx) => {
146136 const dir = await Deno . stat ( path ) . catch ( ( ) => null ) ;
147137
148138 if ( ! dir ) {
149- return ctx . json ( { message : 'File not found' } ) ;
139+ return new Response ( 'File not found' , { status : 404 } ) ;
150140 }
151141
152142 const file = await Deno . open ( path ) ;
153143 const size = ( await file . stat ( ) ) . size ;
154144
155145 //serve file
156- return ctx . newResponse ( file . readable , 200 , {
157- 'Content-Disposition' : `attachment;` ,
158- 'Content-Length' : size . toString ( ) ,
159- 'Content-Type' : 'application/octet-stream'
146+ // return ctx.newResponse(file.readable, 200, {
147+ // 'Content-Disposition': `attachment;`,
148+ // 'Content-Length': size.toString(),
149+ // 'Content-Type': 'application/octet-stream'
150+ // });
151+
152+ return new Response ( file . readable , {
153+ status : 200 ,
154+ headers : new Headers ( {
155+ 'Content-Disposition' : `attachment;` ,
156+ 'Content-Length' : size . toString ( ) ,
157+ 'Content-Type' : 'application/octet-stream'
158+ } )
160159 } ) ;
161160
162161 } catch ( _ ) {
163- ctx . status ( 404 ) ;
164- return ctx . json ( { message : 'Error downloading file' } ) ;
162+ return new Response ( 'Error downloading file' , { status : 404 } ) ;
165163 } finally {
166164
167165 const downloadInfo = await redis . hmget ( `chat:${ key } :file:${ messageId } ` , 'downloadCount' , 'maxDownload' ) ;
168166
169167 const [ downloadCount , maxDownload ] = downloadInfo as unknown as [ number , number ] ;
170168
171169 if ( Number ( downloadCount + 1 ) >= Number ( maxDownload ) ) {
172-
173- await redis . del ( `chat:${ key } :file:${ messageId } ` ) ;
174- await Deno . remove ( `./uploads/${ key } /${ messageId } ` ) ;
175-
176- console . log ( `File deleted: ${ messageId } ` ) ;
170+
171+ try {
172+ await redis . del ( `chat:${ key } :file:${ messageId } ` ) ;
173+ await Deno . remove ( `./uploads/${ key } /${ messageId } ` ) ;
174+ console . log ( `File deleted: ${ messageId } ` ) ;
175+ } catch ( _ ) {
176+ console . log ( 'Error deleting file' ) ;
177+ }
177178 }
178179 }
179180} ) ;
0 commit comments