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

Fixed duplicating os.tmpdir

This commit is contained in:
slaveeks 2022-07-07 12:03:35 +03:00
parent c2130a2225
commit 3bf407b74d
2 changed files with 8 additions and 6 deletions

View file

@ -24,10 +24,12 @@ app.set('views', path.join(__dirname, './', 'views'));
app.set('view engine', 'twig');
require('./utils/twig');
const downloadedFaviconFolder = os.tmpdir();
// Check if favicon is not empty
if (favicon) {
// Upload favicon by url, it's path on server is '/temp/favicon.{format}'
downloadFavicon(favicon).then((res) => {
downloadFavicon(favicon, downloadedFaviconFolder).then((res) => {
app.locals.favicon = res;
console.log('Favicon successfully uploaded');
})
@ -45,7 +47,7 @@ app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, '../../public')));
app.use('/uploads', express.static(config.get('uploads')));
app.use('/favicon', express.static(os.tmpdir()));
app.use('/favicon', express.static(downloadedFaviconFolder));
app.use('/', routes);

View file

@ -1,5 +1,4 @@
import path from 'path';
import os from 'os';
import fs from 'fs';
import fetch from 'node-fetch';
@ -32,9 +31,10 @@ function checkIsUrl(str: string): boolean {
* Upload favicon by url, or get it by path
*
* @param destination - url or path of favicon
* @param faviconFolder - folder to save favicon
* @returns { Promise<FaviconData> } - Promise with data about favicon
*/
export async function downloadFavicon(destination: string): Promise<FaviconData> {
export async function downloadFavicon(destination: string, faviconFolder: string): Promise<FaviconData> {
// Check of destination is empty
if (!destination) {
throw Error('Favicon destination is empty');
@ -67,7 +67,7 @@ export async function downloadFavicon(destination: string): Promise<FaviconData>
clearTimeout(timeoutId);
// Get file path in temporary directory
const filePath = path.join(os.tmpdir(), `favicon.${format}`);
const filePath = path.join(faviconFolder, `favicon.${format}`);
// Save file
await fs.writeFile(filePath, fileData, (err) => {
@ -76,6 +76,6 @@ export async function downloadFavicon(destination: string): Promise<FaviconData>
}
});
return { destination: `/favicon/favicon.${format}`,
return { destination: `/favicon/favicon.${format}`,
type: `image/${format}` } as FaviconData;
}