File

src/uploads/uploads.controller.ts

Prefix

upload

Index

Methods

Methods

Public Async deleteUpload
deleteUpload(req: FastifyRequest, dto: StandardUploadDto)
Decorators :
@Post('/delete')
Parameters :
Name Type Optional
req FastifyRequest No
dto StandardUploadDto No
Returns : unknown
Public Async fetchUploadRoute
fetchUploadRoute(req: FastifyRequest, uploadDto: UploadGetterDto)
Decorators :
@Post('/fetch')
Parameters :
Name Type Optional
req FastifyRequest No
uploadDto UploadGetterDto No
Returns : unknown
Public Async getEmbed
getEmbed(req: FastifyRequest)
Decorators :
@Get('/fetch/embed')
Parameters :
Name Type Optional
req FastifyRequest No
Returns : unknown
Public Async getImgData
getImgData(up: string)
Decorators :
@Get('/data/:upload')
@Public()
Parameters :
Name Type Optional
up string No
Returns : Promise<EdgeUploadCacheDto>
Public Async setEmbed
setEmbed(req: FastifyRequest, setEmbedDto: EmbedSetterDto)
Decorators :
@Post('/set/embed')
Parameters :
Name Type Optional
req FastifyRequest No
setEmbedDto EmbedSetterDto No
Returns : unknown
Public Async toggleFav
toggleFav(req: FastifyRequest, dto: StandardUploadDto)
Decorators :
@Post('/fav/toggle')
Parameters :
Name Type Optional
req FastifyRequest No
dto StandardUploadDto No
Returns : unknown
Async uploadViaApi
uploadViaApi(req: FastifyRequest)
Decorators :
@RateLimit({for: 'Fastify', keyPrefix: 'upload', points: 7, duration: 10, blockDuration: 120, errorMessage: 'Rate limit exceeded.'})
@Post()
@Public()
@ApiKey()
Parameters :
Name Type Optional
req FastifyRequest<literal type> No
Returns : unknown
Async uploadViaAPiJson
uploadViaAPiJson(req: FastifyRequest)
Decorators :
@RateLimit({for: 'Fastify', keyPrefix: 'upload', points: 7, duration: 10, blockDuration: 120, errorMessage: 'Rate limit exceeded.'})
@Post('/json')
@Public()
@ApiKey()
Parameters :
Name Type Optional
req FastifyRequest<literal type> No
Returns : unknown
Async uploadViaDash
uploadViaDash(req: FastifyRequest)
Decorators :
@Post('/dash')
Parameters :
Name Type Optional
req FastifyRequest<literal type> No
Returns : unknown
Public Async wipeUploads
wipeUploads(req: FastifyRequest)
Decorators :
@Get('/wipe')
Parameters :
Name Type Optional
req FastifyRequest No
Returns : unknown
import {
  Controller,
  Post,
  Req,
  ValidationPipe,
  Body,
  Get,
  Logger,
  Param,
  NotFoundException,
} from '@nestjs/common';
import { FastifyRequest } from 'fastify';
import { UploadsService } from './uploads.service';
import { Public } from '../common/decorators/public.decorator';
import { ApiKey } from '../common/decorators/apikey.decorator';
import EmbedSetterDto from './dto/embed-setter.dto';
import { UploadGetterDto } from './dto/upload-getter.dto';
import { StandardUploadDto } from './dto/standardUploadDto';
import { UploadEnum } from '../utils/enums/responses.enum';
import { EdgeUploadCacheDto } from '../../types';
import { RateLimit } from 'nestjs-rate-limiter';
import { serializeError } from 'serialize-error';
import { HttpException } from '@nestjs/common/exceptions/http.exception';

@Controller('upload')
export class UploadsController {
  private readonly logger = new Logger(UploadsController.name);
  constructor(private readonly uploadService: UploadsService) {}
  @RateLimit({
    for: 'Fastify',
    keyPrefix: 'upload',
    points: 7,
    duration: 10,
    blockDuration: 120,
    errorMessage: 'Rate limit exceeded.',
  })
  @Post()
  @Public()
  @ApiKey()
  async uploadViaApi(
    @Req() req: FastifyRequest<{ Headers: { client: string } }>,
  ) {
    try {
      const start = Date.now();
      const url = await this.uploadService.uploadFileFromApi(req);
      this.logger.debug(`Upload took ${Date.now() - start} ms`);
      return url;
    } catch (err) {
      throw err;
    }
  }
  @RateLimit({
    for: 'Fastify',
    keyPrefix: 'upload',
    points: 7,
    duration: 10,
    blockDuration: 120,
    errorMessage: 'Rate limit exceeded.',
  })
  @Post('/json')
  @Public()
  @ApiKey()
  async uploadViaAPiJson(
    @Req() req: FastifyRequest<{ Headers: { client: string } }>,
  ) {
    try {
      const start = Date.now();
      const url = await this.uploadService.uploadFileFromApi(req, true);
      this.logger.debug(`Upload took ${Date.now() - start} ms`);
      return {
        url,
        raw: true,
        errored: false,
      };
    } catch (err) {
      const serialized = serializeError<HttpException>(err);
      return {
        errored: true,
        error: {
          name: serialized.name,
          message: serialized.message,
        },
      };
    }
  }
  @Post('/dash')
  async uploadViaDash(
    @Req() req: FastifyRequest<{ Headers: { client: string } }>,
  ) {
    req.uploadData =
      await this.uploadService.usersService.getUploadMinimalDataFromUser(
        req.user,
      );
    {
      try {
        const start = Date.now();
        const url = await this.uploadService.uploadFileFromApi(req);
        this.logger.debug(`Upload took ${Date.now() - start} ms`);
        return url;
      } catch (err) {
        throw err;
      }
    }
  }

  @Post('/set/embed')
  public async setEmbed(
    @Req() req: FastifyRequest,
    @Body(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
    setEmbedDto: EmbedSetterDto,
  ) {
    await this.uploadService.setUserEmbed(setEmbedDto, req.user);
    return 'Embed changed successfully.';
  }

  @Get('/fetch/embed')
  public async getEmbed(@Req() req: FastifyRequest) {
    return await this.uploadService.getUserEmbed(req.user);
  }

  @Post('/fetch')
  public async fetchUploadRoute(
    @Req() req: FastifyRequest,
    @Body(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
    uploadDto: UploadGetterDto,
  ) {
    return await this.uploadService.fetchUpload(req.user, uploadDto);
  }

  @Post('/delete')
  public async deleteUpload(
    @Req() req: FastifyRequest,
    @Body(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
    dto: StandardUploadDto,
  ) {
    return await this.uploadService.removeUserUpload(req.user, dto.upload);
  }

  @Post('/fav/toggle')
  public async toggleFav(
    @Req() req: FastifyRequest,
    @Body(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
    dto: StandardUploadDto,
  ) {
    return await this.uploadService.toggleUploadFav(req.user, dto.upload);
  }

  @Get('/wipe')
  public async wipeUploads(@Req() req: FastifyRequest) {
    await this.uploadService.wipeUserUploads(req.user);
    return UploadEnum.UPLOADS_WIPED;
  }
  @Get('/data/:upload')
  @Public()
  public async getImgData(
    @Param('upload') up: string,
  ): Promise<EdgeUploadCacheDto> {
    const { snowflake, owner, exists } = await this.uploadService.getFromPath(
      up,
    );
    if (!exists) {
      throw new NotFoundException();
    }
    const uData = await this.uploadService.getUploadData(snowflake);
    const embed = await this.uploadService.getUserEmbed(owner);
    const url = `${uData.path}.${uData.extension}`;
    return {
      embed,
      url,
      ...uData,
      timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
      isFav: uData.favorite,
      album: !!uData.album,
    };
  }
}

results matching ""

    No results matching ""