mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-07 06:25:21 +02:00
Favicon data saves to app.locals, replaced uploading favicon to app.ts
This commit is contained in:
parent
00b7fc6d41
commit
3cab15e514
9 changed files with 56 additions and 40 deletions
|
@ -4,8 +4,5 @@
|
|||
"rcFile": "./.codexdocsrc",
|
||||
"uploads": "/uploads",
|
||||
"secret": "iamasecretstring",
|
||||
"favicon": {
|
||||
"destination": "",
|
||||
"type": ""
|
||||
}
|
||||
"faviconURL": ""
|
||||
}
|
||||
|
|
|
@ -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 }));
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<FaviconData>('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<string> } - Promise with format of saved file
|
||||
*/
|
||||
async function uploadFavicon(url: string): Promise<string> {
|
||||
// Check if string is url
|
||||
if (!checkIsUrl(url)) {
|
||||
return url;
|
||||
export async function uploadFavicon(destination: string): Promise<FaviconData> {
|
||||
// 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;
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<meta property="og:title" content="{{ page.title | striptags }}" />
|
||||
<meta property="article:modified_time" content="{{ (page.body.time / 1000) | date("c") }}" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
|
||||
<link rel="icon" type="image/png" href="/favicon">
|
||||
<link rel="icon" type="{{ favicon.type }}" href="{{ favicon.destination }}">
|
||||
</head>
|
||||
<script>
|
||||
window.config = {
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<title>{{ config.title }}</title>
|
||||
<link rel="stylesheet" href="/dist/main.css" />
|
||||
<link rel="preload" href="{{ config.landingFrameSrc }}" as="document">
|
||||
<link rel="icon" type="{{ faviconFormat }}" href="{{ faviconRoute }}">
|
||||
<link rel="icon" type="{{ favicon.type }}" href="{{ favicon.destination }}">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
|
||||
<meta property="og:title" content="{{ config.title }}" />
|
||||
<meta property="og:site_name" content="{{ config.title }}" />
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue