Support older imagemagick command
This commit is contained in:
parent
b7974c8610
commit
30a74acf3b
@ -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;
|
||||
|
18
magick.ts
18
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);
|
||||
|
26
utils.ts
26
utils.ts
@ -1,4 +1,4 @@
|
||||
export async function checkIfFileExists(filePath: string): Promise<boolean> {
|
||||
export async function checkFileExists(filePath: string): Promise<boolean> {
|
||||
try {
|
||||
await Deno.lstat(filePath);
|
||||
return true;
|
||||
@ -11,6 +11,16 @@ export async function checkIfFileExists(filePath: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
async function checkExecutableExists(name: string): Promise<boolean> {
|
||||
try {
|
||||
const command = new Deno.Command("which", { args: [name] });
|
||||
const { success } = await command.output();
|
||||
return success;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function createSubtrTmpDirectories(): Promise<void> {
|
||||
const dirs = [
|
||||
"/tmp/substr/img/",
|
||||
@ -23,12 +33,22 @@ export async function createSubtrTmpDirectories(): Promise<void> {
|
||||
|
||||
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<string | undefined> {
|
||||
if (await checkExecutableExists("magick")) {
|
||||
return "magick";
|
||||
} else if (await checkExecutableExists("convert")) {
|
||||
return "convert";
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user