Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

1.1 KB
Raw Blame
import { createId } from "@paralleldrive/cuid2";
import db from "../../database";
import { userAvatarTable } from "../../database/schema";
import type { DbOrTx } from "../../project-access/db-or-tx";
import { buildAvatarUrl, decodeAvatarUpload } from "../avatar";

export async function saveAvatar(
  {
    userId,
    contentType,
    data,
  }: {
    userId: string;
    contentType: string;
    data: string;
  },
  database: DbOrTx = db,
) {
  const { mimeType, bytes } = decodeAvatarUpload({ contentType, data });

  const [saved] = await database
    .insert(userAvatarTable)
    .values({
      userId,
      mimeType,
      size: bytes.length,
      data: bytes,
    })
    .onConflictDoUpdate({
      target: userAvatarTable.userId,
      set: {
        id: createId(),
        mimeType,
        size: bytes.length,
        data: bytes,
        updatedAt: new Date(),
      },
    })
    .returning({ id: userAvatarTable.id });

  if (!saved) {
    throw new Error("Failed to store avatar");
  }

  return { id: saved.id, url: buildAvatarUrl(saved.id), size: bytes.length };
}

export default saveAvatar;