Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file upload parameters object, clean up put #131

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 7 additions & 34 deletions src/controllers/rest/impl/FileUploadController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, Inject } from "@tsed/di";
import { Delete, Description, Example, Examples, Get, Name, Patch, Pattern, Put, Returns, Summary } from "@tsed/schema";
import { Delete, Description, Example, Get, Name, Patch, Put, Returns, Summary } from "@tsed/schema";
import { StatusCodes } from "http-status-codes";
import { FileUploadResponseDto } from "../../../model/dto/FileUploadResponseDto.js";
import { BadRequest } from "@tsed/exceptions";
Expand All @@ -12,6 +12,7 @@ import { Logger } from "@tsed/logger";
import { EntryModificationDto } from "../../../model/dto/EntryModificationDto.js";
import type { Request, Response } from "express";
import { DefaultRenderException } from "../../../model/rest/DefaultRenderException.js";
import { FileUploadParameters } from "../../../model/rest/FileUploadParameters.js";

@Controller("/")
@Description("This is the API documentation for uploading and sharing files.")
Expand Down Expand Up @@ -43,37 +44,9 @@ export class FileUploadController extends BaseRestController {
public async addEntry(
@Req() req: Request,
@Res() res: Response,
@QueryParams("expires")
@Examples({
empty: {
summary: "empty",
description: "expires according to retention policy",
value: "",
},
"1d": {
summary: "1d",
description: "expires in 1day",
value: "1d",
},
})
VictoriqueMoe marked this conversation as resolved.
Show resolved Hide resolved
@Description(
"a string containing a number and a letter of `m` for mins, `h` for hours, `d` for days. For example: `1h` would be 1 hour and `1d` would be 1 day. leave this blank if you want the file to exist according to the retention policy",
)
@Pattern(/^$|^\d+[mhd]$/)
customExpiry?: string,
@QueryParams("hide_filename")
@Description(
"if set to true, then your filename will not appear in the URL. if false, then it will appear in the URL. defaults to false",
)
hideFileName?: boolean,
@QueryParams("password")
@Description(
"Set a password for this file, this will encrypt the file on the server that not even the server owner can obtain it, when fetching the file. you can fill out the `x-password` http header with your password to obtain the file via API",
)
password?: string,
@QueryParams() params: FileUploadParameters,
@MultipartFile("file") file?: PlatformMulterFile,
@BodyParams("url") url?: string,
@QueryParams("secret_token") @Description("Shh, it's a secret ;)") secretToken?: string,
): Promise<unknown> {
if (file && url) {
if (file) {
Expand All @@ -91,10 +64,10 @@ export class FileUploadController extends BaseRestController {
[uploadModelResponse, alreadyExists] = await this.fileUploadService.processUpload(
ip,
url || file!,
customExpiry,
hideFileName,
password,
secretToken,
params.expires,
params.hide_filename,
params.password,
params.secret_token,
);
} catch (e) {
this.logger.error(e.message);
Expand Down
32 changes: 32 additions & 0 deletions src/model/rest/FileUploadParameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Description, Name, Optional, Pattern, Property } from "@tsed/schema";

@Name("WaifuUploadParameters")
@Description("Upload parameters for put requests")
export class FileUploadParameters {
@Description(
"a string containing a number and a letter of `m` for mins, `h` for hours, `d` for days. For example: `1h` would be 1 hour and `1d` would be 1 day. leave this blank if you want the file to exist according to the retention policy",
)
@Pattern(/^$|^\d+[mhd]$/)
@Optional()
@Property()
public expires?: string;

@Description(
"if set to true, then your filename will not appear in the URL. if false, then it will appear in the URL. defaults to false",
)
@Optional()
@Property()
public hide_filename?: boolean;

@Description(
"Set a password for this file, this will encrypt the file on the server that not even the server owner can obtain it, when fetching the file. you can fill out the `x-password` http header with your password to obtain the file via API",
)
@Optional()
@Property()
public password?: string;

@Description("Shh, it's a secret ;)")
@Optional()
@Property()
public secret_token?: string;
}