Skip to content

Commit 77f1107

Browse files
committed
style: sonarqube fixes
1 parent ed51710 commit 77f1107

File tree

7 files changed

+49
-34
lines changed

7 files changed

+49
-34
lines changed

docker/ARM64.Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ FROM base AS release
2222
COPY --from=install /temp/prod/node_modules node_modules
2323
COPY --from=prerelease /bun/dist ./dist
2424

25-
RUN mkdir -p /bun/src/image/assets/images
26-
RUN chown -R bun:bun /bun/src/image/assets/images
27-
RUN chmod -R 600 /bun/src/image/assets/images
25+
RUN mkdir -p /bun/src/image/assets/images && \
26+
chown -R bun:bun /bun/src/image/assets/images && \
27+
chmod -R 600 /bun/src/image/assets/images
2828

2929
USER bun
3030
EXPOSE 4000

docker/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ FROM base AS release
2222
COPY --from=install /temp/prod/node_modules node_modules
2323
COPY --from=prerelease /bun/dist ./dist
2424

25-
RUN mkdir -p /bun/src/image/assets/images
26-
RUN chown -R bun:bun /bun/src/image/assets/images
27-
RUN chmod -R 600 /bun/src/image/assets/images
25+
RUN mkdir -p /bun/src/image/assets/images && \
26+
chown -R bun:bun /bun/src/image/assets/images && \
27+
chmod -R 600 /bun/src/image/assets/images
2828

2929
USER bun
3030
EXPOSE 4000

src/image/image-controller.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,62 @@
11
// External Modules
22
import { Request, Response } from 'express';
3+
import { LogArgument } from 'rollbar';
34

45
// Interal Modules
5-
import clearTemporaryFiles from '../common/utils/clear';
6+
import clearTemporaryFiles from 'src/common/utils/clear';
67
import ImageService from './image-service';
7-
import { Query } from '../common/interfaces/types';
8+
import { Query } from 'src/common/interfaces/types';
9+
import { rollbar } from 'src/app/config/rollbar';
810

911
class ImageController {
1012
/**
11-
* @description Upload a file to cloudinary then saves the url on mongodb
12-
* @param { Request } req.body - tag, source, is_nsfw
13-
* @param { Request } file.path - path to the file
14-
* @returns { Promise<Response> } A success message with a Json response format
13+
* @description Uploads a file to Cloudinary and saves the URL in MongoDB
14+
* @param { Request } req - The request object containing the file and additional data
15+
* @returns { Promise<Response> } A JSON response with a success message
1516
*/
1617
async uploadFile(req: Request, res: Response): Promise<Response> {
1718
try {
1819
const { file } = req;
1920
const { tag, source, is_nsfw } = req.body;
20-
const response = await ImageService.cloudinaryUpload(file?.path);
21-
const { public_id, secure_url } = response;
22-
ImageService.upload({
21+
22+
if (!file) {
23+
return res.status(400).json({ error: 'File is required' });
24+
}
25+
26+
const uploadResult = await ImageService.cloudinaryUpload(file.path);
27+
const { public_id, secure_url } = uploadResult;
28+
29+
await ImageService.upload({
2330
source,
2431
is_nsfw,
2532
id: public_id,
2633
url: secure_url,
2734
tag,
2835
});
29-
clearTemporaryFiles(file?.path ?? 'image/assets/images');
30-
return res.json({ url: 'Imagen guardada correctamente' });
36+
37+
clearTemporaryFiles(file.path ?? 'image/assets/images');
38+
return res.json({ message: 'Image saved successfully', url: secure_url });
3139
} catch (error: unknown) {
32-
return res.json({ message: (<Error>error).message });
40+
rollbar.error(error as LogArgument);
41+
return res.status(500).json({ error: 'An error occurred while uploading the image' });
3342
}
3443
}
3544

3645
/**
3746
* @description Get a random waifu from the collection!
38-
* @param { Response } res object with the waifu
47+
* @param { Response } res - object with the waifu
3948
* @query size - number of items to retrieve
4049
* @query tag_id - tag to filter
4150
* @returns { Promise<Response> } An url with the waifu image hosted in cloudinary
4251
*/
4352
async getRandomImage(req: Request, res: Response): Promise<Response> {
4453
try {
45-
const { size, tag_id } = req.query as unknown as Query;
46-
const getImages = await ImageService.getImage(size, tag_id);
47-
return res.json(getImages);
48-
} catch {
49-
return res.json({ message: 'No se pudo encontrar alguna imagen' });
54+
const { size, tag_id } = req.query as Query;
55+
const images = await ImageService.getImage(size, tag_id);
56+
return res.json(images);
57+
} catch (error: unknown) {
58+
rollbar.error(error as LogArgument);
59+
return res.status(500).json({ error: 'An error occurred while getting the image' });
5060
}
5161
}
5262

@@ -58,11 +68,12 @@ class ImageController {
5868
*/
5969
async getImages(req: Request, res: Response): Promise<Response> {
6070
try {
61-
const { tag_id } = req.query as unknown as Query;
71+
const { tag_id } = req.query as Query;
6272
const images = await ImageService.getAllImages(tag_id);
6373
return res.json(images);
6474
} catch (error: unknown) {
65-
return res.json({ message: (<Error>error).message });
75+
rollbar.error(error as LogArgument);
76+
return res.json({ error });
6677
}
6778
}
6879
}

src/image/image-repository.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
import Image from './schema/image-schema';
33
import Tag from '../tag/schema/tag-schema';
44
import { hasTag } from '../common/utils/ref';
5-
import { IImage } from './interfaces/image-interface';
5+
import { ImageProp } from './interfaces/image-interface';
66

77
class ImageRepository {
88
/**
99
* @description Creates a new Image in the database
10-
* @param {IImage} image - the image to create in the database
11-
* @return { Promise<IImage> } - A new image created
10+
* @param { ImageProp } image - the image to create in the database
11+
* @return { Promise<ImageProp> } - A new image created
1212
*/
13-
async create(image: IImage): Promise<IImage> {
13+
async create(image: ImageProp): Promise<ImageProp> {
1414
const tagExists = await Tag.findOne({ tag_id: { $eq: image.tag } });
1515
const _idTag = tagExists?._id;
1616
return Image.create({
@@ -22,9 +22,9 @@ class ImageRepository {
2222
/**
2323
* @description Get all images from the database
2424
* @param tag_id - the id from the tag to retrieve
25-
* @return { Promise<IImage[]> } An array of images
25+
* @return { Promise<ImageProp[]> } An array of images
2626
*/
27-
async findImages(tag_id?: number): Promise<IImage[]> {
27+
async findImages(tag_id?: number): Promise<ImageProp[]> {
2828
const images = await Image.find().populate(hasTag(tag_id));
2929
return images.filter(image => image.tag !== null);
3030
}

src/image/image-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class ImageService {
8080
tag: image.tag,
8181
};
8282
});
83-
const randomArray = urls.sort(randomUrls);
83+
const randomArray = urls.toSorted(randomUrls);
8484
const sizeArray = randomArray.slice(0, size);
8585
const randomUrl = urls[Math.floor(Math.random() * urls.length)];
8686
return size === undefined || null ? randomUrl : sizeArray;

src/image/interfaces/image-interface.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,3 @@ export interface ImageTypeResponse extends ImageType {
2020
}
2121

2222
export type ImageProp = Omit<ImageProps, "save">;
23-
export type IImage = ImageProp;

src/user/interfaces/user-interface.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { Boom } from "@hapi/boom";
2+
import { NextFunction, Response } from "express";
3+
14
export interface UsernameType {
25
username: string;
36
}
@@ -22,3 +25,5 @@ export type UserPicture = Omit<
2225
IUser,
2326
"username" | "password" | "save" | "isAdmin"
2427
>;
28+
29+
export type MiddlewareUser = Boom | NextFunction | Response | void;

0 commit comments

Comments
 (0)