mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-07 06:25:21 +02:00
Added opportunity to upload favicon and route to get saved favicon
This commit is contained in:
parent
f174a42d20
commit
b1d4d2a8ae
8 changed files with 66 additions and 13 deletions
|
@ -3,5 +3,6 @@
|
|||
"database": ".db",
|
||||
"rcFile": "./.codexdocsrc",
|
||||
"uploads": "public/uploads",
|
||||
"secret": "iamasecretstring"
|
||||
"secret": "iamasecretstring",
|
||||
"faviconURL": ""
|
||||
}
|
||||
|
|
|
@ -3,5 +3,6 @@
|
|||
"database": ".db",
|
||||
"rcFile": "./.codexdocsrc",
|
||||
"uploads": "/uploads",
|
||||
"secret": "iamasecretstring"
|
||||
"secret": "iamasecretstring",
|
||||
"faviconURL": ""
|
||||
}
|
||||
|
|
|
@ -3,5 +3,6 @@
|
|||
"database": ".testdb",
|
||||
"rcFile": "./src/test/.codexdocsrc",
|
||||
"uploads": "public/uploads_test",
|
||||
"secret": "iamasecretstring"
|
||||
"secret": "iamasecretstring",
|
||||
"faviconURL": ""
|
||||
}
|
||||
|
|
22
src/backend/routes/favicon.ts
Normal file
22
src/backend/routes/favicon.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import config from 'config';
|
||||
import uploadFavicon from '../utils/uploadFavicon';
|
||||
import path from 'path';
|
||||
import os from 'os';
|
||||
import fs from 'fs';
|
||||
import express from 'express';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/favicon', (req, res) => {
|
||||
const faviconURL: string = config.get('faviconURL');
|
||||
|
||||
uploadFavicon(faviconURL).then((file) => {
|
||||
const filename = faviconURL.substring(faviconURL.lastIndexOf('/')+1);
|
||||
const filePath = path.join(os.tmpdir(), filename);
|
||||
|
||||
fs.writeFileSync(filePath, file);
|
||||
res.sendFile(filePath);
|
||||
});
|
||||
} );
|
||||
|
||||
export default router;
|
|
@ -5,12 +5,14 @@ import auth from './auth';
|
|||
import aliases from './aliases';
|
||||
import api from './api';
|
||||
import pagesMiddleware from './middlewares/pages';
|
||||
import favicon from './favicon';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.use('/', pagesMiddleware, home);
|
||||
router.use('/', pagesMiddleware, pages);
|
||||
router.use('/', pagesMiddleware, auth);
|
||||
router.use('/', favicon);
|
||||
router.use('/api', api);
|
||||
router.use('/', aliases);
|
||||
|
||||
|
|
34
src/backend/utils/uploadFavicon.ts
Normal file
34
src/backend/utils/uploadFavicon.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { get } from 'https';
|
||||
|
||||
// Create empty buffer for file
|
||||
let file: Buffer = Buffer.alloc(0);
|
||||
|
||||
/**
|
||||
* Upload favicon by url
|
||||
*
|
||||
* @param url - url for uploading favicon
|
||||
* @returns { Promise<Buffer> } - Promise with whole file data
|
||||
*/
|
||||
export default function uploadFavicon(url: string): Promise<Buffer> {
|
||||
return new Promise(function (resolve, reject) {
|
||||
const req = get(url, function ( res) {
|
||||
// Reject on bad status
|
||||
if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {
|
||||
return reject(new Error('statusCode=' + res.statusCode));
|
||||
}
|
||||
// Response on incoming data
|
||||
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();
|
||||
});
|
||||
}
|
|
@ -7,11 +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" />
|
||||
{% if config.favicon is empty %}
|
||||
<link rel="icon" type="image/png" href="/favicon.png">
|
||||
{% else %}
|
||||
<link rel="icon" type="image/png" href="{{ config.favicon }}">
|
||||
{% endif %}
|
||||
<link rel="icon" type="image/png" href="/favicon">
|
||||
</head>
|
||||
<script>
|
||||
window.config = {
|
||||
|
|
|
@ -4,11 +4,7 @@
|
|||
<title>{{ config.title }}</title>
|
||||
<link rel="stylesheet" href="/dist/main.css" />
|
||||
<link rel="preload" href="{{ config.landingFrameSrc }}" as="document">
|
||||
{% if config.favicon is empty %}
|
||||
<link rel="icon" type="image/png" href="/favicon.png?v=2">
|
||||
{% else %}
|
||||
<link rel="icon" type="image/png" href="{{ config.favicon }}">
|
||||
{% endif %}
|
||||
<link rel="icon" type="image/png" href="/favicon">
|
||||
<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