feat: Add Simple-Icons and Selfh.st collections (#1118)

* feat: Add Simple-Icons and Selfh.st collections
refactor: icon file path parsing

* refactor: allowed extensions check
This commit is contained in:
SeDemal
2024-09-16 16:34:45 +02:00
committed by GitHub
parent 929054d5bd
commit 9fb6e67ed3
5 changed files with 43 additions and 22 deletions

View File

@@ -11,6 +11,22 @@ const repositories = [
new URL("https://api.github.com/repos/walkxcode/dashboard-icons/git/trees/main?recursive=true"), new URL("https://api.github.com/repos/walkxcode/dashboard-icons/git/trees/main?recursive=true"),
"https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/{0}", "https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/{0}",
), ),
new GitHubIconRepository(
"selfh.st",
"selfhst/icons",
"CC0-1.0",
new URL("https://github.com/selfhst/icons"),
new URL("https://api.github.com/repos/selfhst/icons/git/trees/main?recursive=true"),
"https://cdn.jsdelivr.net/gh/selfhst/icons/{0}",
),
new GitHubIconRepository(
"SimpleIcons",
"simple-icons/simple-icons",
"CC0-1.0",
new URL("https://github.com/simple-icons/simple-icons"),
new URL("https://api.github.com/repos/simple-icons/simple-icons/git/trees/master?recursive=true"),
"https://cdn.simpleicons.org/{1}",
),
new JsdelivrIconRepository( new JsdelivrIconRepository(
"Papirus", "Papirus",
"PapirusDevelopmentTeam/papirus-icon-theme", "PapirusDevelopmentTeam/papirus-icon-theme",

View File

@@ -1,3 +1,5 @@
import { parse } from "path";
import { fetchWithTimeout } from "@homarr/common"; import { fetchWithTimeout } from "@homarr/common";
import type { IconRepositoryLicense } from "../types/icon-repository-license"; import type { IconRepositoryLicense } from "../types/icon-repository-license";
@@ -27,19 +29,23 @@ export class GitHubIconRepository extends IconRepository {
return { return {
success: true, success: true,
icons: listOfFiles.tree icons: listOfFiles.tree
.filter((treeItem) => .filter(({ path }) =>
this.allowedImageFileTypes.some((allowedExtension) => treeItem.path.includes(allowedExtension)), this.allowedImageFileTypes.some((allowedImageFileType) => parse(path).ext === allowedImageFileType),
) )
.map((treeItem) => { .map(({ path, size: sizeInBytes, sha: checksum }) => {
const fileNameWithExtension = this.getFileNameWithoutExtensionFromPath(treeItem.path); const file = parse(path);
const fileNameWithExtension = file.base;
const imageUrl = new URL(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.repositoryBlobUrlTemplate!.replace("{0}", path).replace("{1}", file.name),
);
return { return {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion imageUrl,
imageUrl: new URL(this.repositoryBlobUrlTemplate!.replace("{0}", treeItem.path)), fileNameWithExtension,
fileNameWithExtension: fileNameWithExtension,
local: false, local: false,
sizeInBytes: treeItem.size, sizeInBytes,
checksum: treeItem.sha, checksum,
}; };
}), }),
slug: this.slug, slug: this.slug,

View File

@@ -29,8 +29,4 @@ export abstract class IconRepository {
} }
protected abstract getAllIconsInternalAsync(): Promise<RepositoryIconGroup>; protected abstract getAllIconsInternalAsync(): Promise<RepositoryIconGroup>;
protected getFileNameWithoutExtensionFromPath(path: string) {
return path.replace(/^.*[\\/]/, "");
}
} }

View File

@@ -1,3 +1,5 @@
import { parse } from "path";
import { fetchWithTimeout } from "@homarr/common"; import { fetchWithTimeout } from "@homarr/common";
import type { IconRepositoryLicense } from "../types/icon-repository-license"; import type { IconRepositoryLicense } from "../types/icon-repository-license";
@@ -23,18 +25,19 @@ export class JsdelivrIconRepository extends IconRepository {
return { return {
success: true, success: true,
icons: listOfFiles.files icons: listOfFiles.files
.filter((file) => .filter(({ name: path }) =>
this.allowedImageFileTypes.some((allowedImageFileType) => file.name.includes(allowedImageFileType)), this.allowedImageFileTypes.some((allowedImageFileType) => parse(path).ext === allowedImageFileType),
) )
.map((file) => { .map(({ name: path, size: sizeInBytes, hash: checksum }) => {
const fileNameWithExtension = this.getFileNameWithoutExtensionFromPath(file.name); const file = parse(path);
const fileNameWithExtension = file.base;
return { return {
imageUrl: new URL(this.repositoryBlobUrlTemplate.replace("{0}", file.name)), imageUrl: new URL(this.repositoryBlobUrlTemplate.replace("{0}", path).replace("{1}", file.name)),
fileNameWithExtension: fileNameWithExtension, fileNameWithExtension,
local: false, local: false,
sizeInBytes: file.size, sizeInBytes,
checksum: file.hash, checksum,
}; };
}), }),
slug: this.slug, slug: this.slug,

View File

@@ -1 +1 @@
export type IconRepositoryLicense = "MIT" | "GPL-3.0" | undefined; export type IconRepositoryLicense = "MIT" | "GPL-3.0" | "CC0-1.0" | undefined;