88 lines
2.4 KiB
TypeScript
88 lines
2.4 KiB
TypeScript
// src/drive/drive.controller.ts
|
|
import {
|
|
BadRequestException,
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
Param,
|
|
Post,
|
|
Query,
|
|
} from '@nestjs/common';
|
|
import { DriveService } from './drive.service';
|
|
import * as fs from 'fs';
|
|
|
|
@Controller('drive-test')
|
|
export class DriveController {
|
|
constructor(private readonly driveService: DriveService) { }
|
|
|
|
// ✅ Listar archivos/carpetas
|
|
@Get('list')
|
|
async list(@Query('pageToken') pageToken?: string) {
|
|
return this.driveService.list(pageToken || '');
|
|
}
|
|
|
|
// ✅ Crear carpeta dentro de CARPETA
|
|
@Post('mkdir')
|
|
async createFolder(@Body('name') name: string) {
|
|
if (!name) {
|
|
throw new BadRequestException('El nombre es obligatorio');
|
|
}
|
|
const folderId = await this.driveService.mkDir(name);
|
|
return { folderId };
|
|
}
|
|
|
|
// ✅ Obtener carpeta (buscar o crear)
|
|
@Get('folder/:numeroCuenta')
|
|
async getOrCreateFolder(
|
|
@Param('numeroCuenta') numeroCuenta: string,
|
|
) {
|
|
const folderId = await this.driveService.folder(numeroCuenta);
|
|
return { folderId };
|
|
}
|
|
|
|
// ✅ Subir archivo (archivo dummy para pruebas)
|
|
@Post('upload')
|
|
async uploadFile(
|
|
@Body('parent') parent: string,
|
|
) {
|
|
if (!parent) {
|
|
throw new BadRequestException('Parent es obligatorio');
|
|
}
|
|
|
|
// Archivo temporal para prueba
|
|
const tempPath = './temp-test.txt';
|
|
fs.writeFileSync(tempPath, 'Archivo de prueba DriveService');
|
|
|
|
const fileId = await this.driveService.uploadFile(
|
|
tempPath,
|
|
'test-drive.txt',
|
|
'text/plain',
|
|
parent,
|
|
);
|
|
|
|
return { fileId };
|
|
}
|
|
|
|
// ✅ Mover archivo (update)
|
|
@Post('update')
|
|
async updateFile(
|
|
@Body('fileId') fileId: string,
|
|
@Body('addParents') addParents: string,
|
|
@Body('removeParents') removeParents: string,
|
|
) {
|
|
if (!fileId || !addParents || !removeParents) {
|
|
throw new BadRequestException('Datos incompletos');
|
|
}
|
|
|
|
return this.driveService.update(fileId, addParents, removeParents);
|
|
}
|
|
|
|
// ✅ Eliminar archivo
|
|
@Delete('delete/:fileId')
|
|
async deleteFile(@Param('fileId') fileId: string) {
|
|
await this.driveService.deleteFile(fileId);
|
|
return { message: 'Archivo eliminado correctamente' };
|
|
}
|
|
}
|