mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-14 01:15:47 +01:00
@@ -1,10 +1,13 @@
|
||||
import Consola from 'consola';
|
||||
|
||||
export abstract class AbstractIconRepository {
|
||||
constructor(readonly copyright?: string) {}
|
||||
protected constructor(readonly copyright?: string) {}
|
||||
|
||||
async fetch(): Promise<NormalizedIconRepositoryResult> {
|
||||
try {
|
||||
return await this.fetchInternally();
|
||||
} catch (err) {
|
||||
Consola.error(`Failed to fetch icons from repository '${this.name}': ${err}`);
|
||||
return {
|
||||
success: false,
|
||||
count: 0,
|
||||
@@ -15,6 +18,8 @@ export abstract class AbstractIconRepository {
|
||||
}
|
||||
}
|
||||
protected abstract fetchInternally(): Promise<NormalizedIconRepositoryResult>;
|
||||
|
||||
protected abstract name: string;
|
||||
}
|
||||
|
||||
export type NormalizedIconRepositoryResult = {
|
||||
|
||||
@@ -51,6 +51,8 @@ export class GitHubIconsRepository extends AbstractIconRepository {
|
||||
copyright: this.copyright,
|
||||
};
|
||||
}
|
||||
|
||||
protected name: string = "GitHub";
|
||||
}
|
||||
|
||||
type GitHubRepositoryUrl = {
|
||||
|
||||
@@ -46,6 +46,8 @@ export class JsdelivrIconsRepository extends AbstractIconRepository {
|
||||
copyright: this.copyright,
|
||||
};
|
||||
}
|
||||
|
||||
protected name: string = "JsDelivr";
|
||||
}
|
||||
|
||||
type JsdelivrRepositoryUrl = {
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
import fs from 'fs';
|
||||
|
||||
import { AbstractIconRepository, NormalizedIcon, NormalizedIconRepositoryResult } from './abstract-icons-repository';
|
||||
import {
|
||||
AbstractIconRepository,
|
||||
NormalizedIcon,
|
||||
NormalizedIconRepositoryResult,
|
||||
} from './abstract-icons-repository';
|
||||
import Consola from 'consola';
|
||||
|
||||
const iconsDirectory = './public/icons';
|
||||
|
||||
export class LocalIconsRepository extends AbstractIconRepository {
|
||||
constructor() {
|
||||
@@ -8,7 +15,8 @@ export class LocalIconsRepository extends AbstractIconRepository {
|
||||
}
|
||||
|
||||
protected async fetchInternally(): Promise<NormalizedIconRepositoryResult> {
|
||||
if (!fs.existsSync('./public/icons')) {
|
||||
if (!fs.existsSync(iconsDirectory)) {
|
||||
Consola.info('Local icons repository directory does not exist');
|
||||
return {
|
||||
count: 0,
|
||||
entries: [],
|
||||
@@ -18,24 +26,30 @@ export class LocalIconsRepository extends AbstractIconRepository {
|
||||
};
|
||||
}
|
||||
|
||||
const files = fs.readdirSync('./public/icons');
|
||||
const files = fs.readdirSync(iconsDirectory);
|
||||
Consola.info(`Local icons repository directory exists and contains ${files.length} icons`);
|
||||
|
||||
const normalizedEntries = files
|
||||
.filter((file) => ['.png', '.svg', '.jpeg', '.jpg'].some((x) => file.endsWith(x)))
|
||||
.map(
|
||||
(file): NormalizedIcon => ({
|
||||
(file): NormalizedIcon => {
|
||||
const stats = fs.statSync(`${iconsDirectory}/${file}`);
|
||||
return {
|
||||
name: file,
|
||||
url: `./icons/${file}`,
|
||||
size: 0,
|
||||
})
|
||||
url: `/icons/${file}`,
|
||||
size: stats.size,
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
return {
|
||||
entries: normalizedEntries,
|
||||
count: normalizedEntries.length,
|
||||
success: true,
|
||||
name: 'Local',
|
||||
name: this.name,
|
||||
copyright: this.copyright,
|
||||
};
|
||||
}
|
||||
|
||||
protected name: string = "Local";
|
||||
}
|
||||
|
||||
@@ -35,6 +35,8 @@ export class UnpkgIconsRepository extends AbstractIconRepository {
|
||||
copyright: this.copyright,
|
||||
};
|
||||
}
|
||||
|
||||
protected name: string = "UnPkg";
|
||||
}
|
||||
|
||||
type UnpkgResponse = {
|
||||
|
||||
Reference in New Issue
Block a user