Refonte qualité (shared / back / front) #1
@@ -9,7 +9,6 @@ import {
|
|||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { FilesInterceptor } from '@nestjs/platform-express';
|
import { FilesInterceptor } from '@nestjs/platform-express';
|
||||||
import { diskStorage } from 'multer';
|
import { diskStorage } from 'multer';
|
||||||
import * as fs from 'fs';
|
|
||||||
import * as fsPromises from 'fs/promises';
|
import * as fsPromises from 'fs/promises';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
@@ -69,11 +68,13 @@ export class UploadsController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Lire tous les contenus en mémoire avant toute suppression */
|
/* Lire tous les contenus en mémoire avant toute suppression */
|
||||||
const fileContents = files.map((file) => ({
|
const fileContents = await Promise.all(
|
||||||
|
files.map(async (file) => ({
|
||||||
name: file.originalname,
|
name: file.originalname,
|
||||||
path: file.path,
|
path: file.path,
|
||||||
content: fs.readFileSync(file.path, 'utf-8'),
|
content: await fsPromises.readFile(file.path, 'utf-8'),
|
||||||
}));
|
})),
|
||||||
|
);
|
||||||
|
|
||||||
/* Nettoyage immédiat des fichiers temporaires */
|
/* Nettoyage immédiat des fichiers temporaires */
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
@@ -197,11 +198,9 @@ export class UploadsController {
|
|||||||
if (!body.name) throw new BadRequestException('name is required');
|
if (!body.name) throw new BadRequestException('name is required');
|
||||||
if (!body.convention) throw new BadRequestException('convention is required');
|
if (!body.convention) throw new BadRequestException('convention is required');
|
||||||
|
|
||||||
/* Crée le répertoire si absent */
|
/* Crée le répertoire si absent (mkdir recursive est idempotent) */
|
||||||
const uploadDir = path.join(os.tmpdir(), 'datacat-uploads');
|
const uploadDir = path.join(os.tmpdir(), 'datacat-uploads');
|
||||||
if (!fs.existsSync(uploadDir)) {
|
await fsPromises.mkdir(uploadDir, { recursive: true });
|
||||||
fs.mkdirSync(uploadDir, { recursive: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
let yamlContent: string;
|
let yamlContent: string;
|
||||||
let detectedType: ApiType | null = null;
|
let detectedType: ApiType | null = null;
|
||||||
@@ -211,7 +210,7 @@ export class UploadsController {
|
|||||||
yamlContent = '';
|
yamlContent = '';
|
||||||
} else if (files.length === 1) {
|
} else if (files.length === 1) {
|
||||||
/* Cas simple : un seul fichier */
|
/* Cas simple : un seul fichier */
|
||||||
yamlContent = fs.readFileSync(files[0].path, 'utf-8');
|
yamlContent = await fsPromises.readFile(files[0].path, 'utf-8');
|
||||||
|
|
||||||
/* Validation syntaxique YAML */
|
/* Validation syntaxique YAML */
|
||||||
this.validateYamlSyntax(yamlContent, files[0].originalname);
|
this.validateYamlSyntax(yamlContent, files[0].originalname);
|
||||||
@@ -219,7 +218,7 @@ export class UploadsController {
|
|||||||
detectedType = this.detectTypeFromContent(yamlContent);
|
detectedType = this.detectTypeFromContent(yamlContent);
|
||||||
} else {
|
} else {
|
||||||
/* Cas multi-fichiers : détecter le fichier principal et bundler */
|
/* Cas multi-fichiers : détecter le fichier principal et bundler */
|
||||||
const { mainFile, type } = this.detectMainFile(files);
|
const { mainFile, type } = await this.detectMainFile(files);
|
||||||
detectedType = type;
|
detectedType = type;
|
||||||
|
|
||||||
this.logger.log(`Multi-file upload: main=${mainFile.originalname}, type=${type}, total=${files.length} files`);
|
this.logger.log(`Multi-file upload: main=${mainFile.originalname}, type=${type}, total=${files.length} files`);
|
||||||
@@ -310,9 +309,11 @@ export class UploadsController {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private detectMainFile(files: Express.Multer.File[]): { mainFile: Express.Multer.File; type: ApiType } {
|
private async detectMainFile(
|
||||||
|
files: Express.Multer.File[],
|
||||||
|
): Promise<{ mainFile: Express.Multer.File; type: ApiType }> {
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const content = fs.readFileSync(file.path, 'utf-8');
|
const content = await fsPromises.readFile(file.path, 'utf-8');
|
||||||
const type = this.detectTypeFromContent(content);
|
const type = this.detectTypeFromContent(content);
|
||||||
if (type) {
|
if (type) {
|
||||||
return { mainFile: file, type };
|
return { mainFile: file, type };
|
||||||
|
|||||||
Reference in New Issue
Block a user