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

modify put to have password in body instead of query param #149

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 21 additions & 8 deletions src/controllers/rest/impl/FileUploadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +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";
import { FileUploadQueryParameters } from "../../../model/rest/FileUploadQueryParameters.js";

@Controller("/")
@Description("This is the API documentation for uploading and sharing files.")
Expand Down Expand Up @@ -61,9 +61,21 @@ export class FileUploadController extends BaseRestController {
},
},
})
params: FileUploadParameters,
@MultipartFile("file") file?: PlatformMulterFile,
@BodyParams("url") url?: string,
params: FileUploadQueryParameters,

@Description("The file you want to upload")
@MultipartFile("file")
file?: PlatformMulterFile,

@Description("The URL of the file you want to upload")
@BodyParams("url")
url?: string,

@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",
)
@BodyParams("password")
password?: string,
): Promise<unknown> {
if (file && url) {
if (file) {
Expand All @@ -79,12 +91,13 @@ export class FileUploadController extends BaseRestController {
let alreadyExists: boolean;
const secretToken = req.query["secret_token"]?.toString();
try {
[uploadModelResponse, alreadyExists] = await this.fileUploadService.processUpload(
[uploadModelResponse, alreadyExists] = await this.fileUploadService.processUpload({
ip,
url || file!,
params,
source: url || file!,
options: params,
password,
secretToken,
);
});
} catch (e) {
this.logger.error(e.message);
throw e;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Default, Description, Name, Optional, Pattern, Property } from "@tsed/schema";

@Name("WaifuUploadParameters")
@Description("Upload parameters for put requests")
export class FileUploadParameters {
export class FileUploadQueryParameters {
@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",
)
Expand All @@ -20,13 +18,6 @@ export class FileUploadParameters {
@Default(false)
public hideFilename?: 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("If this is true, then the file will be deleted as soon as it is accessed")
@Optional()
@Property()
Expand Down
28 changes: 17 additions & 11 deletions src/services/FileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import crypto from "node:crypto";
import { FileUploadResponseDto } from "../model/dto/FileUploadResponseDto.js";
import GlobalEnv from "../model/constants/GlobalEnv.js";
import { Logger } from "@tsed/logger";
import type { EntrySettings } from "../utils/typeings.js";
import { EntrySettings, fileUploadProps } from "../utils/typeings.js";
import { BadRequest, Exception, InternalServerError, NotFound, UnsupportedMediaType } from "@tsed/exceptions";
import { FileUtils, ObjectUtils } from "../utils/Utils.js";
import TimeUnit from "../model/constants/TimeUnit.js";
Expand All @@ -20,7 +20,7 @@ import { AvManager } from "../manager/AvManager.js";
import { EncryptionService } from "./EncryptionService.js";
import { RecordInfoSocket } from "./socket/RecordInfoSocket.js";
import { EntryModificationDto } from "../model/dto/EntryModificationDto.js";
import { FileUploadParameters } from "../model/rest/FileUploadParameters.js";
import { FileUploadQueryParameters } from "../model/rest/FileUploadQueryParameters.js";
import { ProcessUploadException } from "../model/exceptions/ProcessUploadException.js";

@Service()
Expand All @@ -41,13 +41,14 @@ export class FileService {
@Inject() private recordInfoSocket: RecordInfoSocket,
) {}

public async processUpload(
ip: string,
source: PlatformMulterFile | string,
options: FileUploadParameters,
secretToken?: string,
): Promise<[FileUploadResponseDto, boolean]> {
const { expires, password } = options;
public async processUpload({
ip,
source,
options,
password,
secretToken,
}: fileUploadProps): Promise<[FileUploadResponseDto, boolean]> {
const { expires } = options;
let resourcePath: string | undefined;
let originalFileName: string | undefined;
try {
Expand All @@ -72,7 +73,12 @@ export class FileService {
}
}

uploadEntry.settings(await this.buildEntrySettings(options));
uploadEntry.settings(
await this.buildEntrySettings({
password,
...options,
}),
);

const ext = FileUtils.getExtension(originalFileName);
if (ext) {
Expand Down Expand Up @@ -150,7 +156,7 @@ export class FileService {
password,
hideFilename,
one_time_download,
}: FileUploadParameters): Promise<EntrySettings | null> {
}: FileUploadQueryParameters & { password?: string }): Promise<EntrySettings | null> {
const retObj: EntrySettings = {};
if (password) {
retObj["password"] = await this.hashPassword(password);
Expand Down
10 changes: 10 additions & 0 deletions src/utils/typeings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Exception } from "@tsed/exceptions";
import { FileUploadModel } from "../model/db/FileUpload.model.js";
import type { PlatformMulterFile } from "@tsed/common";
import { FileUploadQueryParameters } from "../model/rest/FileUploadQueryParameters.js";

export type HttpErrorRenderObj<T extends Exception> = {
status: number;
Expand Down Expand Up @@ -68,3 +70,11 @@ export type RecordInfoPayload = {
recordCount: number;
recordSize: string;
};

export type fileUploadProps = {
ip: string;
source: PlatformMulterFile | string;
options: FileUploadQueryParameters;
password?: string;
secretToken?: string;
};