mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-07 22:45:23 +02:00
Replaced uploadFavicon to initiating /favicon route, updated function, added catching errors from uploadFile
This commit is contained in:
parent
6e57874900
commit
aab5d65481
2 changed files with 34 additions and 15 deletions
|
@ -1,25 +1,28 @@
|
|||
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();
|
||||
|
||||
// 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) => {
|
||||
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);
|
||||
});
|
||||
res.sendFile(filePath);
|
||||
} );
|
||||
|
||||
export default router;
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
import { get } from 'https';
|
||||
import path from 'path';
|
||||
import os from 'os';
|
||||
import fs from 'fs';
|
||||
|
||||
// Create empty buffer for file
|
||||
let file: Buffer = Buffer.alloc(0);
|
||||
|
@ -7,10 +10,11 @@ let file: Buffer = Buffer.alloc(0);
|
|||
* Upload favicon by url
|
||||
*
|
||||
* @param url - url for uploading favicon
|
||||
* @returns { Promise<Buffer> } - Promise with whole file data
|
||||
* @returns { Promise<string> } - Promise with path of saved file
|
||||
*/
|
||||
export default function uploadFavicon(url: string): Promise<Buffer> {
|
||||
return new Promise(function (resolve, reject) {
|
||||
export default async function uploadFavicon(url: string): Promise<string> {
|
||||
// Create prise of getting file data
|
||||
const fileDataPromise = new Promise<Buffer>(function (resolve, reject) {
|
||||
const req = get(url, function ( res) {
|
||||
// Reject on bad status
|
||||
if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {
|
||||
|
@ -31,4 +35,16 @@ export default function uploadFavicon(url: string): Promise<Buffer> {
|
|||
});
|
||||
req.end();
|
||||
});
|
||||
const fileData = await fileDataPromise;
|
||||
|
||||
// Get file name by url
|
||||
const filename = url.substring(url.lastIndexOf('/')+1);
|
||||
|
||||
// Get file path in temporary directory
|
||||
const filePath = path.join(os.tmpdir(), filename);
|
||||
|
||||
// Save file
|
||||
fs.writeFileSync(filePath, fileData);
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue