done
This commit is contained in:
150
apps/backend/src/modules/android/android.controller.ts
Normal file
150
apps/backend/src/modules/android/android.controller.ts
Normal file
@@ -0,0 +1,150 @@
|
||||
import {
|
||||
Controller,
|
||||
Post,
|
||||
Get,
|
||||
Body,
|
||||
Query,
|
||||
Req,
|
||||
UseGuards,
|
||||
UseInterceptors,
|
||||
UploadedFile,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
ForbiddenException,
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiOperation,
|
||||
ApiResponse,
|
||||
ApiBearerAuth,
|
||||
ApiConsumes,
|
||||
ApiQuery,
|
||||
} from '@nestjs/swagger';
|
||||
import { Request } from 'express';
|
||||
import { AndroidService } from './android.service';
|
||||
import { AndroidLoginDto } from './dto/android-login.dto';
|
||||
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
||||
import { RolesGuard } from '../auth/guards/roles.guard';
|
||||
import { Roles } from '../auth/decorators/roles.decorator';
|
||||
import { CurrentUser, AuthenticatedUser } from '../auth/decorators/current-user.decorator';
|
||||
import { UserRole, FileCategory } from '@prisma/client';
|
||||
|
||||
@ApiTags('Android Mobile API')
|
||||
@Controller('android')
|
||||
export class AndroidController {
|
||||
constructor(private readonly androidService: AndroidService) {}
|
||||
|
||||
/**
|
||||
* Android Mobile Login Endpoint
|
||||
* Supports both /android/auth/login and /android/login
|
||||
*/
|
||||
@Post(['auth/login', 'login'])
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiOperation({
|
||||
summary: 'Authenticate Android mobile user and issue Bearer Access & Refresh Tokens',
|
||||
description:
|
||||
'Validates credentials with Argon2id, checks account active status, logs mobile device information, and returns Bearer tokens with server limits.',
|
||||
})
|
||||
@ApiResponse({ status: 200, description: 'Android login successful' })
|
||||
@ApiResponse({ status: 401, description: 'Invalid login credentials' })
|
||||
@ApiResponse({ status: 403, description: 'Account suspended or pending administrator approval' })
|
||||
async login(@Body() dto: AndroidLoginDto, @Req() req: Request) {
|
||||
return this.androidService.login(dto, req.ip, req.headers['user-agent']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Android Mobile Direct File Upload
|
||||
* Supports both /android/files/upload and /android/upload
|
||||
*/
|
||||
@Post(['files/upload', 'upload'])
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({
|
||||
summary: 'Upload a file from Android application using multipart/form-data',
|
||||
description:
|
||||
'Performs storage quota validation, file extension filtering, magic byte validation, canonical SHA-256 hashing, and automatic background video processing.',
|
||||
})
|
||||
@ApiConsumes('multipart/form-data')
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
async uploadFile(
|
||||
@UploadedFile() file: Express.Multer.File,
|
||||
@Body('folderId') folderId: string,
|
||||
@CurrentUser() user: AuthenticatedUser,
|
||||
@Req() req: Request,
|
||||
) {
|
||||
return this.androidService.uploadFile(
|
||||
user.id,
|
||||
user.role,
|
||||
file,
|
||||
folderId,
|
||||
req.ip,
|
||||
req.headers['user-agent'],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Android Authenticated User Profile & Quota
|
||||
* Supports both /android/auth/me and /android/me
|
||||
*/
|
||||
@Get(['auth/me', 'me'])
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Get current user profile, company info, and real-time storage quota usage' })
|
||||
async getProfile(@CurrentUser() user: AuthenticatedUser) {
|
||||
return this.androidService.getProfile(user.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Android File Explorer List
|
||||
*/
|
||||
@Get('files')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'List user files for Android Explorer with category filtering and search' })
|
||||
@ApiQuery({ name: 'folderId', required: false })
|
||||
@ApiQuery({ name: 'category', enum: FileCategory, required: false })
|
||||
@ApiQuery({ name: 'search', required: false })
|
||||
@ApiQuery({ name: 'page', required: false, type: Number })
|
||||
@ApiQuery({ name: 'limit', required: false, type: Number })
|
||||
async getUserFiles(
|
||||
@CurrentUser() user: AuthenticatedUser,
|
||||
@Query('folderId') folderId?: string,
|
||||
@Query('category') category?: FileCategory,
|
||||
@Query('search') search?: string,
|
||||
@Query('page') page?: string,
|
||||
@Query('limit') limit?: string,
|
||||
) {
|
||||
return this.androidService.getUserFiles(user.id, user.role, {
|
||||
folderId,
|
||||
category,
|
||||
search,
|
||||
page: page ? parseInt(page, 10) : 1,
|
||||
limit: limit ? parseInt(limit, 10) : 50,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Android Public Server Configuration
|
||||
*/
|
||||
@Get('config')
|
||||
@ApiOperation({ summary: 'Get server endpoints, maximum upload limits, and allowed file formats' })
|
||||
async getConfig() {
|
||||
return this.androidService.getConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* Super Admin Overview & Telemetry for Android API
|
||||
*/
|
||||
@Get('admin/overview')
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles(UserRole.SUPER_ADMIN, UserRole.ADMIN)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Get Android API status, telemetry metrics, and recent mobile logs (Admin Only)' })
|
||||
async getAdminOverview(@CurrentUser() admin: AuthenticatedUser) {
|
||||
if (admin.role !== UserRole.SUPER_ADMIN && !admin.canViewOtherUsers) {
|
||||
throw new ForbiddenException('Access denied. Super Administrator privileges required.');
|
||||
}
|
||||
return this.androidService.getAdminOverview();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user