Skip to content

Commit 24a9ee8

Browse files
authored
Merge pull request #63 from CoolerMinecraft/stuff
deletion of old images
2 parents 552e31b + 54d1a70 commit 24a9ee8

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

server/routes/admin/updateModals.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
unpublishUpdateModal
1313
} from '../../db/updateModals.js';
1414
import { getClientIp } from '../../utils/getIpAddress.js';
15+
import { deleteOldImage } from '../uploads.js';
1516

1617
const router = express.Router();
1718

@@ -91,8 +92,21 @@ router.put('/:id', async (req, res) => {
9192
return res.status(400).json({ error: 'Invalid modal ID' });
9293
}
9394

95+
const currentModal = await getUpdateModalById(numericId);
96+
if (!currentModal) {
97+
return res.status(404).json({ error: 'Modal not found' });
98+
}
99+
94100
const modal = await updateUpdateModal(numericId, { title, content, banner_url });
95101

102+
if (currentModal.banner_url && currentModal.banner_url !== modal?.banner_url) {
103+
try {
104+
await deleteOldImage(currentModal.banner_url);
105+
} catch (deleteError) {
106+
console.error('Error deleting old modal banner:', deleteError);
107+
}
108+
}
109+
96110
if (req.user?.userId) {
97111
const ip = getClientIp(req);
98112
await logAdminAction({
@@ -121,8 +135,21 @@ router.delete('/:id', async (req, res) => {
121135
return res.status(400).json({ error: 'Invalid modal ID' });
122136
}
123137

138+
const modal = await getUpdateModalById(numericId);
139+
if (!modal) {
140+
return res.status(404).json({ error: 'Modal not found' });
141+
}
142+
124143
await deleteUpdateModal(numericId);
125144

145+
if (modal.banner_url) {
146+
try {
147+
await deleteOldImage(modal.banner_url);
148+
} catch (deleteError) {
149+
console.error('Error deleting modal banner during deletion:', deleteError);
150+
}
151+
}
152+
126153
if (req.user?.userId) {
127154
const ip = getClientIp(req);
128155
await logAdminAction({

server/routes/uploads.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const CEPHIE_API_KEY = process.env.CEPHIE_API_KEY;
1313
const CEPHIE_UPLOAD_URL = 'https://api.cephie.app/api/v1/pfcontrol/upload';
1414
const CEPHIE_DELETE_URL = 'https://api.cephie.app/api/v1/pfcontrol/delete';
1515

16-
async function deleteOldImage(url: string | undefined) {
16+
export async function deleteOldImage(url: string | undefined) {
1717
if (!url) return;
1818
try {
1919
const response = await axios.delete(CEPHIE_DELETE_URL, {

0 commit comments

Comments
 (0)