Switch DriveFile's usageHint field to an enum type

This commit is contained in:
yumeko 2024-04-19 18:22:02 +03:00
parent 968657d26e
commit 6c46bb56fd
No known key found for this signature in database
GPG Key ID: 31A48AD2758B1B53
5 changed files with 14 additions and 9 deletions

View File

@ -3,11 +3,15 @@ import type { MigrationInterface, QueryRunner } from "typeorm";
export class AddDriveFileUsage1713451569342 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "drive_file" ADD "usageHint" character varying(16) DEFAULT NULL`,
`CREATE TYPE drive_file_usage_hint_enum AS ENUM ('userAvatar', 'userBanner')`,
);
await queryRunner.query(
`ALTER TABLE "drive_file" ADD "usageHint" drive_file_usage_hint_enum DEFAULT NULL`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "drive_file" DROP COLUMN "usageHint"`);
await queryRunner.query(`DROP TYPE drive_file_usage_hint_enum`);
}
}

View File

@ -16,7 +16,7 @@ import { DriveFolder } from "./drive-folder.js";
import { DB_MAX_IMAGE_COMMENT_LENGTH } from "@/misc/hard-limits.js";
import { NoteFile } from "./note-file.js";
export type DriveFileUsageHint = "user_avatar" | "user_banner" | null;
export type DriveFileUsageHint = "userAvatar" | "userBanner" | null;
@Entity()
@Index(["userId", "folderId", "id"])
@ -179,8 +179,9 @@ export class DriveFile {
})
public isSensitive: boolean;
@Column("varchar", {
length: 16,
@Column({
type: "enum",
enum: ["userAvatar", "userBanner"],
nullable: true,
comment: "Hint for what the file is used for.",
})

View File

@ -369,7 +369,7 @@ export async function createPerson(
: resolveImage(
user,
img,
index === 0 ? "user_avatar" : index === 1 ? "user_banner" : null,
index === 0 ? "userAvatar" : index === 1 ? "userBanner" : null,
).catch(() => null),
),
);
@ -449,7 +449,7 @@ export async function updatePerson(
: resolveImage(
user,
img,
index === 0 ? "user_avatar" : index === 1 ? "user_banner" : null,
index === 0 ? "userAvatar" : index === 1 ? "userBanner" : null,
).catch(() => null),
),
);

View File

@ -335,14 +335,14 @@ export default define(meta, paramDef, async (ps, _user, token) => {
if (avatar) {
if (user.avatarId)
await DriveFiles.update(user.avatarId, { usageHint: null });
await DriveFiles.update(avatar.id, { usageHint: "user_avatar" });
await DriveFiles.update(avatar.id, { usageHint: "userAvatar" });
}
// Update old/new banner usage hints
if (banner) {
if (user.bannerId)
await DriveFiles.update(user.bannerId, { usageHint: null });
await DriveFiles.update(banner.id, { usageHint: "user_banner" });
await DriveFiles.update(banner.id, { usageHint: "userBanner" });
}
if (Object.keys(updates).length > 0) await Users.update(user.id, updates);

View File

@ -66,7 +66,7 @@ function urlPathJoin(
* @param type Content-Type for original
* @param hash Hash for original
* @param size Size for original
* @param usage Optional usage hint for file (f.e. "user_avatar")
* @param usage Optional usage hint for file (f.e. "userAvatar")
*/
async function save(
file: DriveFile,