import { prisma } from '@/db' import { logger } from '@/logger' import { AncestorFolder, AssetInfo, AttachmentInfo, ChildPreview, CommentInfo, CopyAssetsRequest, CreateAssetRequest, CreateCommentRequest, FieldValueInfo, GetAssetRequest, ListChildrenRequest, PreviewInfo, ReparentAssetsRequest, UpdateAssetNameRequest, UpdateAssetOrderRequest, UserInfo, } from '@/dtos/asset' import { Asset, AssetStatus, AssetType, Prisma, StorageKey, StorageKeyStatus } from '@/generated/prisma/client.ts' import { PaginatedData, paginateQuery, PaginationParams } from '@/services/pagination' import { s3Service } from '@/services/s3/s3' import { generateKeyBetween } from 'jittered-fractional-indexing' import * as path from 'path' import { ulid } from 'ulid' type AssetWithIncludes = Prisma.AssetGetPayload<{ include: { creator: true metadataValues: true storageKey: true target: { include: { creator: true metadataValues: true storageKey: true children: { include: { creator: true; metadataValues: true; storageKey: true } } } } children: { include: { creator: true; metadataValues: true; storageKey: true } } } }> type CommentWithIncludes = Prisma.AssetCommentGetPayload<{ include: { creator: true attachments: { include: { asset: { include: { storageKey: true } } } } replies: { include: { creator: true attachments: { include: { asset: { include: { storageKey: true } } } } } } } }> type AssetWithProjectTeam = Prisma.AssetGetPayload<{ include: { project: { include: { team: true } } } }> function generateSortIndex(previous?: string | null): string { if (!previous) return generateKeyBetween(null, null) return generateKeyBetween(null, previous) } export class AssetService { constructor(private readonly prismaClient: typeof prisma = prisma) {} async getDescendantFolderIds(folderId: string): Promise { const rows = await this.prismaClient.$queryRaw<{ id: string }[]>` WITH RECURSIVE descendant AS ( SELECT id, parent_id FROM assets WHERE parent_id = ${folderId} UNION ALL SELECT a.id, a.parent_id FROM assets a INNER JOIN descendant d ON a.parent_id = d.id ) SELECT id FROM descendant; ` const ids = rows.map((r) => r.id) return [folderId, ...ids] } async listAssetsByIds(ids: string[]): Promise { if (ids.length === 0) return [] const assets = await this.prismaClient.asset.findMany({ where: { id: { in: ids } }, include: { creator: true, metadataValues: true, storageKey: true, target: { include: { creator: true, metadataValues: true, storageKey: true, children: { include: { creator: true, metadataValues: true, storageKey: true }, take: 3, orderBy: { sortIndex: 'asc' }, }, }, }, children: { include: { creator: true, metadataValues: true, storageKey: true }, take: 3, orderBy: { sortIndex: 'asc' }, }, }, }) const assetMap = new Map(assets.map((a) => [a.id, a])) const orderedAssets = ids .map((id) => assetMap.get(id)) .filter((a): a is AssetWithIncludes => !!a) const orderedInfos = await this.toAssetInfos(orderedAssets) return orderedInfos } async updateAncestorsSize(tx: Prisma.TransactionClient, startNodeId: string, sizeDelta: number) { if (sizeDelta === 0) return const rows = await tx.$queryRaw<{ id: string }[]>` WITH RECURSIVE ancestor AS ( SELECT id, parent_id FROM assets WHERE id = ${startNodeId} UNION ALL SELECT a.id, a.parent_id FROM assets a INNER JOIN ancestor d ON a.id = d.parent_id ) SELECT id FROM ancestor; ` const ancestorIds = rows.map((r) => r.id) if (ancestorIds.length > 0) { await tx.asset.updateMany({ where: { id: { in: ancestorIds } }, data: { sizeByte: { increment: sizeDelta } }, }) } } async dissolveStackIfEmpty(tx: Prisma.TransactionClient, stack: Asset) { if (stack.type !== AssetType.version_stack) return const count = await tx.asset.count({ where: { parentId: stack.id } }) if (count === 0) { if (stack.parentId) { await tx.asset.update({ where: { id: stack.parentId }, data: { fileCount: { decrement: 1 } }, }) } await tx.asset.delete({ where: { id: stack.id } }) } else if (count === 1) { const lastChild = await tx.asset.findFirst({ where: { parentId: stack.id }, }) if (!lastChild) return if (!stack.parentId) throw new Error('Stack has no parent') await tx.asset.update({ where: { id: lastChild.id }, data: { parentId: stack.parentId, sortIndex: null }, }) await tx.asset.delete({ where: { id: stack.id } }) } } async reparentAssets(req: ReparentAssetsRequest): Promise { await this.prismaClient.$transaction(async (tx) => { const newParent = await tx.asset.findUnique({ where: { id: req.newParentId }, include: { project: { include: { team: true } } }, }) if (!newParent) throw new Error('New parent not found') if (!newParent.project) throw new Error('New parent folder is not associated with a project') if (newParent.type === AssetType.file) { return this.reparentFileToFile(tx, newParent, req.assetIds, req.creatorId) } const assetsToMove = await tx.asset.findMany({ where: { id: { in: req.assetIds } }, include: { project: { include: { team: true } } }, }) if (assetsToMove.length !== req.assetIds.length) { throw new Error('Not all assets to be moved were found') } if (assetsToMove.length === 0) return let totalSize = 0 const oldParentId = assetsToMove[0].parentId if (!oldParentId) { throw new Error(`Asset ${assetsToMove[0].id} has no parent`) } for (const a of assetsToMove) { if (!a.project || !a.project.team) { throw new Error(`Asset ${a.id} is not associated with a team`) } if (!newParent.project.team) { throw new Error('New parent folder is not associated with a team') } if (a.project.team.id !== newParent.project.team.id) { throw new Error(`Cannot move asset ${a.id} to a different team`) } if (a.parentId !== oldParentId) { throw new Error('All assets must be moved from the same parent folder') } totalSize += a.sizeByte } const totalCount = assetsToMove.length // Sibling folder move if (newParent.parentId && oldParentId === newParent.parentId) { await tx.asset.update({ where: { id: oldParentId }, data: { fileCount: { decrement: totalCount } }, }) await tx.asset.update({ where: { id: newParent.id }, data: { fileCount: { increment: totalCount }, sizeByte: { increment: totalSize }, }, }) } else { await tx.asset.update({ where: { id: oldParentId }, data: { fileCount: { decrement: totalCount } }, }) await this.updateAncestorsSize(tx, oldParentId, -totalSize) await tx.asset.update({ where: { id: newParent.id }, data: { fileCount: { increment: totalCount } }, }) await this.updateAncestorsSize(tx, newParent.id, totalSize) } const firstFile = await tx.asset.findFirst({ where: { parentId: newParent.id }, orderBy: { sortIndex: 'asc' }, }) let firstSortIndex = firstFile?.sortIndex || null for (let i = req.assetIds.length - 1; i >= 0; i--) { const assetId = req.assetIds[i] const newSortIndex = generateSortIndex(firstSortIndex) await tx.asset.update({ where: { id: assetId }, data: { parentId: newParent.id, sortIndex: newSortIndex }, }) firstSortIndex = newSortIndex } const oldParent = await tx.asset.findUnique({ where: { id: oldParentId }, }) if (oldParent) { await this.dissolveStackIfEmpty(tx, oldParent) } }) } async copyAssets(req: CopyAssetsRequest): Promise { await this.prismaClient.$transaction(async (tx) => { const newParent = await tx.asset.findUnique({ where: { id: req.newParentId }, include: { project: { include: { team: true } } }, }) if (!newParent) throw new Error('New parent not found') if (!newParent.project) throw new Error('New parent folder is not associated with a project') const assetsToCopy = await tx.asset.findMany({ where: { id: { in: req.assetIds } }, include: { project: { include: { team: true } } }, }) if (assetsToCopy.length !== req.assetIds.length) { throw new Error('Not all assets to be copied were found') } for (const a of assetsToCopy) { if (!a.project || !a.project.team) { throw new Error(`Asset ${a.id} is not associated with a team`) } if (!newParent.project.team) { throw new Error('New parent folder is not associated with a team') } if (a.project.team.id !== newParent.project.team.id) { throw new Error(`Cannot copy asset ${a.id} to a different team`) } } const firstFile = await tx.asset.findFirst({ where: { parentId: newParent.id }, orderBy: { sortIndex: 'asc' }, }) let firstSortIndex = firstFile?.sortIndex || null let totalSize = 0 for (let i = req.assetIds.length - 1; i >= 0; i--) { const assetId = req.assetIds[i] const asset = assetsToCopy.find((a) => a.id === assetId)! const newSortIndex = generateSortIndex(firstSortIndex) const copiedAsset = await this.copyAssetRecursive( tx, asset, newParent.id, newParent.projectId!, newSortIndex, req.creatorId, req.withComments, ) totalSize += copiedAsset.sizeByte firstSortIndex = newSortIndex } await tx.asset.update({ where: { id: newParent.id }, data: { fileCount: { increment: assetsToCopy.length }, }, }) await this.updateAncestorsSize(tx, newParent.id, totalSize) }) } private async copyAssetRecursive( tx: Prisma.TransactionClient, asset: Asset & { storageKey?: StorageKey | null }, newParentId: string | null, projectId: string | null, sortIndex: string | null, creatorId?: string, withComments: boolean = false, ): Promise { const newAsset = await tx.asset.create({ data: { name: asset.name, nameNgram: asset.nameNgram, type: asset.type, mediaType: asset.mediaType, fileCount: asset.fileCount, sizeByte: asset.sizeByte, status: asset.status, transcodeTaskId: asset.transcodeTaskId, media: (asset.media as any) || undefined, isDeleted: asset.isDeleted, deletedAt: asset.deletedAt, sortIndex: sortIndex, parentId: newParentId, projectId: projectId, creatorId: creatorId || asset.creatorId, taskId: asset.taskId, // eslint-disable-next-line @typescript-eslint/no-explicit-any targetId: asset.targetId as any, storageKeyId: asset.storageKeyId, }, }) if (withComments) { await this.copyComments(tx, asset.id, newAsset.id, creatorId) } if (asset.type === AssetType.folder) { const children = await tx.asset.findMany({ where: { parentId: asset.id }, orderBy: { sortIndex: 'asc' }, }) for (const child of children) { await this.copyAssetRecursive( tx, child, newAsset.id, projectId, child.sortIndex, creatorId, withComments, ) } } return newAsset } private async copyComments( tx: Prisma.TransactionClient, oldAssetId: string, newAssetId: string, creatorId?: string, ) { const comments = await tx.assetComment.findMany({ where: { assetId: oldAssetId, replyToId: null }, include: { attachments: { include: { asset: true } }, replies: { include: { attachments: { include: { asset: true } }, }, }, }, }) for (const comment of comments) { await this.copyCommentRecursive(tx, comment, newAssetId, null, creatorId) } } private async copyCommentRecursive( tx: Prisma.TransactionClient, comment: any, // eslint-disable-line @typescript-eslint/no-explicit-any newAssetId: string, newReplyToId: string | null, creatorId?: string, ) { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { id, createdAt, updatedAt, assetId, replyToId, attachments, replies, ...data } = comment const newComment = await tx.assetComment.create({ data: { ...data, assetId: newAssetId, replyToId: newReplyToId, creatorId: creatorId || comment.creatorId, }, }) if (attachments) { for (const attachment of attachments) { // Deep copy attachment asset const copiedAttachmentAsset = await this.copyAssetRecursive( tx, attachment.asset, null, // attachment usually has no parent attachment.asset.projectId, attachment.asset.sortIndex, creatorId, false, ) await tx.assetCommentAttachment.create({ data: { commentId: newComment.id, assetId: copiedAttachmentAsset.id, }, }) } } if (replies) { for (const reply of replies) { await this.copyCommentRecursive(tx, reply, newAssetId, newComment.id, creatorId) } } } private async reparentFileToFile( tx: Prisma.TransactionClient, destFile: AssetWithProjectTeam, assetIds: string[], creatorId?: string, ) { if (assetIds.length !== 1) { throw new Error('Can only reparent one file to another file') } const sourceId = assetIds[0] if (sourceId === destFile.id) { throw new Error('Cannot reparent a file to itself') } const sourceAsset = await tx.asset.findUnique({ where: { id: sourceId }, include: { project: { include: { team: true } } }, }) if (!sourceAsset) throw new Error('Source asset not found') if (!sourceAsset.project || !sourceAsset.project.team) { throw new Error(`Asset ${sourceAsset.id} is not associated with a team`) } if (!destFile.project || !destFile.project.team) { throw new Error('Destination file is not associated with a team') } if (sourceAsset.project.team.id !== destFile.project.team.id) { throw new Error(`Cannot move asset ${sourceAsset.id} to a different team`) } const oldParentId = sourceAsset.parentId if (!oldParentId) { throw new Error(`Asset ${sourceAsset.id} has no parent`) } const stackParentId = destFile.parentId if (!stackParentId) { throw new Error(`Destination file ${destFile.id} has no parent`) } const stack = await tx.asset.create({ data: { type: AssetType.version_stack, status: 'uploaded', projectId: destFile.project.id, parentId: stackParentId, creatorId: creatorId, name: '', }, }) const isUploading = sourceAsset.status === 'uploading' if (!isUploading) { await tx.asset.update({ where: { id: oldParentId }, data: { fileCount: { decrement: 1 } }, }) await this.updateAncestorsSize(tx, oldParentId, -sourceAsset.sizeByte) await this.updateAncestorsSize(tx, stackParentId, sourceAsset.sizeByte) } const firstIndex = generateKeyBetween(null, null) const secondIndex = generateKeyBetween(firstIndex, null) await tx.asset.update({ where: { id: destFile.id }, data: { parentId: stack.id, sortIndex: secondIndex }, }) await tx.asset.update({ where: { id: sourceAsset.id }, data: { parentId: stack.id, sortIndex: firstIndex }, }) await tx.asset.update({ where: { id: stack.id }, data: { fileCount: isUploading ? 1 : 2, sizeByte: isUploading ? destFile.sizeByte : destFile.sizeByte + sourceAsset.sizeByte, }, }) const oldParent = await tx.asset.findUnique({ where: { id: oldParentId } }) if (oldParent) { await this.dissolveStackIfEmpty(tx, oldParent) } } async listChildren(req: ListChildrenRequest): Promise> { const typesToQuery: AssetType[] = [req.assetType as AssetType] if (req.assetType === AssetType.file) { typesToQuery.push(AssetType.version_stack) } let where: Prisma.AssetWhereInput = {} if (req.showDeleted) { if (!req.projectId) { throw new Error('ProjectID is required when ShowDeleted is true') } const thirtyDaysAgo = new Date() thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30) where = { projectId: req.projectId, isDeleted: true, deletedAt: { gte: thirtyDaysAgo }, OR: [ { type: { in: typesToQuery } }, { type: AssetType.symlink, target: { type: { in: typesToQuery } } }, ], } } else { if (!req.assetId) { throw new Error('AssetId is required when ShowDeleted is false') } let parentId = req.assetId const asset = await this.prismaClient.asset.findUnique({ where: { id: parentId }, }) if (asset?.type === AssetType.symlink && asset.targetId) { parentId = asset.targetId } where = { parentId, isDeleted: false, OR: [ { type: { in: typesToQuery } }, { type: AssetType.symlink, target: { type: { in: typesToQuery } } }, ], } } if (req.prefix) { where.name = { startsWith: req.prefix } } let orderBy: Prisma.AssetOrderByWithRelationInput = {} const dir = req.order === 'desc' ? 'desc' : 'asc' switch (req.sort) { case 'createdAt': orderBy = { id: dir } break case 'name': orderBy = { name: dir } break case 'size': orderBy = { sizeByte: dir } break case 'index': default: orderBy = { sortIndex: 'asc' } break } const { data: assets, pageInfo } = await paginateQuery( async (skip, take) => { return this.prismaClient.asset.findMany({ where, include: { creator: true, metadataValues: true, storageKey: true, target: { include: { creator: true, metadataValues: true, storageKey: true, children: { include: { creator: true, metadataValues: true, storageKey: true }, take: 3, orderBy: { sortIndex: 'asc' }, }, }, }, children: { include: { creator: true, metadataValues: true, storageKey: true }, take: 3, orderBy: { sortIndex: 'asc' }, }, }, orderBy, skip, take, }) }, async () => this.prismaClient.asset.count({ where }), req, ) const infos = await this.toAssetInfos(assets) return { data: infos, pageInfo } } async createAsset(req: CreateAssetRequest): Promise { let projectId = req.projectId let parent: Prisma.AssetGetPayload<{ include: { project: true } }> | null = null if (req.parentId) { parent = await this.prismaClient.asset.findUnique({ where: { id: req.parentId }, include: { project: true }, }) if (!parent) throw new Error('Parent not found') projectId = parent.projectId ?? undefined } if (!projectId) throw new Error('projectId is empty') let sortIndex: string | null = null if (parent) { const firstFile = await this.prismaClient.asset.findFirst({ where: { parentId: parent.id }, orderBy: { sortIndex: 'asc' }, }) sortIndex = generateSortIndex(firstFile?.sortIndex) } const data: Prisma.AssetCreateInput = { name: req.name, type: req.type as AssetType, status: 'uploaded', project: { connect: { id: projectId } }, } if (parent) data.parent = { connect: { id: parent.id } } if (sortIndex) data.sortIndex = sortIndex if (req.key) { ;(data as any).storageKey = { connectOrCreate: { where: { key: req.key }, create: { key: req.key }, }, } } if (req.sizeByte) data.sizeByte = req.sizeByte if (req.contentType) data.mediaType = req.contentType if (req.creatorId) data.creator = { connect: { id: req.creatorId } } const asset = await this.prismaClient.asset.create({ data, include: { creator: true, metadataValues: true, storageKey: true, target: { include: { creator: true, metadataValues: true, storageKey: true, children: { include: { creator: true, metadataValues: true, storageKey: true }, take: 3, orderBy: { sortIndex: 'asc' }, }, }, }, children: { include: { creator: true, metadataValues: true, storageKey: true }, take: 3, orderBy: { sortIndex: 'asc' }, }, }, }) const infos = await this.toAssetInfos([asset]) return infos[0] } async findAsset(parentId: string, name: string): Promise { const asset = await this.prismaClient.asset.findFirst({ where: { parentId, name }, }) if (!asset) return null return this.getAsset({ assetId: asset.id }) } async getAsset(req: GetAssetRequest): Promise { const a = await this.prismaClient.asset.findUnique({ where: { id: req.assetId }, include: { creator: true, metadataValues: true, storageKey: true, target: { include: { creator: true, metadataValues: true, storageKey: true, children: { include: { creator: true, metadataValues: true, storageKey: true }, take: 3, orderBy: { sortIndex: 'asc' }, }, }, }, children: { include: { creator: true, metadataValues: true, storageKey: true }, take: 3, orderBy: { sortIndex: 'asc' }, }, }, }) if (!a) throw new Error('Asset not found') const rows = await this.prismaClient.$queryRaw<{ id: string; name: string; type: string }[]>` WITH RECURSIVE ancestor AS ( SELECT id, parent_id, name, type::text FROM assets WHERE id = ${a.id} UNION ALL SELECT a.id, a.parent_id, a.name, a.type::text FROM assets a INNER JOIN ancestor d ON d.parent_id = a.id ) SELECT id, name, type FROM ancestor; ` const afs: AncestorFolder[] = [] for (const row of rows) { if (row.id === a.id || row.type === 'root' || row.type === 'version_stack') continue afs.push({ id: row.id, name: row.name }) } const infos = await this.toAssetInfos([a]) const info = infos[0] info.ancestorFolders = afs if (info.media) { if (info.media.imageTranscodes) { for (const t of info.media.imageTranscodes) { t.url = await s3Service.presign(process.env.S3_BUCKET || 'shumai', t.key, 'GET') } } if (info.media.videoTranscodes) { for (const t of info.media.videoTranscodes) { t.url = await s3Service.presign(process.env.S3_BUCKET || 'shumai', t.key, 'GET') } } if (info.media.original?.key) { info.media.original.downloadUrl = await s3Service.presign( process.env.S3_BUCKET || 'shumai', info.media.original.key, 'GET', ) } } return info } async resolveTargetAssetId(assetId: string): Promise { const asset = await this.prismaClient.asset.findUnique({ where: { id: assetId }, select: { type: true, targetId: true }, }) if (asset?.type === AssetType.symlink && asset.targetId) { return asset.targetId } return assetId } async resolveLatestVersionId(assetId: string): Promise { const asset = await this.prismaClient.asset.findUnique({ where: { id: assetId }, select: { id: true, type: true, targetId: true }, }) if (!asset) return assetId let currentId = asset.id let currentType = asset.type if (asset.type === AssetType.symlink && asset.targetId) { const target = await this.prismaClient.asset.findUnique({ where: { id: asset.targetId }, select: { id: true, type: true }, }) if (target) { currentId = target.id currentType = target.type } } if (currentType === AssetType.version_stack) { const latestChild = await this.prismaClient.asset.findFirst({ where: { parentId: currentId, isDeleted: false }, orderBy: { sortIndex: 'desc' }, select: { id: true }, }) if (latestChild) { return latestChild.id } } return currentId } async updateAssetName(req: UpdateAssetNameRequest): Promise { const asset = await this.prismaClient.asset.update({ where: { id: req.id }, data: { name: req.name }, include: { creator: true, metadataValues: true, target: { include: { creator: true, metadataValues: true, storageKey: true, children: { include: { creator: true, metadataValues: true, storageKey: true }, take: 3, orderBy: { sortIndex: 'asc' }, }, }, }, children: { include: { creator: true, metadataValues: true, storageKey: true }, take: 3, orderBy: { sortIndex: 'asc' }, }, storageKey: true, }, }) const infos = await this.toAssetInfos([asset as any]) return infos[0] } async updateAssetOrder(id: string, req: UpdateAssetOrderRequest): Promise { const asset = await this.prismaClient.asset.findUnique({ where: { id }, }) if (!asset) throw new Error('Asset not found') let afterSortIndex: string | null = null let beforeSortIndex: string | null = null if (req.beforeIndex) { beforeSortIndex = req.beforeIndex const prevAsset = await this.prismaClient.asset.findFirst({ where: { parentId: asset.parentId, sortIndex: { lt: req.beforeIndex }, id: { not: id }, }, orderBy: { sortIndex: 'desc' }, }) afterSortIndex = prevAsset?.sortIndex || null } else if (req.afterIndex) { afterSortIndex = req.afterIndex beforeSortIndex = null } const newSortIndex = generateKeyBetween(afterSortIndex, beforeSortIndex) await this.prismaClient.asset.update({ where: { id }, data: { sortIndex: newSortIndex }, }) return this.getAsset({ assetId: id }) } async deleteAssets(ids: string[]): Promise { for (const id of ids) { await this.prismaClient.$transaction(async (tx) => { const a = await tx.asset.findUnique({ where: { id }, include: { project: { include: { team: true } } }, }) if (!a) throw new Error('Asset not found') if (!a.parentId) throw new Error('Asset has no parent') await tx.asset.update({ where: { id: a.parentId }, data: { fileCount: { decrement: 1 } }, }) const rows = await tx.$queryRaw<{ id: string }[]>` WITH RECURSIVE descendant AS ( SELECT id, parent_id FROM assets WHERE parent_id = ${a.id} UNION ALL SELECT a.id, a.parent_id FROM assets a INNER JOIN descendant d ON a.parent_id = d.id ) SELECT id FROM descendant; ` const descendantIds = rows.map((r) => r.id) if (descendantIds.length > 0) { await tx.asset.updateMany({ where: { id: { in: descendantIds } }, data: { isDeleted: true }, }) } await tx.asset.update({ where: { id: a.id }, data: { isDeleted: true, status: 'trashed', deletedAt: new Date(), }, }) await this.updateAncestorsSize(tx, a.parentId, -a.sizeByte) }) } } private cleanupJobsRunning = false async startCleanupJob() { this.cleanupJobsRunning = true const runCleanup = async () => { if (!this.cleanupJobsRunning) return try { await this.expireTrashedAssets() await this.purgePendingAssets() await this.purgeUnreferencedStorageKeys() } catch (e: unknown) { console.error('Error in asset cleanup job:', e) } if (this.cleanupJobsRunning) { setTimeout(runCleanup, 5000) // Run every 5 seconds after previous run completion } } runCleanup() } stopCleanupJob() { this.cleanupJobsRunning = false } /** * Stage 1: Find root assets that have been in the trash for > 30 days. * Cascade the 'pending_purge' status to all their descendants. */ private async expireTrashedAssets() { const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) // Find up to 100 expired roots, ordered by oldest first to avoid starvation const expiredRoots = await this.prismaClient.asset.findMany({ where: { status: 'trashed', deletedAt: { lt: thirtyDaysAgo }, isDeleted: true, }, orderBy: { deletedAt: 'asc' }, select: { id: true }, take: 100, }) for (const root of expiredRoots) { await this.prismaClient.$transaction(async (tx) => { // Use recursive CTE to find all descendants and mark them all as pending_purge await tx.$executeRaw` WITH RECURSIVE descendant AS ( SELECT id FROM assets WHERE id = ${root.id} UNION ALL SELECT a.id FROM assets a INNER JOIN descendant d ON a.parent_id = d.id ) UPDATE assets SET status = 'pending_purge', updated_at = NOW() WHERE id IN (SELECT id FROM descendant); ` }) } } /** * Stage 2: Delete DB records for assets marked as 'pending_purge'. */ private async purgePendingAssets() { const { count } = await this.prismaClient.asset.deleteMany({ where: { status: AssetStatus.pending_purge }, }) if (count > 0) { logger.info( { purgedCount: count, }, `${count} assets purged from database`, ) } } /** * Stage 3: Garbage Collection - Physically delete files and StorageKey records * that have no associated logical assets. */ private async purgeUnreferencedStorageKeys() { const bucket = process.env.S3_BUCKET || 'shumai' // 1. Find storage keys with no associated assets and older than 24 hours // using FOR UPDATE SKIP LOCKED to ensure multiple servers don't pick up the same keys. const lockedKeys = await this.prismaClient.$queryRaw<{ id: string; key: string }[]>` WITH to_purge AS ( SELECT sk.id FROM storage_keys sk LEFT JOIN assets a ON a.storage_key_id = sk.id WHERE a.id IS NULL AND sk.created_at < NOW() - INTERVAL '24 hours' AND (sk.status = 'active' OR (sk.status = 'purging' AND sk.updated_at < NOW() - INTERVAL '1 hour')) LIMIT 100 FOR UPDATE SKIP LOCKED ) UPDATE storage_keys SET status = 'purging', updated_at = NOW() WHERE id IN (SELECT id FROM to_purge) RETURNING id, key; ` if (lockedKeys.length === 0) return let physicalFilesDeleted = 0 let purgedCount = 0 for (const sk of lockedKeys) { try { // Physically delete from S3 const parts = sk.key.split('/') if (parts.length > 2) { const prefix = parts.slice(0, parts.length - 1).join('/') + '/' const count = await s3Service.deletePrefix(bucket, prefix) physicalFilesDeleted += count } else { const count = await s3Service.deleteObject(bucket, sk.key) physicalFilesDeleted += count } // Delete from database await this.prismaClient.storageKey.delete({ where: { id: sk.id }, }) purgedCount++ } catch (e: unknown) { logger.error( { key: sk.key, error: e instanceof Error ? e.message : String(e) }, 'Failed to purge unreferenced storage key', ) } } if (purgedCount > 0) { logger.info( { purgedCount, physicalFilesDeleted }, `Garbage collection: purged ${purgedCount} storage keys and ${physicalFilesDeleted} physical files`, ) } } async restoreAssets(ids: string[]): Promise { for (const id of ids) { await this.prismaClient.$transaction(async (tx) => { const a = await tx.asset.findUnique({ where: { id }, include: { project: { include: { team: true } } }, }) if (!a) throw new Error('Asset not found') if (!a.parentId) throw new Error('Asset has no parent') await tx.asset.update({ where: { id: a.parentId }, data: { fileCount: { increment: 1 } }, }) const rows = await tx.$queryRaw<{ id: string }[]>` WITH RECURSIVE descendant AS ( SELECT id, parent_id FROM assets WHERE parent_id = ${a.id} UNION ALL SELECT a.id, a.parent_id FROM assets a INNER JOIN descendant d ON a.parent_id = d.id ) SELECT id FROM descendant; ` const descendantIds = rows.map((r) => r.id) if (descendantIds.length > 0) { await tx.asset.updateMany({ where: { id: { in: descendantIds } }, data: { isDeleted: false }, }) } await tx.asset.update({ where: { id: a.id }, data: { isDeleted: false, status: 'processed', deletedAt: null, }, }) await this.updateAncestorsSize(tx, a.parentId, a.sizeByte) }) } } async createComment(req: CreateCommentRequest): Promise { let commentId = '' const resolvedAssetId = await this.resolveLatestVersionId(req.assetId) await this.prismaClient.$transaction(async (tx) => { const a = await tx.asset.findUnique({ where: { id: resolvedAssetId }, include: { project: { include: { team: true } } }, }) if (!a) throw new Error('Asset not found') let actualReplyToId = req.replyToId let parentComment = null if (req.replyToId) { parentComment = await tx.assetComment.findUnique({ where: { id: req.replyToId }, include: { creator: true }, }) if (parentComment?.replyToId) { actualReplyToId = parentComment.replyToId } } const comment = await tx.assetComment.create({ data: { assetId: a.id, creatorId: req.userId, message: req.message, annotation: req.annotations, second: req.second, replyToId: actualReplyToId, }, }) commentId = comment.id for (const attachmentId of req.attachmentIds) { const attachmentAsset = await tx.asset.findUnique({ where: { id: attachmentId }, }) if (attachmentAsset) { await tx.assetCommentAttachment.create({ data: { assetId: attachmentAsset.id, commentId: comment.id, }, }) } } // Parse bot mentions const botMentionMatches = [...req.message.matchAll(/<@([^>]+)>/g)] const mentionedAgentIds = new Set(botMentionMatches.map((match) => match[1])) const handledAgentIds = new Set() if (parentComment && a.project) { const rootSessionId = parentComment.sessionId const isRootAgent = !!rootSessionId || parentComment.creator?.type === 'agent' const rootAgentId = parentComment.creatorId if (isRootAgent && rootAgentId) { const explicitMention = mentionedAgentIds.has(rootAgentId) await tx.workflowTask.create({ data: { assetId: a.id, type: 'chat', status: 'pending', teamId: a.project.team.id, projectId: a.project.id, payload: { userCommentId: comment.id, agentId: rootAgentId, projectId: a.project.id, sessionId: rootSessionId || undefined, explicitMention, }, }, }) handledAgentIds.add(rootAgentId) } } // Handle any other explicitly mentioned agents for (const agentId of mentionedAgentIds) { if (handledAgentIds.has(agentId)) continue let foundAgent = false if (agentId === 'default') { foundAgent = true } else { const agent = await tx.agent.findUnique({ where: { id: agentId } }) if (agent) foundAgent = true } if (foundAgent && a.project) { await tx.workflowTask.create({ data: { assetId: a.id, type: 'chat', status: 'pending', teamId: a.project.team.id, projectId: a.project.id, payload: { userCommentId: comment.id, agentId: agentId, projectId: a.project.id, explicitMention: true, }, }, }) handledAgentIds.add(agentId) } } }) return this.getComment(commentId) } async getComment(commentId: string): Promise { const c = await this.prismaClient.assetComment.findUnique({ where: { id: commentId }, include: { creator: true, asset: { include: { storageKey: true } }, attachments: { include: { asset: { include: { storageKey: true } } } }, replies: { include: { creator: true, attachments: { include: { asset: { include: { storageKey: true } } } }, }, orderBy: { id: 'asc' }, }, }, }) if (!c) throw new Error('Comment not found') return this.toCommentInfo(c) } async listComments( assetId: string, params: PaginationParams, ): Promise> { const resolvedAssetId = await this.resolveLatestVersionId(assetId) const { data: comments, pageInfo } = await paginateQuery( async (skip, take) => { return this.prismaClient.assetComment.findMany({ where: { assetId: resolvedAssetId, replyToId: null }, include: { creator: true, asset: { include: { storageKey: true } }, attachments: { include: { asset: { include: { storageKey: true } } } }, replies: { include: { creator: true, attachments: { include: { asset: { include: { storageKey: true } } } }, }, orderBy: { id: 'asc' }, }, }, skip, take, orderBy: { id: 'asc' }, }) }, async () => this.prismaClient.assetComment.count({ where: { assetId, replyToId: null } }), params, ) const infos = await Promise.all(comments.map((c) => this.toCommentInfo(c))) return { data: infos, pageInfo } } private async toAssetInfos(assets: AssetWithIncludes[]): Promise { const stackIds = new Set() for (const a of assets) { if ( a.type === AssetType.version_stack || (a.type === AssetType.symlink && a.target?.type === AssetType.version_stack) ) { stackIds.add(a.type === AssetType.symlink ? a.targetId! : a.id) } const folderForPreview = a.type === AssetType.symlink && a.target?.type === AssetType.folder ? a.target : a if ( (folderForPreview.type === AssetType.folder || folderForPreview.type === AssetType.share_root || folderForPreview.type === AssetType.share) && 'children' in folderForPreview && folderForPreview.children ) { for (const child of folderForPreview.children) { if (child.type === AssetType.version_stack) { stackIds.add(child.id) } } } } const versionsMap = new Map() if (stackIds.size > 0) { const allVersions = await this.prismaClient.asset.findMany({ where: { parentId: { in: Array.from(stackIds) }, isDeleted: false }, include: { creator: true, metadataValues: true, storageKey: true }, }) for (const v of allVersions) { const list = versionsMap.get(v.parentId!) || [] list.push(v) versionsMap.set(v.parentId!, list) } for (const list of versionsMap.values()) { list.sort((x, y) => { if (!x.sortIndex || !y.sortIndex) return 0 return x.sortIndex < y.sortIndex ? -1 : x.sortIndex > y.sortIndex ? 1 : 0 }) } } const result: AssetInfo[] = [] for (const a of assets) { let latestVersion: | AssetWithIncludes | AssetWithIncludes['children'][0] | NonNullable = a let versionStack: AssetInfo['versionStack'] = null if ( a.type === AssetType.version_stack || (a.type === AssetType.symlink && a.target?.type === AssetType.version_stack) ) { const stackId = a.type === AssetType.symlink ? a.targetId! : a.id const versions = versionsMap.get(stackId) || [] if (versions.length > 0) { latestVersion = versions[0] versionStack = { id: stackId, versions: await Promise.all( versions.map(async (v, i) => { const preview = await this.toPreviewInfo(v as Asset) return { version: versions.length - i, current: i === 0, id: v.id, name: v.name, previewUrl: preview?.thumbnailUrl || null, creator: v.creator ? { id: v.creator.id, name: v.creator.name } : null, } }), ), } } } else if (a.type === AssetType.symlink) { if (a.target) { latestVersion = a.target } } const latestChildren: ChildPreview[] = [] const folderForPreview = a.type === AssetType.symlink && a.target?.type === AssetType.folder ? a.target : a if ( (folderForPreview.type === AssetType.folder || folderForPreview.type === AssetType.share_root || folderForPreview.type === AssetType.share) && 'children' in folderForPreview && folderForPreview.children ) { for (const child of folderForPreview.children) { let previewAsset = child as Asset let mediaType = child.mediaType if (child.type === AssetType.version_stack) { const versions = versionsMap.get(child.id) || [] if (versions.length > 0) { const latestChild = versions[0] previewAsset = latestChild as Asset mediaType = latestChild.mediaType } } latestChildren.push({ type: mediaType, preview: await this.toPreviewInfo(previewAsset), }) } } const preview = await this.toPreviewInfo(latestVersion as Asset) const creator = latestVersion.creator ? { id: latestVersion.creator.id, name: latestVersion.creator.name } : null const fieldValues: FieldValueInfo[] = [] if ('metadataValues' in latestVersion && latestVersion.metadataValues) { for (const mv of latestVersion.metadataValues) { let value: unknown = null if (mv.jsonValue !== null) { value = mv.jsonValue } else if (mv.dateValue !== null) { value = mv.dateValue.toISOString() } else if (mv.stringValue !== null) { value = mv.stringValue } else if (mv.numberValue !== null) { value = mv.numberValue } else if (mv.booleanValue !== null) { value = mv.booleanValue } fieldValues.push({ fieldId: mv.fieldId, value }) } } const media = latestVersion.media as PrismaJson.MediaInfo | null if (media && media.videoPreview?.key) { media.videoPreview.url = await s3Service.presign( process.env.S3_BUCKET || 'shumai', media.videoPreview.key, 'GET', ) } const key = latestVersion.storageKey?.key if (media && key) { media.original = { key, downloadUrl: await s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET'), filesizeInBytes: latestVersion.sizeByte, codec: '', // TODO: extract from metadata if needed } } result.push({ id: a.id, name: a.type === AssetType.version_stack && (a.name === '' || !a.name) ? latestVersion.name : a.name, sizeByte: latestVersion.sizeByte, fileCount: latestVersion.fileCount, type: a.type, targetType: a.type === AssetType.symlink ? a.target?.type : null, status: latestVersion.status, mediaType: latestVersion.mediaType, latestChildren, preview, createdAt: a.createdAt.toISOString(), updatedAt: a.updatedAt.toISOString(), deletedAt: a.deletedAt ? a.deletedAt.toISOString() : null, creator, fieldValues, sortIndex: a.sortIndex, media: media as unknown as AssetInfo['media'], versionStack, }) } return result } async getStackVersions(stackId: string): Promise< Array<{ id: string version: number name: string previewUrl: string | null creator: { id: string; name: string | null } | null }> > { const versions = await this.prismaClient.asset.findMany({ where: { parentId: stackId, isDeleted: false }, include: { creator: true }, }) versions.sort((x, y) => { if (!x.sortIndex || !y.sortIndex) return 0 return x.sortIndex < y.sortIndex ? -1 : x.sortIndex > y.sortIndex ? 1 : 0 }) return await Promise.all( versions.map(async (v, i) => { const preview = await this.toPreviewInfo(v) return { id: v.id, version: versions.length - i, name: v.name, previewUrl: preview?.thumbnailUrl || null, creator: v.creator ? { id: v.creator.id, name: v.creator.name } : null, } }), ) } private async toPreviewInfo(asset: Asset | AssetWithIncludes): Promise { if (!asset.media) return null let thumbnailUrl = undefined if (asset.mediaType?.startsWith('image/') && asset.media.thumbnail?.key) { thumbnailUrl = await s3Service.presign( process.env.S3_BUCKET || 'shumai', asset.media.thumbnail.key, 'GET', ) } else if (asset.mediaType?.startsWith('video/') && asset.media.poster?.key) { thumbnailUrl = await s3Service.presign( process.env.S3_BUCKET || 'shumai', asset.media.poster.key, 'GET', ) } let spriteUrl = undefined if (asset.media.sprite?.key) { spriteUrl = await s3Service.presign( process.env.S3_BUCKET || 'shumai', asset.media.sprite.key, 'GET', ) } return { mediaType: asset.mediaType, thumbnailUrl, originalHeight: asset.media.metadata?.originalHeight, originalWidth: asset.media.metadata?.originalWidth, spriteUrl, } } private async toCommentInfo( c: CommentWithIncludes | CommentWithIncludes['replies'][0], ): Promise { const replies: CommentInfo[] = [] if ('replies' in c && c.replies) { for (const r of c.replies) { replies.push(await this.toCommentInfo(r)) } } const attachments: AttachmentInfo[] = [] if (c.attachments) { for (const a of c.attachments) { if (a.asset?.storageKey?.key) { const url = await s3Service.presign( process.env.S3_BUCKET || 'shumai', a.asset.storageKey.key, 'GET', ) attachments.push({ id: a.id, assetId: a.asset.id, url, mediaType: a.asset.mediaType, }) } } } const mentions: UserInfo[] = [] const botMentionMatch = (c.message || '').match(/<@([^>]+)>/g) if (botMentionMatch) { const userIds = botMentionMatch.map((m: string) => m.replace('<@', '').replace('>', '')) const users = await this.prismaClient.user.findMany({ where: { id: { in: userIds } }, }) for (const u of users) { mentions.push({ id: u.id, name: u.name }) } } const creator: UserInfo = { id: '', name: '' } if (c.sessionId) { creator.id = c.creatorId || '' creator.name = c.creator?.name || 'Ai Bot' } else if (c.creator) { creator.id = c.creator.id creator.name = c.creator.name } return { id: c.id, assetId: c.assetId, message: c.message, annotations: c.annotation, creator, replies, attachments, mentions, createdAt: c.createdAt.toISOString(), updatedAt: c.updatedAt.toISOString(), sessionId: c.sessionId, } } } export const assetService = new AssetService()