Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #49 from Sebastian-Webster/25-image-post-image-doe…
Browse files Browse the repository at this point in the history
…s-not-get-deleted-when-deleting-image-post

25 image post image does not get deleted when deleting image post
  • Loading branch information
Sebastian-Webster authored Sep 10, 2022
2 parents ec90dea + 3978440 commit c020a5c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
31 changes: 23 additions & 8 deletions backend/controllers/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const TextPostLibrary = require('../libraries/TextPost');
const ImagePostLibrary = require('../libraries/ImagePost');
const RedisLibrary = require('../libraries/Redis');
const GeneralLibrary = require('../libraries/General');
const FilesystemLibrary = require('../libraries/Filesystem')
const {v4: uuidv4} = require('uuid');
const user = new UserLibrary();
const http = new HTTPHandler();
Expand All @@ -14,6 +15,7 @@ const TextPost = new TextPostLibrary();
const ImagePost = new ImagePostLibrary();
const redis = new RedisLibrary();
const generalLib = new GeneralLibrary();
const filesystem = new FilesystemLibrary();
const bcrypt = require('bcrypt');

const login = async (req, res) => {
Expand Down Expand Up @@ -686,6 +688,18 @@ const deleteImagePost = async (req, res) => {
return
}

const imagePost = await ImagePost.findPostById(postId);

if (imagePost === null) {
http.NotFound(res, 'Image post not found.')
return
}

if (imagePost.error) {
http.ServerError(res, 'An error occured while deleting image post. Please try again later.')
return
}

const isOwner = await ImagePost.checkIfUserIsPostOwner(userId, postId)

if (typeof isOwner === 'object' && isOwner.error) {
Expand All @@ -699,10 +713,13 @@ const deleteImagePost = async (req, res) => {
return
}

ImagePost.deletePostById(postId).then(() => {
Promise.all([
ImagePost.deletePostById(postId),
filesystem.deleteFileAsync(`/uploads/${imagePost.imageKey}`)
]).then(() => {
http.OK(res, 'Post successfully deleted.')
}).catch(error => {
http.ServerError(res, 'An error occured while deleting iamge post. Please try again later.')
http.ServerError(res, 'An error occured while deleting image post. Please try again later.')
logger.error(error)
})
}
Expand Down Expand Up @@ -739,13 +756,11 @@ const deleteTextPost = async (req, res) => {
return
}

TextPost.deletePostById(postId).then(() => {
Promise.all([
TextPost.deletePostById(postId),
redis.removeTextPostFromCache(userId, postId)
]).then(() => {
http.OK(res, 'Post successfully deleted.')
redis.removeTextPostFromCache(userId, postId).then(() => {
logger.log(`Successfully deleted post with ID: ${postId} from database and Redis cache`)
}).catch(error => {
logger.error(`Successfully deleted post with ID: ${postId} from database but ran into error while removing it from Redis cache: ${error}`)
})
}).catch(error => {
http.ServerError(res, 'An error occured while deleting the text post. Please try again later.')
logger.error(error)
Expand Down
14 changes: 14 additions & 0 deletions backend/libraries/Filesystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require('fs')

class Filesystem {
deleteFileAsync = (filepath) => {
return new Promise((resolve, reject) => {
fs.unlink(filepath, (err) => {
if (err) reject(err)
else resolve()
})
})
}
}

module.exports = Filesystem
2 changes: 1 addition & 1 deletion backend/libraries/Redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class RedisLibrary {
const cache = await this.getCache(redisCacheKey)
if (!Array.isArray(cache)) return resolve() //No need to throw an error because there is no cache to delete a post from
const postIndex = cache.findIndex(post => post._id === postId)
if (postIndex === -1) throw new Error('Post was not found in cache.')
if (postIndex === -1) return resolve() //No need to throw an error because there is no post to remove from cache. It is weird that it is not in the cache but we don't need to throw an error.
cache.splice(postIndex, 1)
this.setCache(redisCacheKey, cache).then(resolve)
} catch (error) {
Expand Down

0 comments on commit c020a5c

Please sign in to comment.