♻️ Refactor icon picker (#724)
This commit is contained in:
44
src/tools/server/images/local-icons-repository.ts
Normal file
44
src/tools/server/images/local-icons-repository.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import fs from 'fs';
|
||||
import {
|
||||
AbstractIconRepository,
|
||||
NormalizedIcon,
|
||||
NormalizedIconRepositoryResult,
|
||||
} from './abstract-icons-repository';
|
||||
|
||||
export class LocalIconsRepository extends AbstractIconRepository {
|
||||
constructor() {
|
||||
super('');
|
||||
}
|
||||
|
||||
protected async fetchInternally(): Promise<NormalizedIconRepositoryResult> {
|
||||
if (!fs.existsSync('./public/icons')) {
|
||||
return {
|
||||
count: 0,
|
||||
entries: [],
|
||||
name: 'Local',
|
||||
success: true,
|
||||
copyright: this.copyright,
|
||||
};
|
||||
}
|
||||
|
||||
const files = fs.readdirSync('./public/icons');
|
||||
|
||||
const normalizedEntries = files
|
||||
.filter((file) => ['.png', '.svg', '.jpeg', '.jpg'].some((x) => file.endsWith(x)))
|
||||
.map(
|
||||
(file): NormalizedIcon => ({
|
||||
name: file,
|
||||
url: `./icons/${file}`,
|
||||
size: 0,
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
entries: normalizedEntries,
|
||||
count: normalizedEntries.length,
|
||||
success: true,
|
||||
name: 'Local',
|
||||
copyright: this.copyright,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user