mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-11 02:13:48 +00:00
Upload corporate logo (#1314)
* feat: upload logo to project data --------- Co-authored-by: Carlos Valente <carlosvalente@pm.me>
This commit is contained in:
@@ -2,12 +2,14 @@ import { ErrorResponse, ProjectData } from 'ontime-types';
|
||||
import { getErrorMessage } from 'ontime-utils';
|
||||
|
||||
import type { Request, Response } from 'express';
|
||||
import { join } from 'path';
|
||||
|
||||
import { removeUndefined } from '../../utils/parserUtils.js';
|
||||
import { deleteFile, removeUndefined } from '../../utils/parserUtils.js';
|
||||
import { failEmptyObjects } from '../../utils/routerUtils.js';
|
||||
import { getDataProvider } from '../../classes/data-provider/DataProvider.js';
|
||||
import { publicDir } from '../../setup/index.js';
|
||||
|
||||
export async function getProjectData(_req: Request, res: Response<ProjectData>) {
|
||||
export function getProjectData(_req: Request, res: Response<ProjectData>) {
|
||||
res.json(getDataProvider().getProjectData());
|
||||
}
|
||||
|
||||
@@ -17,6 +19,8 @@ export async function postProjectData(req: Request, res: Response<ProjectData |
|
||||
}
|
||||
|
||||
try {
|
||||
const currentProjectData = getDataProvider().getProjectData();
|
||||
|
||||
const newEvent: Partial<ProjectData> = removeUndefined({
|
||||
title: req.body?.title,
|
||||
description: req.body?.description,
|
||||
@@ -25,8 +29,20 @@ export async function postProjectData(req: Request, res: Response<ProjectData |
|
||||
backstageUrl: req.body?.backstageUrl,
|
||||
backstageInfo: req.body?.backstageInfo,
|
||||
endMessage: req.body?.endMessage,
|
||||
projectLogo: req.body?.projectLogo,
|
||||
});
|
||||
|
||||
const newData = await getDataProvider().setProjectData(newEvent);
|
||||
|
||||
// Delete the old logo if the new logo is empty
|
||||
if (!newData.projectLogo && currentProjectData.projectLogo) {
|
||||
const filePath = join(publicDir.logoDir, currentProjectData.projectLogo);
|
||||
|
||||
deleteFile(filePath).catch((_error) => {
|
||||
/** we do not handle this error */
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).send(newData);
|
||||
} catch (error) {
|
||||
const message = getErrorMessage(error);
|
||||
|
||||
@@ -2,8 +2,11 @@ import express from 'express';
|
||||
|
||||
import { getProjectData, postProjectData } from './project.controller.js';
|
||||
import { projectSanitiser } from './project.validation.js';
|
||||
import { uploadImageFile } from '../db/db.middleware.js';
|
||||
import { postProjectLogo } from '../db/db.controller.js';
|
||||
|
||||
export const router = express.Router();
|
||||
|
||||
router.get('/', getProjectData);
|
||||
router.post('/', projectSanitiser, postProjectData);
|
||||
router.post('/upload', uploadImageFile, postProjectLogo);
|
||||
|
||||
@@ -9,6 +9,7 @@ export const projectSanitiser = [
|
||||
body('backstageUrl').optional().isString().trim(),
|
||||
body('backstageInfo').optional().isString().trim(),
|
||||
body('endMessage').optional().isString().trim(),
|
||||
body('projectLogo').optional({ nullable: true }).isString().trim(),
|
||||
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
const errors = validationResult(req);
|
||||
|
||||
Reference in New Issue
Block a user