Fix resolving paths for .face.icon in dashboard and add home ('~') path support (#606)

* Fix resolving paths for .face.icon in dashboard and add home ('~') path support

* Fix ESLint issues

* Update src/lib/utils.ts

Co-authored-by: davfsa <davfsa@gmail.com>

* Update src/lib/utils.ts

* Rename `resolvePath` to `normalizePath`

* Rename missing reference

---------

Co-authored-by: Jas Singh <jaskiratpal.singh@outlook.com>
This commit is contained in:
davfsa
2024-12-24 11:56:11 +01:00
committed by GitHub
parent c87a6ca251
commit e9df5eb230
4 changed files with 29 additions and 16 deletions

View File

@@ -11,7 +11,6 @@ import options from '../options';
import { Astal, Gdk, Gtk } from 'astal/gtk3';
import AstalApps from 'gi://AstalApps?version=0.1';
import { exec, execAsync } from 'astal/process';
import { Gio } from 'astal';
/**
* Handles errors by throwing a new Error with a message.
@@ -228,18 +227,15 @@ export function launchApp(app: AstalApps.Application): void {
* This function attempts to load an image from the specified filepath using GdkPixbuf.
* If the image is successfully loaded, it returns true. Otherwise, it logs an error and returns false.
*
* Note: Unlike GdkPixbuf, this function will normalize the given path.
*
* @param imgFilePath The path to the image file.
*
* @returns True if the filepath is a valid image, false otherwise.
*/
export function isAnImage(imgFilePath: string): boolean {
try {
const file = Gio.File.new_for_path(imgFilePath);
if (!file.query_exists(null)) {
return false;
}
GdkPixbuf.Pixbuf.new_from_file(imgFilePath);
GdkPixbuf.Pixbuf.new_from_file(normalizePath(imgFilePath));
return true;
} catch (error) {
console.error(error);
@@ -247,6 +243,24 @@ export function isAnImage(imgFilePath: string): boolean {
}
}
/**
* Normalize a path to the absolute representation of the path.
*
* Note: This will only expand '~' if present. Path traversal is not supported.
*
* @param path The path to normalize.
*
* @returns The normalized path.
*/
export function normalizePath(path: string): string {
if (path.charAt(0) == '~') {
// Replace will only replace the first match, in this case, the first character
return path.replace('~', GLib.get_home_dir());
}
return path;
}
/**
* Sends a notification using the `notify-send` command.
*