Skip to content

Commit

Permalink
Merge pull request #227 from waifuvault/fix-swagger
Browse files Browse the repository at this point in the history
Fix swagger
  • Loading branch information
VictoriqueMoe authored Jan 31, 2025
2 parents 71382c1 + d0bff6a commit cd79c30
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 126 deletions.
10 changes: 5 additions & 5 deletions src/controllers/rest/impl/FileUploadController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Controller, Inject } from "@tsed/di";
import { Delete, Description, Examples, Get, Name, Patch, Put, Returns, Summary } from "@tsed/schema";
import { StatusCodes } from "http-status-codes";
import { FileUploadResponseDto } from "../../../model/dto/FileUploadResponseDto.js";
import { WaifuFileWithAlbum } from "../../../model/dto/WaifuFile.js";
import { FileUploadModel } from "../../../model/db/FileUpload.model.js";
import { BadRequest } from "@tsed/exceptions";
import { MultipartFile, PathParams, type PlatformMulterFile, QueryParams, Req, Res } from "@tsed/common";
Expand Down Expand Up @@ -33,9 +33,9 @@ export class FileUploadController extends BaseRestController {
@(Put("/:bucketToken")
.Description("Upload a file or a URL to a specific bucket, the bucket must exist")
.Summary("Upload a file or send URL to a specific bucket"))
@(Returns(StatusCodes.CREATED, FileUploadResponseDto).Description("If the file was stored successfully"))
@(Returns(StatusCodes.CREATED, WaifuFileWithAlbum).Description("If the file was stored successfully"))
@(Returns(StatusCodes.BAD_REQUEST, DefaultRenderException).Description("If the request was malformed"))
@(Returns(StatusCodes.OK, FileUploadResponseDto).Description("If the file already exists"))
@(Returns(StatusCodes.OK, WaifuFileWithAlbum).Description("If the file already exists"))
@(Returns(StatusCodes.UNSUPPORTED_MEDIA_TYPE, DefaultRenderException).Description(
"If the media type of the file specified was blocked",
))
Expand Down Expand Up @@ -116,7 +116,7 @@ export class FileUploadController extends BaseRestController {
}

@Get("/:token")
@Returns(StatusCodes.OK, FileUploadResponseDto)
@Returns(StatusCodes.OK, WaifuFileWithAlbum)
@Returns(StatusCodes.BAD_REQUEST, DefaultRenderException)
@Description("Get entry info such as when it will expire and the URL")
@Summary("Get entry info via token")
Expand All @@ -136,7 +136,7 @@ export class FileUploadController extends BaseRestController {
}

@Patch("/:token")
@Returns(StatusCodes.OK, FileUploadResponseDto)
@Returns(StatusCodes.OK, WaifuFileWithAlbum)
@Returns(StatusCodes.BAD_REQUEST, DefaultRenderException)
@Description("Modify an entry such as password, expiry and other settings")
@Summary("Modify components of an entry")
Expand Down
12 changes: 7 additions & 5 deletions src/engine/impl/transfomers/dto/FileModelTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@ import { Injectable, InjectContext, ProviderScope } from "@tsed/di";
import { TRANSFORMER } from "../../../../model/di/tokens.js";
import { ITransformer } from "../../../ITransformer.js";
import { FileUploadModel } from "../../../../model/db/FileUpload.model.js";
import { FileUploadResponseDto } from "../../../../model/dto/FileUploadResponseDto.js";
import { WaifuFile, WaifuFileWithAlbum } from "../../../../model/dto/WaifuFile.js";
import type { PlatformContext } from "@tsed/common";

@Injectable({
scope: ProviderScope.SINGLETON,
type: TRANSFORMER,
})
export class FileModelTransformer implements ITransformer<FileUploadModel, FileUploadResponseDto> {
export class FileModelTransformer implements ITransformer<FileUploadModel, WaifuFile> {
@InjectContext()
private $ctx?: PlatformContext;

public supportsInput(input: unknown): boolean {
return input instanceof FileUploadModel;
}

public transform(input: FileUploadModel): Promise<FileUploadResponseDto> {
public transform(input: FileUploadModel): Promise<WaifuFile> {
if (this.$ctx) {
if (typeof this.$ctx.request.query.formatted !== "undefined") {
return FileUploadResponseDto.fromModel(input, this.$ctx.request.query.formatted === "true", true);
return Promise.resolve(
WaifuFileWithAlbum.fromModelAlbum(input, this.$ctx.request.query.formatted === "true"),
);
}
}
return FileUploadResponseDto.fromModel(input, true, true);
return Promise.resolve(WaifuFileWithAlbum.fromModelAlbum(input, true));
}
}
8 changes: 4 additions & 4 deletions src/model/dto/AlbumDto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CollectionOf, Description, Name, Nullable, Property } from "@tsed/schema";
import { FileUploadResponseDto } from "./FileUploadResponseDto.js";
import { WaifuFile } from "./WaifuFile.js";
import { AlbumModel } from "../db/Album.model.js";
import { Builder } from "builder-pattern";

Expand Down Expand Up @@ -30,15 +30,15 @@ export class AlbumDto {
@Property()
@Description("The files belonging to this album")
@Name("files")
@CollectionOf(FileUploadResponseDto)
public files: FileUploadResponseDto[];
@CollectionOf(WaifuFile)
public files: WaifuFile[];

@Property()
@Description("When the album was created")
public dateCreated: number;

public static fromModel(model: AlbumModel): AlbumDto {
const fileDtos = model.files ? model.files.map(f => FileUploadResponseDto.fromModel(f, false, false)) : [];
const fileDtos = model.files ? model.files.map(f => WaifuFile.fromModel(f, false)) : [];
return Builder(AlbumDto)
.token(model.albumToken)
.bucketToken(model.bucketToken)
Expand Down
8 changes: 4 additions & 4 deletions src/model/dto/BucketDto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CollectionOf, Description, Name, Property } from "@tsed/schema";
import { BucketModel } from "../db/Bucket.model.js";
import { Builder } from "builder-pattern";
import { FileUploadResponseDto } from "./FileUploadResponseDto.js";
import { WaifuFileWithAlbum } from "./WaifuFile.js";
import { AlbumInfo } from "../rest/AlbumInfo.js";

@Name("WaifuBucket")
Expand All @@ -15,8 +15,8 @@ export class BucketDto {
@Property()
@Description("The files belonging to this bucket")
@Name("files")
@CollectionOf(FileUploadResponseDto)
public files: FileUploadResponseDto[];
@CollectionOf(WaifuFileWithAlbum)
public files: WaifuFileWithAlbum[];

@Property()
@Description("All the albums in this bucket")
Expand All @@ -25,7 +25,7 @@ export class BucketDto {

public static async fromModel(model: BucketModel): Promise<BucketDto> {
const fileDtos = model.files
? await Promise.all(model.files.map(f => FileUploadResponseDto.fromModel(f, false, true))) // albums in files are resolved here in the query, so no lazy loading is done
? await Promise.all(model.files.map(f => WaifuFileWithAlbum.fromModelAlbum(f, false))) // albums in files are resolved here in the query, so no lazy loading is done
: [];
const albums = model.albums ? model.albums.map(a => AlbumInfo.fromModel(a)) : [];
return Builder(BucketDto).token(model.bucketToken).files(fileDtos).albums(albums).build();
Expand Down
10 changes: 5 additions & 5 deletions src/model/dto/PublicAlbumDto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CollectionOf, Description, Name, Nullable, Property, string } from "@tsed/schema";
import { FileUploadResponseDto } from "./FileUploadResponseDto.js";
import { CollectionOf, Description, Name, Nullable, Property } from "@tsed/schema";
import { WaifuFile } from "./WaifuFile.js";
import { AlbumModel } from "../db/Album.model.js";
import { Builder } from "builder-pattern";
import { FileUtils } from "../../utils/Utils.js";
Expand All @@ -13,13 +13,13 @@ class WaifuPublicFileMetadata {
@Property()
@Description("The URL to the thumbnail of the file, if it is an image")
@Name("thumbnail")
@Nullable(string)
@Nullable(String)
public thumbnail: string | null;

@Property()
@Description("The media type of the file")
@Name("mediaType")
@Nullable(string)
@Nullable(String)
public mediaType: string | null;
}

Expand Down Expand Up @@ -74,7 +74,7 @@ export class PublicAlbumDto {
public static fromModel(model: AlbumModel): PublicAlbumDto {
const fileDtos = model.files
? model.files.map(f => {
const { url, options } = FileUploadResponseDto.fromModel(f, true, false);
const { url, options } = WaifuFile.fromModel(f, true);
const metadata = Builder(WaifuPublicFileMetadata)
.thumbnail(PublicAlbumDto.getThumbnail(model, f))
.mediaType(f.mediaType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Default, Description, Name, Nullable, Property } from "@tsed/schema";
import { FileUploadModel } from "../db/FileUpload.model.js";
import { Builder } from "builder-pattern";
import { Builder, IBuilder } from "builder-pattern";
import { ObjectUtils } from "../../utils/Utils.js";
import { EntrySettings } from "../../utils/typeings.js";
import { AlbumInfo } from "../rest/AlbumInfo.js";
Expand All @@ -22,9 +22,9 @@ class ResponseOptions implements Required<Omit<EntrySettings, "password">> {
public protected = false;
}

@Name("WaifuResponse")
@Name("WaifuFile")
@Description("This is a standard response for the service, containing info about the entry")
export class FileUploadResponseDto {
export class WaifuFile {
@Property()
@Description("Used for file info and deleting")
public token: string;
Expand All @@ -42,11 +42,6 @@ export class FileUploadResponseDto {
@Nullable(String)
public bucket: string | null = null;

@Property()
@Description("The album that this file belongs to")
@Nullable(AlbumInfo)
public album: AlbumInfo | null = null;

@Property()
@Description("How long this file will exist for")
@Nullable(Number, String)
Expand All @@ -57,17 +52,20 @@ export class FileUploadResponseDto {
@Description("The options for this entry")
public options: ResponseOptions;

public static fromModel<T extends boolean>(
fileUploadModel: FileUploadModel,
format: boolean,
lazyLoadRelations: T,
): T extends true ? Promise<FileUploadResponseDto> : FileUploadResponseDto;
public static fromModel(
public album: AlbumInfo | null = null;

public static fromModel<T extends WaifuFile>(
fileUploadModel: FileUploadModel,
format: boolean,
lazyLoadRelations = false,
): FileUploadResponseDto | Promise<FileUploadResponseDto> {
const builder = Builder(FileUploadResponseDto)
builderToUse?: IBuilder<T>,
): WaifuFile {
let builder: IBuilder<T>;
if (builderToUse) {
builder = builderToUse;
} else {
builder = Builder<T>(WaifuFile as unknown as T);
}
builder
.token(fileUploadModel.token)
.bucket(fileUploadModel.bucketToken ?? null)
.views(fileUploadModel.views)
Expand All @@ -79,15 +77,7 @@ export class FileUploadResponseDto {
builder.retentionPeriod(fileUploadModel.expiresIn);
}

builder.options(FileUploadResponseDto.makeOptions(fileUploadModel.settings));
if (lazyLoadRelations) {
return fileUploadModel.album.then(album => {
if (!album) {
return builder.build();
}
return builder.album(AlbumInfo.fromModel(album)).build();
});
}
builder.options(WaifuFile.makeOptions(fileUploadModel.settings));
return builder.build();
}

Expand All @@ -103,3 +93,24 @@ export class FileUploadResponseDto {
return options.build();
}
}

@Name("WaifuFileWithAlbum")
@Description("This is a standard response for the service, containing info about the entry and the album it belongs to")
export class WaifuFileWithAlbum extends WaifuFile {
@Property()
@Description("The album that this file belongs to")
@Nullable(AlbumInfo)
public override album: AlbumInfo | null = null;

public static async fromModelAlbum(fileUploadModel: FileUploadModel, format: boolean): Promise<WaifuFileWithAlbum> {
const album = await fileUploadModel.album;

const builder = Builder(WaifuFileWithAlbum);
if (album) {
builder.album(AlbumInfo.fromModel(album));
} else {
builder.album(null);
}
return WaifuFile.fromModel(fileUploadModel, format, builder) as WaifuFileWithAlbum;
}
}
Loading

0 comments on commit cd79c30

Please sign in to comment.