11import archiver from 'archiver' ;
22import { fetch } from 'bun' ;
3- import { Elysia , t } from 'elysia' ;
3+ import { Elysia , t , type UnwrapSchema } from 'elysia' ;
44import { ObjectId } from 'mongodb' ;
55import type { Dirent } from 'node:fs' ;
66import { createWriteStream } from 'node:fs' ;
@@ -22,17 +22,34 @@ import type { ServerDocument } from 'src/util/document-with-objectid-type';
2222import { RouterTags } from './tags' ;
2323import { parseHttpRangeHeaders } from 'src/util/parse-http-range-headers' ;
2424
25- type KompressorStateResponse = {
26- progress : number ;
27- finished : boolean ;
28- state : 'DONE' | 'PROCESSING' | 'ERROR' | 'QUEUED' ;
29- message : string ;
25+ const KompressorStateResponseSchema = t . Object ( {
26+ progress : t . Number ( ) ,
27+ finished : t . Boolean ( ) ,
28+ state : t . UnionEnum ( [ 'DONE' , 'PROCESSING' , 'ERROR' , 'QUEUED' ] ) ,
29+ message : t . String ( ) ,
30+ } ) ;
31+ type KompressorStateResponse = UnwrapSchema < typeof KompressorStateResponseSchema > ;
32+ const isKompressorStateResponse = ( obj : unknown ) : obj is KompressorStateResponse => {
33+ return (
34+ typeof obj === 'object' &&
35+ obj !== null &&
36+ 'progress' in obj &&
37+ 'finished' in obj &&
38+ 'state' in obj &&
39+ 'message' in obj
40+ ) ;
3041} ;
3142
32- type KompressorQueueResponse = {
33- status : 'OK' | 'ERROR' ;
34- message : string ;
35- id : string ;
43+ const KompressorQueueResponseSchema = t . Object ( {
44+ status : t . UnionEnum ( [ 'OK' , 'ERROR' ] ) ,
45+ message : t . String ( ) ,
46+ id : t . String ( ) ,
47+ } ) ;
48+ type KompressorQueueResponse = UnwrapSchema < typeof KompressorQueueResponseSchema > ;
49+ const isKompressorQueueResponse = ( obj : unknown ) : obj is KompressorQueueResponse => {
50+ return (
51+ typeof obj === 'object' && obj !== null && 'status' in obj && 'message' in obj && 'id' in obj
52+ ) ;
3653} ;
3754
3855// Helper functions
@@ -821,12 +838,17 @@ const uploadRouter = new Elysia()
821838 }
822839
823840 try {
824- const queueResponse = await fetch (
841+ const response = await fetch (
825842 `http://${ Hostname } :${ Port } /process/${ type } /${ ids . at ( 0 ) ! } ` ,
826- ) . then ( response => response . json ( ) as Promise < KompressorQueueResponse > ) ;
843+ ) . then ( response => response . json ( ) ) ;
844+ if ( ! isKompressorQueueResponse ( response ) ) {
845+ throw new Error (
846+ `Invalid response from Kompressor queue endpoint: ${ Bun . inspect ( response ) } ` ,
847+ ) ;
848+ }
827849
828850 return {
829- status : queueResponse . status ,
851+ status : response . status ,
830852 uuid,
831853 type,
832854 requiresProcessing : true ,
@@ -853,7 +875,7 @@ const uploadRouter = new Elysia()
853875 } ) ,
854876 response : {
855877 200 : t . Object ( {
856- status : t . Union ( [ t . Literal ( 'OK' ) , t . Literal ( 'Error' ) ] ) ,
878+ status : t . Pick ( KompressorQueueResponseSchema , [ 'status' ] ) ,
857879 uuid : t . String ( ) ,
858880 type : t . String ( ) ,
859881 requiresProcessing : t . Boolean ( ) ,
@@ -870,7 +892,7 @@ const uploadRouter = new Elysia()
870892 )
871893 . post (
872894 '/process/info' ,
873- async ( { body : { uuid, type } } ) => {
895+ async ( { body : { uuid, type } , status } ) => {
874896 const { Enabled, Hostname, Port } = Configuration . Kompressor ;
875897 if ( ! Enabled ) {
876898 return {
@@ -884,14 +906,15 @@ const uploadRouter = new Elysia()
884906 const path = `${ RootDirectory } /${ UploadDirectory } /${ type } /${ uuid } ` ;
885907 const { ids } = await getUploadedFiles ( { type, path } ) ;
886908
887- const response = await fetch ( `http://${ Hostname } :${ Port } /progress/${ ids . at ( 0 ) ! } ` ) ;
888- const info = ( await response . json ( ) ) as KompressorStateResponse ;
889- return {
890- status : 'OK' ,
891- uuid,
892- type,
893- progress : info . progress ,
894- } ;
909+ const response = await fetch ( `http://${ Hostname } :${ Port } /progress/${ ids . at ( 0 ) ! } ` ) . then (
910+ res => res . json ( ) ,
911+ ) ;
912+ if ( ! isKompressorStateResponse ( response ) ) {
913+ err ( `Invalid response from Kompressor progress endpoint: ${ Bun . inspect ( response ) } ` ) ;
914+ return status ( 500 , 'Invalid response from Kompressor' ) ;
915+ }
916+
917+ return { status : 'OK' , uuid, type, progress : response . progress } ;
895918 } ,
896919 {
897920 body : t . Object ( {
@@ -900,7 +923,7 @@ const uploadRouter = new Elysia()
900923 } ) ,
901924 response : {
902925 200 : t . Object ( {
903- status : t . Literal ( 'OK' ) ,
926+ status : t . Pick ( KompressorStateResponseSchema , [ 'state' ] ) ,
904927 uuid : t . String ( ) ,
905928 type : t . String ( ) ,
906929 progress : t . Number ( ) ,
0 commit comments