Skip to content

Commit c10efa0

Browse files
committed
clean background images when resetting
1 parent 3dc55f7 commit c10efa0

File tree

6 files changed

+42
-4
lines changed

6 files changed

+42
-4
lines changed

backend/src/routes/system.route.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,21 @@ systemRoute.post('/upload', upload.single('file'), (req: Request, res: Response,
8080
next(error);
8181
}
8282
});
83+
84+
// Clean up background images (removes all files at root level in uploads directory)
85+
systemRoute.post('/clean-background', (req: Request, res: Response) => {
86+
try {
87+
// Remove all files in the root of uploads directory
88+
removeExistingFiles();
89+
90+
res.status(StatusCodes.OK).json({
91+
message: 'Background images cleaned successfully'
92+
});
93+
} catch (error) {
94+
console.error('Error cleaning background images:', error);
95+
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
96+
message: 'Error cleaning background images',
97+
error: (error as Error).message
98+
});
99+
}
100+
});

backend/src/utils/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export const removeExistingFiles = (exceptFileName?: string) => {
1313
}
1414

1515
const filePath = path.join(UPLOAD_DIRECTORY, file);
16+
17+
// Skip directories
18+
if (fs.statSync(filePath).isDirectory()) {
19+
return;
20+
}
21+
1622
fs.unlinkSync(filePath);
1723
});
1824
} catch (error) {

frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lab-dash-frontend",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"private": true,
55
"scripts": {
66
"dev": "vite --host",

frontend/src/api/dash-api.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,16 @@ export class DashApi {
314314
}
315315
}
316316

317+
public static async cleanBackgroundImages(): Promise<boolean> {
318+
try {
319+
await axios.post(`${BACKEND_URL}/api/system/clean-background`);
320+
return true;
321+
} catch (error) {
322+
console.error('Failed to clean background images:', error);
323+
return false;
324+
}
325+
}
326+
317327
public static async uploadAppIcon(file: File): Promise<Icon | null> {
318328
try {
319329
const formData = new FormData();

frontend/src/components/forms/SettingsForm.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,17 @@ export const SettingsForm = () => {
326326
const resetBackground = async () => {
327327
PopupManager.deleteConfirmation({
328328
title: 'Reset Background',
329-
text: 'This will restore the default background.',
329+
text: 'This will restore the default background and remove all uploaded background images.',
330330
confirmText: 'Yes, Reset',
331331
confirmAction: async () => {
332332
try {
333+
// First clean up all background images in the root directory
334+
await DashApi.cleanBackgroundImages();
335+
336+
// Then update the config to use the default background
333337
await updateConfig({ backgroundImage: '' });
334-
PopupManager.success('Background has been reset');
335338

339+
PopupManager.success('Background has been reset');
336340
} catch (error) {
337341
PopupManager.failure('Failed to reset background. Please try again.');
338342
console.error('Error resetting background:', error);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lab-dash",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "This is an open-source user interface designed to manage your server and homelab",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)