Skip to content

Commit

Permalink
fix(api): fix uploaded images being over max field length
Browse files Browse the repository at this point in the history
  • Loading branch information
hwgilbert16 committed Mar 23, 2024
1 parent ce10d04 commit 51a4a5e
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 36 deletions.
2 changes: 1 addition & 1 deletion apps/api/src/app/cards/cards.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class CardsService {
await this.storageService.getInstance()
.putFile("media/sets/" + fileName, decoded);

side = side.replace(source, "/api/sets/" + setId + "/media/" + fileName);
side = side.replace(source, "/api/sets/" + setId + "/media/" + name);
}
} else return false;

Expand Down
10 changes: 3 additions & 7 deletions apps/api/src/app/cards/dto/createCard.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsNotEmpty, IsNumber, IsOptional, IsString, IsUUID, Max, MaxLength, Min } from "class-validator";
import { IsNotEmpty, IsNumber, IsOptional, IsString, IsUUID, Max, Min } from "class-validator";
import { ApiProperty } from "@nestjs/swagger";
import { Transform, TransformFnParams } from "class-transformer";
import * as sanitizeHtml from "sanitize-html";
Expand Down Expand Up @@ -28,12 +28,10 @@ export class CreateCardDto {

@ApiProperty({
description: "The front or \"term\" of the card",
example: "The definition of the card",
maxLength: 65535
example: "The definition of the card"
})
@IsString()
@IsNotEmpty()
@MaxLength(65535)
@Transform((params: TransformFnParams) => sanitizeHtml(params.value, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]),
allowedAttributes: { "img": ["src", "width", "height"], "span": ["style"] },
Expand All @@ -43,12 +41,10 @@ export class CreateCardDto {

@ApiProperty({
description: "The back or \"definition\" of the card",
example: "The definition of the card",
maxLength: 65535
example: "The definition of the card"
})
@IsString()
@IsNotEmpty()
@MaxLength(65535)
@Transform((params: TransformFnParams) => sanitizeHtml(params.value, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]),
allowedAttributes: { "img": ["src", "width", "height"], "span": ["style"] },
Expand Down
6 changes: 1 addition & 5 deletions apps/api/src/app/cards/dto/updateCard.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsNotEmpty, IsNumber, IsOptional, IsString, Max, MaxLength, Min } from "class-validator";
import { IsNotEmpty, IsNumber, IsOptional, IsString, Max, Min } from "class-validator";
import { ApiProperty } from "@nestjs/swagger";
import { Transform, TransformFnParams } from "class-transformer";
import * as sanitizeHtml from "sanitize-html";
Expand All @@ -21,13 +21,11 @@ export class UpdateCardDto {
@ApiProperty({
description: "The front or \"term\" of the card",
example: "The definition of the card",
maxLength: 65535,
required: false
})
@IsString()
@IsOptional()
@IsNotEmpty()
@MaxLength(65535)
@Transform((params: TransformFnParams) => sanitizeHtml(params.value, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]),
allowedAttributes: { "img": ["src", "width", "height"], "span": ["style"] },
Expand All @@ -38,13 +36,11 @@ export class UpdateCardDto {
@ApiProperty({
description: "The back or \"definition\" of the card",
example: "The definition of the card",
maxLength: 65535,
required: false
})
@IsString()
@IsOptional()
@IsNotEmpty()
@MaxLength(65535)
@Transform((params: TransformFnParams) => sanitizeHtml(params.value, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]),
allowedAttributes: { "img": ["src", "width", "height"], "span": ["style"] },
Expand Down
2 changes: 0 additions & 2 deletions apps/api/src/app/folders/folders.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ export class FoldersController {
@UseGuards(AuthenticatedGuard)
@Post()
async createFolder(@Body(HtmlDecodePipe) body: CreateFolderDto, @Request() req: ExpressRequest): Promise<ApiResponse<Folder>> {
console.log(body);

const user = await this.authService.getUserInfo(req);
if (!user) {
throw new UnauthorizedException({
Expand Down
6 changes: 0 additions & 6 deletions apps/api/src/app/providers/storage/provider/local.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ export class LocalStorageProvider implements StorageProvider {
throw new Error(`the path provided is not a directory: "${dirPath}"`);
}

for (const entity of await fs.promises.readdir(dirPath)) {
if (await this.isDirectory(nodePath.join(dirPath, entity))) {
throw new Error(`directory "${dirPath}" contains subdirectories.`);
}
}

await fs.promises.rm(dirPath, { recursive: true, force: true });
}

Expand Down
10 changes: 3 additions & 7 deletions apps/api/src/app/sets/validator/card.validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsNotEmpty, IsNumber, IsString, Max, MaxLength, Min } from "class-validator";
import { IsNotEmpty, IsNumber, IsString, Max, Min } from "class-validator";
import { ApiProperty } from "@nestjs/swagger";
import { Transform, TransformFnParams } from "class-transformer";
import * as sanitizeHtml from "sanitize-html";
Expand All @@ -18,12 +18,10 @@ export class CardValidator {

@ApiProperty({
description: "The front or \"term\" of the card",
example: "The term of the card",
maxLength: 65535
example: "The term of the card"
})
@IsString()
@IsNotEmpty()
@MaxLength(65535)
@Transform((params: TransformFnParams) => sanitizeHtml(params.value, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]),
allowedAttributes: { "img": ["src", "width", "height"], "span": ["style"] },
Expand All @@ -33,12 +31,10 @@ export class CardValidator {

@ApiProperty({
description: "The back or \"definition\" of the card",
example: "The definition of the card",
maxLength: 65535
example: "The definition of the card"
})
@IsString()
@IsNotEmpty()
@MaxLength(65535)
@Transform((params: TransformFnParams) => sanitizeHtml(params.value, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]),
allowedAttributes: { "img": ["src", "width", "height"], "span": ["style"] },
Expand Down
12 changes: 4 additions & 8 deletions apps/api/src/app/sets/validator/cardWithId.validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsNotEmpty, IsNumber, IsOptional, IsString, Max, MaxLength, Min } from "class-validator";
import { IsNotEmpty, IsNumber, IsOptional, IsString, Max, Min } from "class-validator";
import { Transform, TransformFnParams } from "class-transformer";
import * as sanitizeHtml from "sanitize-html";
import { ApiProperty } from "@nestjs/swagger";
Expand All @@ -14,7 +14,7 @@ export class CardWithIdValidator {
minLength: 36
})
@IsOptional()
id: string;
id?: string;

@ApiProperty({
description: "The index of the card in the set",
Expand All @@ -30,12 +30,10 @@ export class CardWithIdValidator {

@ApiProperty({
description: "The front or \"term\" of the card",
example: "The term of the card",
maxLength: 65535
example: "The term of the card"
})
@IsString()
@IsNotEmpty()
@MaxLength(65535)
@Transform((params: TransformFnParams) => sanitizeHtml(params.value, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]),
allowedAttributes: { "img": ["src", "width", "height"], "span": ["style"] },
Expand All @@ -45,12 +43,10 @@ export class CardWithIdValidator {

@ApiProperty({
description: "The back or \"definition\" of the card",
example: "The definition of the card",
maxLength: 65535
example: "The definition of the card"
})
@IsString()
@IsNotEmpty()
@MaxLength(65535)
@Transform((params: TransformFnParams) => sanitizeHtml(params.value, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]),
allowedAttributes: { "img": ["src", "width", "height"], "span": ["style"] },
Expand Down

0 comments on commit 51a4a5e

Please sign in to comment.