Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 7x 7x 3x 3x 1x 2x 2x 7x | import { Request, Response } from 'express';
import { config } from '../config/config';
import fs from 'fs';
import path from 'path';
import { uploadImage } from '../services/resources_service';
import multer from 'multer';
import {CustomRequest} from "types/customRequest";
import {updateUserById} from "../services/users_service";
import {handleError} from "../utils/handle_error";
const createUserImageResource = async (req: CustomRequest, res: Response) => {
try {
const imageFilename = await uploadImage(req);
const updatedUser = updateUserById(req.user.id, {imageFilename});
return res.status(201).send(updatedUser);
} catch (error) {
handleError(error, res);
}
};
const createImageResource = async (req: Request, res: Response) => {
try {
const imageFilename = await uploadImage(req);
return res.status(201).send(imageFilename);
} catch (error) {
if (error instanceof multer.MulterError || error instanceof TypeError) {
return res.status(400).send(error.message);
} else E{
handleError(error, res);
}
}
};
const getImageResource = async (req: Request, res: Response) => {
try {
const { filename } = req.params;
const imagePath = path.resolve(config.resources.imagesDirectoryPath(), filename);
if (!fs.existsSync(imagePath)) {
return res.status(404).send('Image not found');
}
res.sendFile(imagePath);
} catch (error) {
handleError(error, res);
}
};
export default { createUserImageResource, createImageResource, getImageResource }; |