1
1
import { existsSync , mkdirSync } from 'node:fs' ;
2
2
import { extname , parse } from 'node:path' ;
3
3
4
- import { BadRequestException , Body , Controller , Post , Req , UploadedFiles , UseInterceptors } from '@nestjs/common' ;
4
+ import { BadRequestException , Body , Controller , Get , Param , Post , Query , Req , UploadedFiles , UseInterceptors } from '@nestjs/common' ;
5
5
import { FilesInterceptor } from '@nestjs/platform-express' ;
6
6
import { UploadStatus , UploadType , User } from '@prisma/client' ;
7
7
import * as crypto from 'crypto' ;
@@ -19,43 +19,11 @@ import { UploadService } from './upload.service';
19
19
export class UploadController {
20
20
constructor ( private readonly uploadService : UploadService ) { }
21
21
22
- @Post ( 'cost' )
23
- getEstimate ( @ Body ( ) body : EstimatesDto ) {
24
- return this . uploadService . getCostEstimate ( body ) ;
22
+ @Get ( )
23
+ async getUploadRequests ( @ Query ( 'page' ) page : number , @ Query ( 'limit' ) limit : number ) {
24
+ return this . uploadService . getUploadRequests ( { page , perPage : limit } ) ;
25
25
}
26
26
27
- @Post ( 'create' )
28
- createUploadRequest ( @Body ( ) body : CreateUploadRequestDto , @Req ( ) req : Request ) {
29
- const { totalChunks, uploadType, fileName, size } = body
30
- const user = ( req as any ) . user as User
31
- if ( ! user ) {
32
- throw new BadRequestException ( 'User not found' ) ;
33
- }
34
-
35
- if ( totalChunks !== 1 ) {
36
- throw new BadRequestException ( 'Total chunks must be 1' ) ;
37
- }
38
-
39
- if ( uploadType === UploadType . MULTIPART_FILE ) {
40
- throw new BadRequestException ( 'Invalid upload type. Only single file upload is supported.' ) ;
41
- }
42
-
43
- // Validate filename has proper extension
44
- if ( ! fileName . includes ( '.' ) ) {
45
- throw new BadRequestException ( 'Filename must include a file extension' ) ;
46
- }
47
-
48
- const extension = fileName . split ( '.' ) . pop ( ) ;
49
- if ( ! extension || extension . length === 0 ) {
50
- throw new BadRequestException ( 'Invalid file. No extension found' ) ;
51
- }
52
-
53
- if ( size <= 0 ) {
54
- throw new BadRequestException ( 'File size must be greater than 0' ) ;
55
- }
56
-
57
- return this . uploadService . createUploadRequest ( body , user ) ;
58
- }
59
27
60
28
@Post ( )
61
29
@UseInterceptors ( FilesInterceptor ( 'file' , 1 , {
@@ -127,6 +95,45 @@ export class UploadController {
127
95
return receipt
128
96
}
129
97
98
+
99
+ @Post ( 'cost' )
100
+ getEstimate ( @Body ( ) body : EstimatesDto ) {
101
+ return this . uploadService . getCostEstimate ( body ) ;
102
+ }
103
+
104
+ @Post ( 'create' )
105
+ createUploadRequest ( @Body ( ) body : CreateUploadRequestDto , @Req ( ) req : Request ) {
106
+ const { totalChunks, uploadType, fileName, size } = body
107
+ const user = ( req as any ) . user as User
108
+ if ( ! user ) {
109
+ throw new BadRequestException ( 'User not found' ) ;
110
+ }
111
+
112
+ if ( totalChunks !== 1 ) {
113
+ throw new BadRequestException ( 'Total chunks must be 1' ) ;
114
+ }
115
+
116
+ if ( uploadType === UploadType . MULTIPART_FILE ) {
117
+ throw new BadRequestException ( 'Invalid upload type. Only single file upload is supported.' ) ;
118
+ }
119
+
120
+ // Validate filename has proper extension
121
+ if ( ! fileName . includes ( '.' ) ) {
122
+ throw new BadRequestException ( 'Filename must include a file extension' ) ;
123
+ }
124
+
125
+ const extension = fileName . split ( '.' ) . pop ( ) ;
126
+ if ( ! extension || extension . length === 0 ) {
127
+ throw new BadRequestException ( 'Invalid file. No extension found' ) ;
128
+ }
129
+
130
+ if ( size <= 0 ) {
131
+ throw new BadRequestException ( 'File size must be greater than 0' ) ;
132
+ }
133
+
134
+ return this . uploadService . createUploadRequest ( body , user ) ;
135
+ }
136
+
130
137
@Post ( 'chunk' )
131
138
async uploadChunk ( @Req ( ) req : Request ) {
132
139
const currentChunk = req . headers [ 'x-current-chunk' ] ;
@@ -199,4 +206,19 @@ export class UploadController {
199
206
200
207
return rest ;
201
208
}
209
+
210
+ @Get ( 'receipt' )
211
+ async getReceipts ( @Query ( 'page' ) page : number , @Query ( 'limit' ) limit : number ) {
212
+ return this . uploadService . getReceipts ( { page, perPage : limit } ) ;
213
+ }
214
+
215
+ @Get ( 'receipt/:id' )
216
+ async getReceipt ( @Param ( 'id' ) id : string ) {
217
+ return this . uploadService . getReceiptById ( id ) ;
218
+ }
219
+
220
+ @Get ( ':id' )
221
+ async getUploadRequest ( @Param ( 'id' ) id : string ) {
222
+ return this . uploadService . getUploadRequest ( id ) ;
223
+ }
202
224
}
0 commit comments