Skip to content

Commit

Permalink
Merge pull request #151 from dali-lab/add-blog-endpoint
Browse files Browse the repository at this point in the history
feat: add path parser for images in blog
  • Loading branch information
wu-ciesielska authored Nov 24, 2023
2 parents a082508 + 488e27e commit 9a3272f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/controllers/blog.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mongoose from 'mongoose';
import { RESPONSE_CODES } from '../constants';
import { Blog } from '../models';
import { getFileUrl } from '../utils/upload-file';

/**
* @description retrieves blog post object
Expand Down Expand Up @@ -60,11 +61,13 @@ export const createBlogPost = async (fields, uploadedFile, user) => {

const { first_name: firstName, last_name: lastName, _id: id } = user;

const imagePath = getFileUrl(uploadedFile?.path);

post.title = title;
post.body = body;
post.author = `${firstName} ${lastName}`;
post.authorId = id;
post.image = uploadedFile?.path || null;
post.image = imagePath;

try {
const savedPost = await post.save();
Expand Down
2 changes: 1 addition & 1 deletion src/routers/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { queryFetch } from '../utils';
import { Blog, User } from '../controllers';
import { requireAuth } from '../middleware';
import uploadFile from '../utils/upload-file';
import { uploadFile } from '../utils/upload-file';

const blogRouter = Router();

Expand Down
11 changes: 10 additions & 1 deletion src/utils/upload-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import multer from 'multer';

const uploadFilePath = path.resolve(__dirname, '../..', 'public/uploads');

const getFileUrl = (originalUrl) => {
if (originalUrl) {
const url = new URL(originalUrl);
url.pathname = url.pathname.replace('/app/public', '');
return url.toString();
}
return null;
};

const storageFile = multer.diskStorage({
destination: uploadFilePath,
filename: (_req, file, fn) => {
Expand Down Expand Up @@ -44,4 +53,4 @@ const uploadFile = multer({
},
}).single('image');

export default uploadFile;
export { uploadFile, getFileUrl };

0 comments on commit 9a3272f

Please sign in to comment.