@lxdhub/api
Version:
Display, search and copy LXD-images using a web interface.
128 lines (121 loc) • 3.97 kB
text/typescript
import {
BadRequestException,
Body,
Controller,
Get,
Param,
ParseIntPipe,
Post,
Query,
UseInterceptors,
ValidationPipe,
NotFoundException,
InternalServerErrorException,
UploadedFile,
Logger,
Inject,
ForbiddenException
} from '@nestjs/common';
import { ApiResponse } from '@nestjs/swagger';
import { CloneImageDto, CloneImageResponseDto, ImageDetailDto, ImageListOptions, ImageListItemResponse } from '.';
import { ResponseDto } from '@lxdhub/interfaces';
import { ImageService } from './image.service';
import { ImageListItemInterceptor } from './interceptors/image-list-item.interceptor';
import { FileInterceptor } from '@nestjs/platform-express';
import { LXDHubAPISettings } from '../main';
import { ImportImageDto } from './dtos/import-image.dto';
/**
* The Image Controller, which is the API
* interface for Image-Operations.
*/
export class ImageController {
private logger;
constructor(
private readonly imageService: ImageService,
private settings: LXDHubAPISettings
) {
this.logger = new Logger('image.controller');
}
/**
* Returns images, limited by the given pagination options and
* filters it by the given remoteId.
* If remoteId is not given, it takes the first remote.
* @param options The options to paginate through the requested data
*/
async findByRemote(
options: ImageListOptions
): Promise<ImageListItemResponse> {
try {
// Fetches the images
return await this.imageService
.findByRemote(options.remote, options, options.query ? options.query.trim() : null);
}
catch (err) {
if (err instanceof TypeError) {
// Is a search query error
throw new BadRequestException(err.message);
} else if (err instanceof NotFoundException) {
// Not found
throw err;
} else {
// Unknwon error. Should not occur
throw new InternalServerErrorException('Internal Server Error');
}
}
}
/**
* Returns a detail image with the given id
* @param {number} fingerprint The fingerprint of the image
*/
async findOne(
fingerprint: string
): Promise<ResponseDto<ImageDetailDto>> {
return await this.imageService.findOne(fingerprint);
}
/**
* Clones the image with the given id
* @param {number} id The id of the image
*/
async clone(
// Convert id to an integer
id: number,
cloneImageDto: CloneImageDto
)
: Promise<ResponseDto<CloneImageResponseDto>> {
return await this.imageService.cloneImage(id, cloneImageDto);
}
async import(
image,
body: ImportImageDto
) {
if (this.settings.upload) {
return await this.imageService.importImage(image, body.remote, body.aliases);
} else {
throw new ForbiddenException('Image upload is disabled!');
}
}
}