Added ability to use custom favicon (#202)
* Added ability to change favicon in config
* Turned back version of icon in index.twig
* Added opportunity to upload favicon and route to get saved favicon
* Removed favicon from .codexdocsrc.sample
* Added docs to favicon route
* Replaced uploadFavicon to initiating /favicon route, updated function, added catching errors from uploadFile
* Updated Readme, added info about setting up app
* Updated Readme.md
* Some changes
* Favicon data saves to app.locals, replaced uploading favicon to app.ts
* Changed naming in config, from faviconURL to favicon, changed using app.locals variables
* Renamed uploadFavicon to downLoadFavicon, removed log in locals.ts
* Renamed favicon variable in app.ts
* Added checking favicon before uploading function, removed passing locals to views
* Added timeout for uploading favicon request and writeFileSync changed to writeFile
* Removed passing favicon locals and turned back removed variables
* Turned back variables
* Fixed duplicating os.tmpdir
* Fixed braces in objects, added new lines
* Added default favicon path, if favicon does not exists in config
* Updated docs, fixed using local favicon
2022-07-10 15:21:32 +03:00
|
|
|
import path from 'path';
|
|
|
|
import fs from 'fs';
|
2022-08-24 17:05:40 +03:00
|
|
|
import fetch, { RequestInit } from 'node-fetch';
|
Added ability to use custom favicon (#202)
* Added ability to change favicon in config
* Turned back version of icon in index.twig
* Added opportunity to upload favicon and route to get saved favicon
* Removed favicon from .codexdocsrc.sample
* Added docs to favicon route
* Replaced uploadFavicon to initiating /favicon route, updated function, added catching errors from uploadFile
* Updated Readme, added info about setting up app
* Updated Readme.md
* Some changes
* Favicon data saves to app.locals, replaced uploading favicon to app.ts
* Changed naming in config, from faviconURL to favicon, changed using app.locals variables
* Renamed uploadFavicon to downLoadFavicon, removed log in locals.ts
* Renamed favicon variable in app.ts
* Added checking favicon before uploading function, removed passing locals to views
* Added timeout for uploading favicon request and writeFileSync changed to writeFile
* Removed passing favicon locals and turned back removed variables
* Turned back variables
* Fixed duplicating os.tmpdir
* Fixed braces in objects, added new lines
* Added default favicon path, if favicon does not exists in config
* Updated docs, fixed using local favicon
2022-07-10 15:21:32 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Uploaded favicon data
|
|
|
|
*/
|
|
|
|
export interface FaviconData {
|
|
|
|
// Uploaded favicon path
|
|
|
|
destination: string;
|
|
|
|
|
|
|
|
// File type
|
|
|
|
type: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initiate controller for aborting request
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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, 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, faviconFolder: string): Promise<FaviconData> {
|
|
|
|
// Check of destination is empty
|
|
|
|
if (!destination) {
|
|
|
|
throw Error('Favicon destination is empty');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get file name by destination
|
|
|
|
const filename = destination.substring(destination.lastIndexOf('/')+1);
|
|
|
|
|
|
|
|
// Get file format
|
|
|
|
const format = filename.split('.')[1];
|
|
|
|
|
|
|
|
// Check if string is url
|
|
|
|
if (!checkIsUrl(destination)) {
|
|
|
|
return {
|
|
|
|
destination: `/${filename}`,
|
|
|
|
type: `image/${format}`,
|
|
|
|
} as FaviconData;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create timeout to abort request
|
|
|
|
const timeoutId = setTimeout(() => {
|
|
|
|
controller.abort();
|
|
|
|
console.log('Favicon request has timed out.');
|
|
|
|
}, 5000);
|
|
|
|
|
|
|
|
// Make get request to url
|
2022-08-24 17:05:40 +03:00
|
|
|
const res = await fetch(destination, { signal: controller.signal as RequestInit['signal'] });
|
Added ability to use custom favicon (#202)
* Added ability to change favicon in config
* Turned back version of icon in index.twig
* Added opportunity to upload favicon and route to get saved favicon
* Removed favicon from .codexdocsrc.sample
* Added docs to favicon route
* Replaced uploadFavicon to initiating /favicon route, updated function, added catching errors from uploadFile
* Updated Readme, added info about setting up app
* Updated Readme.md
* Some changes
* Favicon data saves to app.locals, replaced uploading favicon to app.ts
* Changed naming in config, from faviconURL to favicon, changed using app.locals variables
* Renamed uploadFavicon to downLoadFavicon, removed log in locals.ts
* Renamed favicon variable in app.ts
* Added checking favicon before uploading function, removed passing locals to views
* Added timeout for uploading favicon request and writeFileSync changed to writeFile
* Removed passing favicon locals and turned back removed variables
* Turned back variables
* Fixed duplicating os.tmpdir
* Fixed braces in objects, added new lines
* Added default favicon path, if favicon does not exists in config
* Updated docs, fixed using local favicon
2022-07-10 15:21:32 +03:00
|
|
|
// Get buffer data from response
|
|
|
|
const fileData = await res.buffer();
|
|
|
|
|
|
|
|
// Clear timeout, if data was got
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
|
|
|
// Get file path in temporary directory
|
|
|
|
const filePath = path.join(faviconFolder, `favicon.${format}`);
|
|
|
|
|
|
|
|
// Save file
|
|
|
|
await fs.writeFile(filePath, fileData, (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
destination: `/favicon/favicon.${format}`,
|
|
|
|
type: `image/${format}`,
|
|
|
|
} as FaviconData;
|
|
|
|
}
|