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

Replaced uploadFavicon to initiating /favicon route, updated function, added catching errors from uploadFile

This commit is contained in:
slaveeks 2022-06-15 13:29:56 +03:00
parent 6e57874900
commit aab5d65481
2 changed files with 34 additions and 15 deletions

View file

@ -1,25 +1,28 @@
import config from 'config'; import config from 'config';
import uploadFavicon from '../utils/uploadFavicon'; import uploadFavicon from '../utils/uploadFavicon';
import path from 'path';
import os from 'os';
import fs from 'fs';
import express from 'express'; import express from 'express';
const router = express.Router(); 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 * Get favicon
*/ */
router.get('/favicon', (req, res) => { 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; export default router;

View file

@ -1,4 +1,7 @@
import { get } from 'https'; import { get } from 'https';
import path from 'path';
import os from 'os';
import fs from 'fs';
// Create empty buffer for file // Create empty buffer for file
let file: Buffer = Buffer.alloc(0); let file: Buffer = Buffer.alloc(0);
@ -7,10 +10,11 @@ let file: Buffer = Buffer.alloc(0);
* Upload favicon by url * Upload favicon by url
* *
* @param url - url for uploading favicon * @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> { export default async function uploadFavicon(url: string): Promise<string> {
return new Promise(function (resolve, reject) { // Create prise of getting file data
const fileDataPromise = new Promise<Buffer>(function (resolve, reject) {
const req = get(url, function ( res) { const req = get(url, function ( res) {
// Reject on bad status // Reject on bad status
if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) { if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {
@ -31,4 +35,16 @@ export default function uploadFavicon(url: string): Promise<Buffer> {
}); });
req.end(); 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;
} }