1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-08-07 14:35:26 +02:00

Some changes

This commit is contained in:
slaveeks 2022-06-29 12:21:42 +03:00
parent 39be0816cf
commit 00b7fc6d41
9 changed files with 23406 additions and 63 deletions

View file

@ -4,5 +4,8 @@
"rcFile": "./.codexdocsrc", "rcFile": "./.codexdocsrc",
"uploads": "/uploads", "uploads": "/uploads",
"secret": "iamasecretstring", "secret": "iamasecretstring",
"faviconURL": "" "favicon": {
"destination": "",
"type": ""
}
} }

23343
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -7,6 +7,7 @@ import routes from './routes';
import HttpException from './exceptions/httpException'; import HttpException from './exceptions/httpException';
import * as dotenv from 'dotenv'; import * as dotenv from 'dotenv';
import config from 'config'; import config from 'config';
import os from 'os';
dotenv.config(); dotenv.config();
const app = express(); const app = express();
@ -24,6 +25,7 @@ app.use(express.urlencoded({ extended: true }));
app.use(cookieParser()); app.use(cookieParser());
app.use(express.static(path.join(__dirname, '../../public'))); app.use(express.static(path.join(__dirname, '../../public')));
app.use('/uploads', express.static(config.get('uploads'))); app.use('/uploads', express.static(config.get('uploads')));
app.use('/favicon', express.static(os.tmpdir()));
app.use('/', routes); app.use('/', routes);

View file

@ -1,28 +0,0 @@
import config from 'config';
import uploadFavicon from '../utils/uploadFavicon';
import express from 'express';
const router = express.Router();
// Get url to upload favicon from config
const faviconURL: string = config.get('faviconURL');
// Favicon path
let filePath: string;
// Upload favicon by url
uploadFavicon(faviconURL).then((res) => {
filePath = res;
})
.catch( (err) => {
console.log(err);
});
/**
* Get favicon
*/
router.get('/favicon', (req, res) => {
res.sendFile(filePath);
} );
export default router;

View file

@ -1,8 +1,25 @@
import express, { Request, Response } from 'express'; import express, { Request, Response } from 'express';
import verifyToken from './middlewares/token'; import verifyToken from './middlewares/token';
import appConfig from 'config';
import uploadFavicon from '../utils/uploadFavicon';
const router = express.Router(); 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. */ /* GET home page. */
router.get('/', verifyToken, async (req: Request, res: Response) => { router.get('/', verifyToken, async (req: Request, res: Response) => {
const config = req.app.locals.config; const config = req.app.locals.config;
@ -10,7 +27,9 @@ router.get('/', verifyToken, async (req: Request, res: Response) => {
if (config.startPage) { if (config.startPage) {
return res.redirect(config.startPage); return res.redirect(config.startPage);
} }
res.render('pages/index', { isAuthorized: res.locals.isAuthorized }); res.render('pages/index', { isAuthorized: res.locals.isAuthorized,
faviconFormat: `image/${fileFormat}`,
faviconRoute: `favicon/favicon.${fileFormat}` });
}); });
export default router; export default router;

View file

@ -5,14 +5,12 @@ import auth from './auth';
import aliases from './aliases'; import aliases from './aliases';
import api from './api'; import api from './api';
import pagesMiddleware from './middlewares/pages'; import pagesMiddleware from './middlewares/pages';
import favicon from './favicon';
const router = express.Router(); const router = express.Router();
router.use('/', pagesMiddleware, home); router.use('/', pagesMiddleware, home);
router.use('/', pagesMiddleware, pages); router.use('/', pagesMiddleware, pages);
router.use('/', pagesMiddleware, auth); router.use('/', pagesMiddleware, auth);
router.use('/', favicon);
router.use('/api', api); router.use('/api', api);
router.use('/', aliases); router.use('/', aliases);

View file

@ -1,5 +1,7 @@
import { NextFunction, Request, Response } from 'express'; import { NextFunction, Request, Response } from 'express';
console.log('Initiated');
/** /**
* Middleware for checking locals.isAuthorized property, which allows to edit/create pages * Middleware for checking locals.isAuthorized property, which allows to edit/create pages
* *

View file

@ -1,50 +1,54 @@
import { get } from 'https';
import path from 'path'; import path from 'path';
import os from 'os'; import os from 'os';
import fs from 'fs'; import fs from 'fs';
import fetch from 'node-fetch';
import config from 'config';
// Create empty buffer for file interface FaviconData {
let file: Buffer = Buffer.alloc(0); destination: string;
type: string;
}
const favicon = config.get<FaviconData>('favicon');
/**
* Check if string is url
*
* @param str - string to check
*/
function checkIsUrl(str: string): boolean {
const re = new RegExp('https?://');
return re.test(str);
}
/** /**
* Upload favicon by url * Upload favicon by url
* *
* @param url - url for uploading favicon * @param url - url for uploading favicon
* @returns { Promise<string> } - Promise with path of saved file * @returns { Promise<string> } - Promise with format of saved file
*/ */
export default async function uploadFavicon(url: string): Promise<string> { async function uploadFavicon(url: string): Promise<string> {
// Create prise of getting file data // Check if string is url
const fileDataPromise = new Promise<Buffer>(function (resolve, reject) { if (!checkIsUrl(url)) {
const req = get(url, function ( res) { return url;
// Reject on bad status }
if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) { // Make get request to url
return reject(new Error('statusCode=' + res.statusCode)); const res = await fetch(url);
} // Get buffer data from response
// Response on incoming data const fileData = await res.buffer();
res.on('data', (chunk) => {
file = Buffer.concat([file, chunk]);
});
res.on('end', function () {
resolve(file);
});
});
// Reject on request error
req.on('error', function (err) {
reject(err);
});
req.end();
});
const fileData = await fileDataPromise;
// Get file name by url // Get file name by url
const filename = url.substring(url.lastIndexOf('/')+1); const filename = url.substring(url.lastIndexOf('/')+1);
// Get file format
const format = filename.split('.')[1];
// Get file path in temporary directory // Get file path in temporary directory
const filePath = path.join(os.tmpdir(), filename); const filePath = path.join(os.tmpdir(), `favicon.${format}`);
// Save file // Save file
fs.writeFileSync(filePath, fileData); fs.writeFileSync(filePath, fileData);
return filePath; return `/favicon/favicon.${format}`;
} }

View file

@ -4,7 +4,7 @@
<title>{{ config.title }}</title> <title>{{ config.title }}</title>
<link rel="stylesheet" href="/dist/main.css" /> <link rel="stylesheet" href="/dist/main.css" />
<link rel="preload" href="{{ config.landingFrameSrc }}" as="document"> <link rel="preload" href="{{ config.landingFrameSrc }}" as="document">
<link rel="icon" type="image/png" href="/favicon"> <link rel="icon" type="{{ faviconFormat }}" href="{{ faviconRoute }}">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <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:title" content="{{ config.title }}" />
<meta property="og:site_name" content="{{ config.title }}" /> <meta property="og:site_name" content="{{ config.title }}" />