45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import { Body, Controller, HttpCode, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
|
|
import { Throttle } from '@nestjs/throttler';
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
import { memoryStorage } from 'multer';
|
|
import { QuickImportDto } from './dto/quick-import.dto';
|
|
import { QuickImportService, QuickImportResult } from './quick-import.service';
|
|
|
|
@Controller('quick-import')
|
|
export class QuickImportController {
|
|
constructor(private readonly quickImportService: QuickImportService) {}
|
|
|
|
@Post()
|
|
@HttpCode(200)
|
|
@Throttle({ default: { ttl: 60_000, limit: 20 } })
|
|
@UseInterceptors(
|
|
FileInterceptor('file', {
|
|
storage: memoryStorage(),
|
|
limits: { fileSize: 10 * 1024 * 1024 },
|
|
fileFilter: (req, file, callback) => {
|
|
if (
|
|
file.mimetype === 'application/pdf' ||
|
|
file.mimetype === 'application/octet-stream' ||
|
|
file.mimetype === 'image/jpeg' ||
|
|
file.mimetype === 'image/png' ||
|
|
file.mimetype === 'image/webp'
|
|
) {
|
|
callback(null, true);
|
|
} else {
|
|
callback(new Error('Otillåten filtyp. Använd JPEG, PNG, WebP eller PDF.'), false);
|
|
}
|
|
},
|
|
}),
|
|
)
|
|
async importFromInput(
|
|
@Body() body: QuickImportDto,
|
|
@UploadedFile() file?: Express.Multer.File,
|
|
): Promise<QuickImportResult> {
|
|
if (file) {
|
|
return this.quickImportService.importFromUpload(file);
|
|
}
|
|
|
|
return this.quickImportService.importFromInput(body?.input ?? '');
|
|
}
|
|
}
|