Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

5.7 KB
Raw Blame
import { db, issue, issueAttachment, issueFieldValue } from '@repo/db';
import { and, eq, sql } from 'drizzle-orm';
import { iso, num } from '#shared/lib';
import {
  assertAttachmentStorageCapacity,
  lockAttachmentStorage,
  stripAttachmentEmbeds,
} from './storage';

// Data access for issue attachments. File bytes live in the S3-compatible object
// store (@repo/storage); these rows hold the metadata and the object key. publicId is
// the unguessable id used in the public download URL.

export interface AttachmentRow {
  id: number;
  publicId: string;
  issueId: number;
  s3Key: string;
  filename: string;
  contentType: string;
  sizeBytes: number;
  createdAt: string;
}

export function mapAttachment(row: typeof issueAttachment.$inferSelect): AttachmentRow {
  return {
    id: row.id,
    publicId: row.publicId,
    issueId: row.issueId,
    s3Key: row.s3Key,
    filename: row.filename,
    contentType: row.contentType,
    sizeBytes: num(row.sizeBytes),
    createdAt: iso(row.createdAt),
  };
}

export async function createAttachment(input: {
  projectId: number;
  issueId: number;
  s3Key: string;
  filename: string;
  contentType: string;
  sizeBytes: number;
}): Promise<AttachmentRow> {
  return db.transaction(async (tx) => {
    await lockAttachmentStorage(tx, input.projectId);
    await assertAttachmentStorageCapacity(input.projectId, input.sizeBytes, 0, tx);
    const [row] = await tx
      .insert(issueAttachment)
      .values({
        issueId: input.issueId,
        s3Key: input.s3Key,
        filename: input.filename,
        contentType: input.contentType,
        sizeBytes: input.sizeBytes,
      })
      .returning();
    return mapAttachment(row);
  });
}

export async function listAttachments(issueId: number): Promise<AttachmentRow[]> {
  const rows = await db
    .select()
    .from(issueAttachment)
    .where(eq(issueAttachment.issueId, issueId))
    .orderBy(issueAttachment.createdAt);
  return rows.map(mapAttachment);
}

// Bytes currently stored for a project, across every issue in it. Read before an
// upload to enforce the instance project quota.
export async function getProjectAttachmentBytes(projectId: number): Promise<number> {
  const rows = await db
    .select({ total: sql<string>`coalesce(sum(${issueAttachment.sizeBytes}), 0)` })
    .from(issueAttachment)
    .innerJoin(issue, eq(issue.id, issueAttachment.issueId))
    .where(eq(issue.projectId, projectId));
  return num(rows[0]?.total ?? 0);
}

export async function getAttachmentByPublicId(publicId: string): Promise<AttachmentRow | null> {
  const rows = await db
    .select()
    .from(issueAttachment)
    .where(eq(issueAttachment.publicId, publicId));
  return rows[0] ? mapAttachment(rows[0]) : null;
}

// Points an attachment at new bytes, keeping its row and its publicId. An embed
// of it in a description keeps working and shows the new file, which is what
// makes editing an attachment in place possible. Returns null if no row matched.
export async function replaceAttachmentContent(
  publicId: string,
  projectId: number,
  input: { s3Key: string; filename: string; contentType: string; sizeBytes: number },
): Promise<{ attachment: AttachmentRow; replacedS3Key: string } | null> {
  return db.transaction(async (tx) => {
    await lockAttachmentStorage(tx, projectId);
    const [current] = await tx
      .select({ attachment: issueAttachment })
      .from(issueAttachment)
      .innerJoin(issue, eq(issue.id, issueAttachment.issueId))
      .where(and(eq(issueAttachment.publicId, publicId), eq(issue.projectId, projectId)));
    if (!current) return null;
    await assertAttachmentStorageCapacity(
      projectId,
      input.sizeBytes,
      num(current.attachment.sizeBytes),
      tx,
    );
    const rows = await tx
      .update(issueAttachment)
      .set(input)
      .where(eq(issueAttachment.id, current.attachment.id))
      .returning();
    if (!rows[0]) return null;
    return {
      attachment: mapAttachment(rows[0]),
      replacedS3Key: current.attachment.s3Key,
    };
  });
}

// Deletes the row and returns it (with its s3Key) so the caller can remove the
// object from the store. Returns null if no row matched.
export async function deleteAttachmentByPublicId(publicId: string): Promise<AttachmentRow | null> {
  const rows = await db
    .delete(issueAttachment)
    .where(eq(issueAttachment.publicId, publicId))
    .returning();
  return rows[0] ? mapAttachment(rows[0]) : null;
}

// Remove embeds of one attachment from the issue description and its markdown
// custom field values. Called on delete so a removed attachment leaves no broken
// image behind.
export async function removeAttachmentEmbeds(issueId: number, publicId: string): Promise<void> {
  let changed = false;

  const [row] = await db
    .select({ description: issue.description })
    .from(issue)
    .where(eq(issue.id, issueId));
  if (row) {
    const next = stripAttachmentEmbeds(row.description, publicId);
    if (next !== row.description) {
      await db.update(issue).set({ description: next }).where(eq(issue.id, issueId));
      changed = true;
    }
  }

  const values = await db
    .select({ id: issueFieldValue.id, valueText: issueFieldValue.valueText })
    .from(issueFieldValue)
    .where(eq(issueFieldValue.issueId, issueId));
  for (const v of values) {
    if (v.valueText == null) continue;
    const next = stripAttachmentEmbeds(v.valueText, publicId);
    if (next !== v.valueText) {
      await db.update(issueFieldValue).set({ valueText: next }).where(eq(issueFieldValue.id, v.id));
      changed = true;
    }
  }

  // Stripping an embed is an edit of the issue, and sorting by "recently updated"
  // has to place it accordingly.
  if (changed) {
    await db
      .update(issue)
      .set({ updatedAt: sql`now()` })
      .where(eq(issue.id, issueId));
  }
}