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