11// External Modules
22import { 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' ;
67import 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
911class 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}
0 commit comments