Refactor MediaLibrary and MediaListener to use static parsePath method for improved consistency

This commit is contained in:
Elio Struyf
2026-03-06 16:19:42 +01:00
parent f5b636d960
commit 57e0e2e7b7
3 changed files with 74 additions and 58 deletions

View File

@@ -4,9 +4,10 @@
### 🎨 Enhancements
- [#937](https://github.com/estruyf/vscode-front-matter/issues/937): Dashboard "Structure" view for documentation sites
- [#937](https://github.com/estruyf/vscode-front-matter/issues/937): Dashboard "Structure" view for documentation sites *WIP*
- [#965](https://github.com/estruyf/vscode-front-matter/issues/965): Added SEO support for the keyword in the first paragraph
- [#973](https://github.com/estruyf/vscode-front-matter/issues/973): Support for number fields in the snippets
- [#1004](https://github.com/estruyf/vscode-front-matter/issues/1004): Fix for `mediaDB.json` containing full paths on Windows instead of relative paths
### 🐞 Fixes

View File

@@ -113,7 +113,7 @@ export class MediaLibrary {
public async get(id: string): Promise<MediaRecord | undefined> {
try {
const fileId = this.parsePath(id);
const fileId = MediaLibrary.parsePath(id);
if (await this.db?.exists(fileId)) {
return await this.db?.getData(fileId);
}
@@ -142,13 +142,13 @@ export class MediaLibrary {
}
public set(id: string, metadata: any): void {
const fileId = this.parsePath(id);
const fileId = MediaLibrary.parsePath(id);
this.db?.push(fileId, metadata, true);
}
public async rename(oldId: string, newId: string): Promise<void> {
const fileId = this.parsePath(oldId);
const newFileId = this.parsePath(newId);
const fileId = MediaLibrary.parsePath(oldId);
const newFileId = MediaLibrary.parsePath(newId);
const data = await this.get(fileId);
if (data) {
this.db?.delete(fileId);
@@ -157,7 +157,7 @@ export class MediaLibrary {
}
public async remove(path: string): Promise<void> {
const fileId = this.parsePath(path);
const fileId = MediaLibrary.parsePath(path);
await this.db?.delete(fileId);
}
@@ -183,9 +183,12 @@ export class MediaLibrary {
}
}
public parsePath(path: string) {
public static parsePath(path: string) {
const wsFolder = Folders.getWorkspaceFolder();
let absPath = path.replace(parseWinPath(wsFolder?.fsPath || ''), WORKSPACE_PLACEHOLDER);
let absPath = parseWinPath(path).replace(
parseWinPath(wsFolder?.fsPath || ''),
WORKSPACE_PLACEHOLDER
);
absPath = isWindows() ? absPath.split('\\').join('/') : absPath;
return absPath.toLowerCase();
}

View File

@@ -75,25 +75,30 @@ export class MediaListener extends BaseListener {
return;
}
window.withProgress({
location: ProgressLocation.Notification,
title: localize(LocalizationKey.listenersDashboardMediaListenersDeleteMediaFolderProgressTitle),
cancellable: false
}, async () => {
const folderPath = parse(msg.folder).dir;
window.withProgress(
{
location: ProgressLocation.Notification,
title: localize(
LocalizationKey.listenersDashboardMediaListenersDeleteMediaFolderProgressTitle
),
cancellable: false
},
async () => {
const folderPath = parse(msg.folder).dir;
const mediaLib = MediaLibrary.getInstance();
const parsedPath = mediaLib.parsePath(msg.folder);
const mediaFiles = await mediaLib.getAllByPath(parsedPath);
const mediaLib = MediaLibrary.getInstance();
const parsedPath = MediaLibrary.parsePath(msg.folder);
const mediaFiles = await mediaLib.getAllByPath(parsedPath);
for (const fileName of Object.keys(mediaFiles)) {
const filePath = join(msg.folder, fileName);
await mediaLib.remove(filePath);
for (const fileName of Object.keys(mediaFiles)) {
const filePath = join(msg.folder, fileName);
await mediaLib.remove(filePath);
}
await workspace.fs.delete(Uri.file(msg.folder), { recursive: true, useTrash: false });
await MediaListener.sendMediaFiles(0, folderPath);
}
await workspace.fs.delete(Uri.file(msg.folder), { recursive: true, useTrash: false });
await MediaListener.sendMediaFiles(0, folderPath);
});
);
}
public static async updateMediaFolder(msg: {
@@ -105,41 +110,48 @@ export class MediaListener extends BaseListener {
return;
}
window.withProgress({
location: ProgressLocation.Notification,
title: localize(LocalizationKey.listenersDashboardMediaListenersUpdateMediaFolderProgressTitle),
cancellable: false
}, async () => {
const folderName = parse(msg.folder).base;
const newFolderName = await window.showInputBox({
prompt: 'Enter new folder name',
value: folderName
});
window.withProgress(
{
location: ProgressLocation.Notification,
title: localize(
LocalizationKey.listenersDashboardMediaListenersUpdateMediaFolderProgressTitle
),
cancellable: false
},
async () => {
const folderName = parse(msg.folder).base;
if (!newFolderName || newFolderName === folderName) {
return;
const newFolderName = await window.showInputBox({
prompt: 'Enter new folder name',
value: folderName
});
if (!newFolderName || newFolderName === folderName) {
return;
}
const newFolderPath = join(parse(msg.folder).dir, newFolderName);
// Get all media files from the folder
const mediaLib = MediaLibrary.getInstance();
const parsedPath = MediaLibrary.parsePath(msg.folder);
const mediaFiles = await mediaLib.getAllByPath(parsedPath);
// Update the folder
await workspace.fs.rename(Uri.file(msg.folder), Uri.file(newFolderPath), {
overwrite: false
});
// Update the media files
for (const fileName of Object.keys(mediaFiles)) {
const newFilePath = join(newFolderPath, fileName);
const oldFilePath = join(msg.folder, fileName);
await mediaLib.rename(oldFilePath, newFilePath);
}
await this.sendMediaFiles(0, parse(msg.folder).dir);
}
const newFolderPath = join(parse(msg.folder).dir, newFolderName);
// Get all media files from the folder
const mediaLib = MediaLibrary.getInstance();
const parsedPath = mediaLib.parsePath(msg.folder);
const mediaFiles = await mediaLib.getAllByPath(parsedPath);
// Update the folder
await workspace.fs.rename(Uri.file(msg.folder), Uri.file(newFolderPath), { overwrite: false });
// Update the media files
for (const fileName of Object.keys(mediaFiles)) {
const newFilePath = join(newFolderPath, fileName);
const oldFilePath = join(msg.folder, fileName);
await mediaLib.rename(oldFilePath, newFilePath);
}
await this.sendMediaFiles(0, parse(msg.folder).dir);
});
);
}
/**
@@ -205,7 +217,7 @@ export class MediaListener extends BaseListener {
for (const file of filesEndingWith) {
const absPath = FilesHelper.relToAbsPath(file);
if (!(await existsAsync(absPath))) {
const parsedPath = mediaLib.parsePath(absPath);
const parsedPath = MediaLibrary.parsePath(absPath);
const metadata = await mediaLib.get(parsedPath);
if (metadata) {
unmappedFiles.push({