Spaces:
Sleeping
Sleeping
File size: 4,492 Bytes
639bb77 a6d0e2a 639bb77 | 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 195 196 197 198 199 200 201 202 203 204 | import {
Controller,
Get,
Post,
Delete,
Param,
Query,
UseGuards,
Request,
HttpCode,
HttpStatus,
UploadedFile,
UseInterceptors,
Body,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { ApiTags, ApiBearerAuth, ApiConsumes, ApiBody } from '@nestjs/swagger';
import { FilesService } from './files.service';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { UploadFileDto, QueryFilesDto } from './dto';
interface RequestWithUser extends Request {
user: {
id: string;
email: string;
};
}
@ApiTags('Files')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@Controller('files')
export class FilesController {
constructor(private readonly filesService: FilesService) {}
/**
* POST /files
* Upload a file
*/
@Post()
@HttpCode(HttpStatus.CREATED)
@UseInterceptors(FileInterceptor('file'))
@ApiConsumes('multipart/form-data')
@ApiBody({
schema: {
type: 'object',
properties: {
file: {
type: 'string',
format: 'binary',
description: 'File to upload (max 100MB)',
},
spaceId: {
type: 'string',
description: 'Space ID where file belongs',
},
roomId: {
type: 'string',
description: 'Room ID where file belongs',
},
description: {
type: 'string',
description: 'File description',
},
fileType: {
type: 'string',
enum: ['image', 'video', 'audio', 'document', 'other'],
description: 'File type category',
},
},
},
})
async uploadFile(
@UploadedFile() file: Express.Multer.File,
@Body() dto: UploadFileDto,
@Request() req: RequestWithUser,
): Promise<any> {
const result = await this.filesService.uploadFile(req.user.id, file, dto);
return {
success: true,
data: result,
};
}
/**
* GET /files
* Get files list
*/
@Get()
@HttpCode(HttpStatus.OK)
async getFiles(
@Query() query: QueryFilesDto,
@Request() req: RequestWithUser,
): Promise<any> {
const result = await this.filesService.getFiles(req.user.id, query);
return {
success: true,
data: result.files,
meta: {
total: result.total,
hasMore: result.hasMore,
page: query.page || 1,
limit: query.limit || 20,
},
};
}
/**
* GET /files/recent
* Get recent files for user
*/
@Get('recent')
@HttpCode(HttpStatus.OK)
async getRecentFiles(
@Query('limit') limit: number,
@Request() req: RequestWithUser,
): Promise<any> {
const files = await this.filesService.getRecentFiles(
req.user.id,
limit || 10,
);
return {
success: true,
data: files,
};
}
/**
* GET /files/stats
* Get user's file statistics
*/
@Get('stats')
@HttpCode(HttpStatus.OK)
async getFileStats(@Request() req: RequestWithUser): Promise<any> {
const stats = await this.filesService.getUserFileStats(req.user.id);
return {
success: true,
data: stats,
};
}
/**
* GET /files/:fileId
* Get file by ID
*/
@Get(':fileId')
@HttpCode(HttpStatus.OK)
async getFile(
@Param('fileId') fileId: string,
@Request() req: RequestWithUser,
): Promise<any> {
const file = await this.filesService.getFileById(fileId, req.user.id);
return {
success: true,
data: file,
};
}
/**
* DELETE /files/:fileId
* Delete a file
*/
@Delete(':fileId')
@HttpCode(HttpStatus.OK)
async deleteFile(
@Param('fileId') fileId: string,
@Request() req: RequestWithUser,
): Promise<any> {
await this.filesService.deleteFile(fileId, req.user.id);
return {
success: true,
message: 'File deleted successfully',
};
}
/**
* GET /spaces/:spaceId/files
* Get files in a space
*/
@Get('spaces/:spaceId/files')
@HttpCode(HttpStatus.OK)
async getSpaceFiles(
@Param('spaceId') spaceId: string,
@Query() query: QueryFilesDto,
@Request() req: RequestWithUser,
): Promise<any> {
const result = await this.filesService.getSpaceFiles(
spaceId,
req.user.id,
query,
);
return {
success: true,
data: result.files,
meta: {
total: result.total,
hasMore: result.hasMore,
page: query.page || 1,
limit: query.limit || 20,
},
};
}
}
|