mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(database): ♻️ Make emojis use a Media instead of just rawdogging the URI
This commit is contained in:
parent
c7aae24d42
commit
cf1104d762
18 changed files with 4823 additions and 128 deletions
|
|
@ -4,7 +4,6 @@ import { Emojis } from "@versia/kit/tables";
|
|||
import chalk from "chalk";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import ora from "ora";
|
||||
import { MediaManager } from "~/classes/media/media-manager";
|
||||
import { BaseCommand } from "~/cli/base";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
|
|
@ -97,35 +96,22 @@ export default class EmojiAdd extends BaseCommand<typeof EmojiAdd> {
|
|||
);
|
||||
}
|
||||
|
||||
const mediaManager = new MediaManager(config);
|
||||
|
||||
const spinner = ora("Uploading emoji").start();
|
||||
|
||||
const uploaded = await mediaManager.addFile(file).catch((e: Error) => {
|
||||
spinner.fail();
|
||||
this.log(`${chalk.red("✗")} Error: ${chalk.red(e.message)}`);
|
||||
return null;
|
||||
});
|
||||
|
||||
if (!uploaded) {
|
||||
return this.exit(1);
|
||||
}
|
||||
const media = await Media.fromFile(file);
|
||||
|
||||
spinner.succeed();
|
||||
|
||||
await Emoji.insert({
|
||||
shortcode: args.shortcode,
|
||||
url: Media.getUrl(uploaded.path),
|
||||
mediaId: media.id,
|
||||
visibleInPicker: true,
|
||||
contentType: uploaded.uploadedFile.type,
|
||||
});
|
||||
|
||||
this.log(
|
||||
`${chalk.green("✓")} Created emoji ${chalk.green(
|
||||
args.shortcode,
|
||||
)} with url ${chalk.blue(
|
||||
chalk.underline(Media.getUrl(uploaded.path)),
|
||||
)}`,
|
||||
)} with url ${chalk.blue(chalk.underline(media.getUrl()))}`,
|
||||
);
|
||||
|
||||
this.exit(0);
|
||||
|
|
|
|||
|
|
@ -55,13 +55,10 @@ export default class EmojiDelete extends EmojiFinderCommand<
|
|||
|
||||
flags.print &&
|
||||
this.log(
|
||||
formatArray(emojis, [
|
||||
"id",
|
||||
"shortcode",
|
||||
"alt",
|
||||
"contentType",
|
||||
"instanceUrl",
|
||||
]),
|
||||
formatArray(
|
||||
emojis.map((e) => e.data),
|
||||
["id", "shortcode", "alt", "contentType", "instanceUrl"],
|
||||
),
|
||||
);
|
||||
|
||||
if (flags.confirm) {
|
||||
|
|
@ -80,13 +77,13 @@ export default class EmojiDelete extends EmojiFinderCommand<
|
|||
const spinner = ora("Deleting emoji(s)").start();
|
||||
|
||||
for (const emoji of emojis) {
|
||||
spinner.text = `Deleting emoji ${chalk.gray(emoji.shortcode)} (${
|
||||
spinner.text = `Deleting emoji ${chalk.gray(emoji.data.shortcode)} (${
|
||||
emojis.findIndex((e) => e.id === emoji.id) + 1
|
||||
}/${emojis.length})`;
|
||||
|
||||
const mediaManager = new MediaManager(config);
|
||||
|
||||
await mediaManager.deleteFileByUrl(emoji.url);
|
||||
await mediaManager.deleteFileByUrl(emoji.media.getUrl());
|
||||
|
||||
await db.delete(Emojis).where(eq(Emojis.id, emoji.id));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import { and, inArray, isNull } from "drizzle-orm";
|
|||
import { lookup } from "mime-types";
|
||||
import ora from "ora";
|
||||
import { unzip } from "unzipit";
|
||||
import { MediaManager } from "~/classes/media/media-manager";
|
||||
import { BaseCommand } from "~/cli/base";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
|
|
@ -169,8 +168,6 @@ export default class EmojiImport extends BaseCommand<typeof EmojiImport> {
|
|||
|
||||
const importSpinner = ora("Importing emojis").start();
|
||||
|
||||
const mediaManager = new MediaManager(config);
|
||||
|
||||
const successfullyImported: MetaType["emojis"] = [];
|
||||
|
||||
for (const emoji of newEmojis) {
|
||||
|
|
@ -197,26 +194,12 @@ export default class EmojiImport extends BaseCommand<typeof EmojiImport> {
|
|||
type: contentType,
|
||||
});
|
||||
|
||||
const uploaded = await mediaManager
|
||||
.addFile(newFile)
|
||||
.catch((e: Error) => {
|
||||
this.log(
|
||||
`${chalk.red("✗")} Error uploading ${chalk.red(
|
||||
emoji.emoji.name,
|
||||
)}: ${chalk.red(e.message)}`,
|
||||
);
|
||||
return null;
|
||||
});
|
||||
|
||||
if (!uploaded) {
|
||||
continue;
|
||||
}
|
||||
const media = await Media.fromFile(newFile);
|
||||
|
||||
await Emoji.insert({
|
||||
shortcode: emoji.emoji.name,
|
||||
url: Media.getUrl(uploaded.path),
|
||||
mediaId: media.id,
|
||||
visibleInPicker: true,
|
||||
contentType: uploaded.uploadedFile.type,
|
||||
});
|
||||
|
||||
successfullyImported.push(emoji);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue