Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | import { Controller, Get, Post, Put, Delete, Param, Body, Query, Req, } from '@nestjs/common'; import { StorageService } from './storage.service'; import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger'; @Controller('api/v1/files') @ApiTags('File') export class FileController { constructor(private readonly storageService: StorageService) {} private getContext(req: any) { const tenantId = req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM'; const userId = req.user?.id || req.headers['x-user-id'] || 'system-user-id'; return { tenantId, userId }; } @Post('upload-init') @ApiOperation({ summary: 'Init upload operation' }) @ApiResponse({ status: 201, description: 'Operation successful' }) async initUpload( @Body() body: { fileName: string; mimeType: string; size: number; folderId?: string; }, @Req() req: any, ) { const ctx = this.getContext(req); return this.storageService.initUpload({ ...body, tenantId: ctx.tenantId, userId: ctx.userId, }); } @Post('upload-complete') @ApiOperation({ summary: 'Complete upload operation' }) @ApiResponse({ status: 201, description: 'Operation successful' }) async completeUpload( @Body() body: { uploadId: string; fileName: string; mimeType: string; folderId?: string; tags?: string[]; }, @Req() req: any, ) { const ctx = this.getContext(req); // In production, buffer comes from multipart; here we accept a placeholder const buffer = Buffer.from('placeholder-file-content'); return this.storageService.completeUpload({ ...body, tenantId: ctx.tenantId, userId: ctx.userId, buffer, }); } @Get() @ApiOperation({ summary: 'List files operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async listFiles( @Query('folderId') folderId: string, @Query('page') page: string, @Query('limit') limit: string, @Req() req: any, ) { const ctx = this.getContext(req); return this.storageService.listFiles( ctx.tenantId, folderId || undefined, page ? parseInt(page) : 1, limit ? parseInt(limit) : 20, ); } @Get(':id') @ApiOperation({ summary: 'Get file operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async getFile(@Param('id') id: string, @Req() req: any) { const ctx = this.getContext(req); return this.storageService.getFile(id, ctx.tenantId); } @Get(':id/download') @ApiOperation({ summary: 'Download file operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async downloadFile(@Param('id') id: string, @Req() req: any) { const ctx = this.getContext(req); const url = await this.storageService.getDownloadUrl(id, ctx.tenantId); return { downloadUrl: url }; } @Post(':id/share') @ApiOperation({ summary: 'Share file operation' }) @ApiResponse({ status: 201, description: 'Operation successful' }) async shareFile(@Param('id') id: string, @Req() req: any) { const ctx = this.getContext(req); return this.storageService.createShareLink(id, ctx.tenantId); } @Delete(':id') @ApiOperation({ summary: 'Delete file operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async deleteFile(@Param('id') id: string, @Req() req: any) { const ctx = this.getContext(req); return this.storageService.softDelete(id, ctx.tenantId, ctx.userId); } @Post(':id/restore') @ApiOperation({ summary: 'Restore file operation' }) @ApiResponse({ status: 201, description: 'Operation successful' }) async restoreFile(@Param('id') id: string, @Req() req: any) { const ctx = this.getContext(req); return this.storageService.restoreFile(id, ctx.tenantId); } } @Controller('api/v1/folders') @ApiTags('Folder') export class FolderController { constructor(private readonly storageService: StorageService) {} private getContext(req: any) { const tenantId = req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM'; const userId = req.user?.id || req.headers['x-user-id'] || 'system-user-id'; return { tenantId, userId }; } @Get() @ApiOperation({ summary: 'List folders operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async listFolders( @Query('parentFolderId') parentFolderId: string, @Req() req: any, ) { const ctx = this.getContext(req); return this.storageService.listFolders( ctx.tenantId, parentFolderId || undefined, ); } @Post() @ApiOperation({ summary: 'Create folder operation' }) @ApiResponse({ status: 201, description: 'Operation successful' }) async createFolder( @Body() body: { name: string; parentFolderId?: string }, @Req() req: any, ) { const ctx = this.getContext(req); return this.storageService.createFolder({ ...body, tenantId: ctx.tenantId, ownerId: ctx.userId, }); } @Put(':id') @ApiOperation({ summary: 'Update folder operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async updateFolder( @Param('id') id: string, @Body() body: { name?: string }, @Req() req: any, ) { const ctx = this.getContext(req); return this.storageService.updateFolder(id, ctx.tenantId, body); } @Delete(':id') @ApiOperation({ summary: 'Delete folder operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async deleteFolder(@Param('id') id: string, @Req() req: any) { const ctx = this.getContext(req); return this.storageService.deleteFolder(id, ctx.tenantId); } } |