diff --git a/config.ts b/config.ts index 87c70bc..6e843ed 100644 --- a/config.ts +++ b/config.ts @@ -1,6 +1,6 @@ import { load } from "@std/dotenv"; import { parse as parseYaml } from "jsr:@std/yaml"; -import { checkIfFileExists } from "./utils.ts"; +import { checkFileExists } from "./utils.ts"; import { log } from "./log.ts"; const dirname = Deno.cwd(); @@ -16,7 +16,7 @@ const defaultUserConfigPaths = [ ]; for (const path of defaultUserConfigPaths) { - const fileExists = await checkIfFileExists(path); + const fileExists = await checkFileExists(path); if (fileExists) { userConfigPath = path; break; diff --git a/magick.ts b/magick.ts index 1daa1f2..56779fb 100644 --- a/magick.ts +++ b/magick.ts @@ -1,11 +1,15 @@ import Profile from "./models/profile.ts"; -import { checkIfFileExists, runCommand } from "./utils.ts"; +import { checkFileExists, getImageMagickCommand, runCommand } from "./utils.ts"; import { log } from "./log.ts"; const tmpImgDir = "/tmp/substr/img"; +const magick = await getImageMagickCommand(); +if (!magick) { + log("ImageMagick is not installed. Cannot generate preview images", "yellow") +} function createRoundedImage(profile: Profile) { - if (!profile.picture) return false; + if (!magick || !profile.picture) return false; const args = [ profile.picture, @@ -21,10 +25,12 @@ function createRoundedImage(profile: Profile) { `${tmpImgDir}/p-${profile.event.id}-rounded.png` ]; - return runCommand("magick", args); + return runCommand(magick, args); } async function createOgImage(profile: Profile, ogImagePath: string, backgroundColor: string) { + if (!magick) return false; + const status = await createRoundedImage(profile); if (status && status.success) { @@ -39,16 +45,16 @@ async function createOgImage(profile: Profile, ogImagePath: string, backgroundCo ogImagePath ]; - return runCommand("magick", args); + return runCommand(magick, args); } }; export async function generateOgProfileImage(profile: Profile) { - if (!profile.picture) return false; + if (!magick || !profile.picture) return false; const ogImagePath = `${tmpImgDir}/og-p-${profile.event.id}.png`; const backgroundColor = "#333333"; - const fileExists = await checkIfFileExists(ogImagePath); + const fileExists = await checkFileExists(ogImagePath); if (!fileExists) { const status = await createOgImage(profile, ogImagePath, backgroundColor); diff --git a/utils.ts b/utils.ts index a407d29..2030b00 100644 --- a/utils.ts +++ b/utils.ts @@ -1,4 +1,4 @@ -export async function checkIfFileExists(filePath: string): Promise { +export async function checkFileExists(filePath: string): Promise { try { await Deno.lstat(filePath); return true; @@ -11,6 +11,16 @@ export async function checkIfFileExists(filePath: string): Promise { } } +async function checkExecutableExists(name: string): Promise { + try { + const command = new Deno.Command("which", { args: [name] }); + const { success } = await command.output(); + return success; + } catch { + return false; + } +} + export async function createSubtrTmpDirectories(): Promise { const dirs = [ "/tmp/substr/img/", @@ -23,12 +33,22 @@ export async function createSubtrTmpDirectories(): Promise { export async function runCommand(cmd: string, args: string[]) { const command = new Deno.Command(cmd, { args }); - const { code, stdout, stderr } = await command.output(); + const { code, success, stdout, stderr } = await command.output(); if (code === 1) { console.log(new TextDecoder().decode(stdout)); console.log(new TextDecoder().decode(stderr)); } - return { success: code === 0, stdout, stderr }; + return { success, stdout, stderr }; +} + +export async function getImageMagickCommand(): Promise { + if (await checkExecutableExists("magick")) { + return "magick"; + } else if (await checkExecutableExists("convert")) { + return "convert"; + } else { + return undefined; + } }