Support older imagemagick command
This commit is contained in:
parent
b7974c8610
commit
30a74acf3b
@ -1,6 +1,6 @@
|
|||||||
import { load } from "@std/dotenv";
|
import { load } from "@std/dotenv";
|
||||||
import { parse as parseYaml } from "jsr:@std/yaml";
|
import { parse as parseYaml } from "jsr:@std/yaml";
|
||||||
import { checkIfFileExists } from "./utils.ts";
|
import { checkFileExists } from "./utils.ts";
|
||||||
import { log } from "./log.ts";
|
import { log } from "./log.ts";
|
||||||
|
|
||||||
const dirname = Deno.cwd();
|
const dirname = Deno.cwd();
|
||||||
@ -16,7 +16,7 @@ const defaultUserConfigPaths = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
for (const path of defaultUserConfigPaths) {
|
for (const path of defaultUserConfigPaths) {
|
||||||
const fileExists = await checkIfFileExists(path);
|
const fileExists = await checkFileExists(path);
|
||||||
if (fileExists) {
|
if (fileExists) {
|
||||||
userConfigPath = path;
|
userConfigPath = path;
|
||||||
break;
|
break;
|
||||||
|
18
magick.ts
18
magick.ts
@ -1,11 +1,15 @@
|
|||||||
import Profile from "./models/profile.ts";
|
import Profile from "./models/profile.ts";
|
||||||
import { checkIfFileExists, runCommand } from "./utils.ts";
|
import { checkFileExists, getImageMagickCommand, runCommand } from "./utils.ts";
|
||||||
import { log } from "./log.ts";
|
import { log } from "./log.ts";
|
||||||
|
|
||||||
const tmpImgDir = "/tmp/substr/img";
|
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) {
|
function createRoundedImage(profile: Profile) {
|
||||||
if (!profile.picture) return false;
|
if (!magick || !profile.picture) return false;
|
||||||
|
|
||||||
const args = [
|
const args = [
|
||||||
profile.picture,
|
profile.picture,
|
||||||
@ -21,10 +25,12 @@ function createRoundedImage(profile: Profile) {
|
|||||||
`${tmpImgDir}/p-${profile.event.id}-rounded.png`
|
`${tmpImgDir}/p-${profile.event.id}-rounded.png`
|
||||||
];
|
];
|
||||||
|
|
||||||
return runCommand("magick", args);
|
return runCommand(magick, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createOgImage(profile: Profile, ogImagePath: string, backgroundColor: string) {
|
async function createOgImage(profile: Profile, ogImagePath: string, backgroundColor: string) {
|
||||||
|
if (!magick) return false;
|
||||||
|
|
||||||
const status = await createRoundedImage(profile);
|
const status = await createRoundedImage(profile);
|
||||||
|
|
||||||
if (status && status.success) {
|
if (status && status.success) {
|
||||||
@ -39,16 +45,16 @@ async function createOgImage(profile: Profile, ogImagePath: string, backgroundCo
|
|||||||
ogImagePath
|
ogImagePath
|
||||||
];
|
];
|
||||||
|
|
||||||
return runCommand("magick", args);
|
return runCommand(magick, args);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function generateOgProfileImage(profile: Profile) {
|
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 ogImagePath = `${tmpImgDir}/og-p-${profile.event.id}.png`;
|
||||||
const backgroundColor = "#333333";
|
const backgroundColor = "#333333";
|
||||||
const fileExists = await checkIfFileExists(ogImagePath);
|
const fileExists = await checkFileExists(ogImagePath);
|
||||||
|
|
||||||
if (!fileExists) {
|
if (!fileExists) {
|
||||||
const status = await createOgImage(profile, ogImagePath, backgroundColor);
|
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 {
|
try {
|
||||||
await Deno.lstat(filePath);
|
await Deno.lstat(filePath);
|
||||||
return true;
|
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> {
|
export async function createSubtrTmpDirectories(): Promise<void> {
|
||||||
const dirs = [
|
const dirs = [
|
||||||
"/tmp/substr/img/",
|
"/tmp/substr/img/",
|
||||||
@ -23,12 +33,22 @@ export async function createSubtrTmpDirectories(): Promise<void> {
|
|||||||
|
|
||||||
export async function runCommand(cmd: string, args: string[]) {
|
export async function runCommand(cmd: string, args: string[]) {
|
||||||
const command = new Deno.Command(cmd, { args });
|
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) {
|
if (code === 1) {
|
||||||
console.log(new TextDecoder().decode(stdout));
|
console.log(new TextDecoder().decode(stdout));
|
||||||
console.log(new TextDecoder().decode(stderr));
|
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