diff --git a/config/production.json b/config/production.json index 8eb3027..daaa167 100644 --- a/config/production.json +++ b/config/production.json @@ -4,8 +4,5 @@ "rcFile": "./.codexdocsrc", "uploads": "/uploads", "secret": "iamasecretstring", - "favicon": { - "destination": "", - "type": "" - } + "faviconURL": "" } diff --git a/src/backend/app.ts b/src/backend/app.ts index 2ef12b4..2b38ab3 100644 --- a/src/backend/app.ts +++ b/src/backend/app.ts @@ -8,17 +8,32 @@ import HttpException from './exceptions/httpException'; import * as dotenv from 'dotenv'; import config from 'config'; import os from 'os'; +import appConfig from 'config'; +import { uploadFavicon } from './utils/uploadFavicon'; dotenv.config(); const app = express(); const localConfig = rcParser.getConfiguration(); +// Get url to upload favicon from config +const faviconURL: string = appConfig.get('faviconURL'); + app.locals.config = localConfig; // view engine setup app.set('views', path.join(__dirname, './', 'views')); app.set('view engine', 'twig'); require('./utils/twig'); +// Upload favicon by url, it's path on server is '/temp/favicon.{format}' +uploadFavicon(faviconURL).then((res) => { + app.locals.favicon = res; + console.log('Favicon successfully uploaded'); +}) + .catch( (err) => { + console.log(err); + console.log('Favicon has not uploaded'); + }); + app.use(morgan('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: true })); diff --git a/src/backend/routes/aliases.ts b/src/backend/routes/aliases.ts index 1dc2195..01614af 100644 --- a/src/backend/routes/aliases.ts +++ b/src/backend/routes/aliases.ts @@ -3,6 +3,7 @@ import Aliases from '../controllers/aliases'; import Pages from '../controllers/pages'; import Alias from '../models/alias'; import verifyToken from './middlewares/token'; +import app from '../app'; const router = express.Router(); @@ -36,6 +37,7 @@ router.get('*', verifyToken, async (req: Request, res: Response) => { page, pageParent, config: req.app.locals.config, + favicon: app.locals.favicon, }); } } diff --git a/src/backend/routes/auth.ts b/src/backend/routes/auth.ts index 8ee3a66..e647930 100644 --- a/src/backend/routes/auth.ts +++ b/src/backend/routes/auth.ts @@ -2,6 +2,7 @@ import express, { Request, Response } from 'express'; import jwt from 'jsonwebtoken'; import config from 'config'; import csrf from 'csurf'; +import app from '../app'; const router = express.Router(); const csrfProtection = csrf({ cookie: true }); @@ -14,6 +15,7 @@ router.get('/auth', csrfProtection, function (req: Request, res: Response) { res.render('auth', { title: 'Login page', csrfToken: req.csrfToken(), + favicon: app.locals.favicon, }); }); @@ -27,6 +29,7 @@ router.post('/auth', parseForm, csrfProtection, async (req: Request, res: Respon title: 'Login page', header: 'Password not set', csrfToken: req.csrfToken(), + favicon: app.locals.favicon, }); return; @@ -37,6 +40,7 @@ router.post('/auth', parseForm, csrfProtection, async (req: Request, res: Respon title: 'Login page', header: 'Wrong password', csrfToken: req.csrfToken(), + favicon: app.locals.favicon, }); return; @@ -59,6 +63,7 @@ router.post('/auth', parseForm, csrfProtection, async (req: Request, res: Respon title: 'Login page', header: 'Password not set', csrfToken: req.csrfToken(), + favicon: app.locals.favicon }); return; diff --git a/src/backend/routes/home.ts b/src/backend/routes/home.ts index 62172b2..a1824b8 100644 --- a/src/backend/routes/home.ts +++ b/src/backend/routes/home.ts @@ -1,25 +1,9 @@ import express, { Request, Response } from 'express'; import verifyToken from './middlewares/token'; -import appConfig from 'config'; -import uploadFavicon from '../utils/uploadFavicon'; +import app from '../app'; const router = express.Router(); -// Get url to upload favicon from config -const faviconURL: string = appConfig.get('faviconURL'); - -let fileFormat: string; - -// Upload favicon by url, it's path on server is '/temp/favicon.{format}' -uploadFavicon(faviconURL).then((res) => { - fileFormat = res; - console.log('Favicon successfully uploaded'); -}) - .catch( (err) => { - console.log(err); - console.log('Favicon has not uploaded'); - }); - /* GET home page. */ router.get('/', verifyToken, async (req: Request, res: Response) => { const config = req.app.locals.config; @@ -28,8 +12,7 @@ router.get('/', verifyToken, async (req: Request, res: Response) => { return res.redirect(config.startPage); } res.render('pages/index', { isAuthorized: res.locals.isAuthorized, - faviconFormat: `image/${fileFormat}`, - faviconRoute: `favicon/favicon.${fileFormat}` }); + favicon: app.locals.favicon }); }); export default router; diff --git a/src/backend/routes/pages.ts b/src/backend/routes/pages.ts index 82c8107..86b80f6 100644 --- a/src/backend/routes/pages.ts +++ b/src/backend/routes/pages.ts @@ -3,6 +3,7 @@ import Pages from '../controllers/pages'; import PagesOrder from '../controllers/pagesOrder'; import verifyToken from './middlewares/token'; import allowEdit from './middlewares/locals'; +import app from '../app'; const router = express.Router(); @@ -16,6 +17,7 @@ router.get('/page/new', verifyToken, allowEdit, async (req: Request, res: Respon res.render('pages/form', { pagesAvailable, page: null, + favicon: app.locals.favicon, }); } catch (error) { res.status(404); @@ -43,6 +45,7 @@ router.get('/page/edit/:id', verifyToken, allowEdit, async (req: Request, res: R page, parentsChildrenOrdered, pagesAvailable, + favicon: app.locals.favicon, }); } catch (error) { res.status(404); @@ -65,6 +68,7 @@ router.get('/page/:id', verifyToken, async (req: Request, res: Response, next: N page, pageParent, config: req.app.locals.config, + favicon: app.locals.favicon, }); } catch (error) { res.status(404); diff --git a/src/backend/utils/uploadFavicon.ts b/src/backend/utils/uploadFavicon.ts index 3e26dbd..dabde1b 100644 --- a/src/backend/utils/uploadFavicon.ts +++ b/src/backend/utils/uploadFavicon.ts @@ -2,15 +2,18 @@ import path from 'path'; import os from 'os'; import fs from 'fs'; import fetch from 'node-fetch'; -import config from 'config'; +/** + * Uploaded favicon data + */ interface FaviconData { + // Uploaded favicon path destination: string; + + // File type type: string; } -const favicon = config.get('favicon'); - /** * Check if string is url * @@ -25,30 +28,37 @@ function checkIsUrl(str: string): boolean { /** * Upload favicon by url * - * @param url - url for uploading favicon + * @param destination - url for uploading favicon * @returns { Promise } - Promise with format of saved file */ -async function uploadFavicon(url: string): Promise { - // Check if string is url - if (!checkIsUrl(url)) { - return url; +export async function uploadFavicon(destination: string): Promise { + // Check of destination is empty + if (!destination) { + throw Error('Favicon destination is empty'); } - // Make get request to url - const res = await fetch(url); - // Get buffer data from response - const fileData = await res.buffer(); - // Get file name by url - const filename = url.substring(url.lastIndexOf('/')+1); + // Get file name by destination + const filename = destination.substring(destination.lastIndexOf('/')+1); // Get file format const format = filename.split('.')[1]; + // Check if string is url + if (!checkIsUrl(destination)) { + return { destination: destination, + type: `image/${format}` } as FaviconData; + } + // Make get request to url + const res = await fetch(destination); + // Get buffer data from response + const fileData = await res.buffer(); + // Get file path in temporary directory const filePath = path.join(os.tmpdir(), `favicon.${format}`); // Save file fs.writeFileSync(filePath, fileData); - return `/favicon/favicon.${format}`; + return { destination: `/favicon/favicon.${format}`, + type: `image/${format}` } as FaviconData; } diff --git a/src/backend/views/layout.twig b/src/backend/views/layout.twig index c4df9c0..63b92d3 100644 --- a/src/backend/views/layout.twig +++ b/src/backend/views/layout.twig @@ -7,7 +7,7 @@ - +