mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 18:17:16 +00:00
feat(project): implement async project deletion and storage cleanup
This commit is contained in:
@@ -113,6 +113,19 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
const p = await projectService.getProject(projectId)
|
||||
return c.json(p)
|
||||
})
|
||||
.delete('/projects/:projectId', async (c) => {
|
||||
const projectId = c.req.param('projectId')
|
||||
const user = c.get('user')
|
||||
|
||||
await authzService.hasPermission({
|
||||
projectId,
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
})
|
||||
|
||||
await projectService.deleteProject(projectId)
|
||||
return c.json({ success: true })
|
||||
})
|
||||
.get('/projects/:projectId/team', async (c) => {
|
||||
const projectId = c.req.param('projectId')
|
||||
const user = c.get('user')
|
||||
|
||||
@@ -22,6 +22,7 @@ import teamRoute from '@/api/team'
|
||||
import uploadRoute from '@/api/upload'
|
||||
import versionStackRoute from '@/api/versionStack'
|
||||
import { metadataService } from '@/services/metadata/metadata'
|
||||
import { assetService } from '@/services/asset/asset'
|
||||
import { workflowService } from '@/workflow/workflow'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
@@ -61,6 +62,7 @@ export type AppType = typeof apiAppRoute
|
||||
export async function run() {
|
||||
// Start services
|
||||
await metadataService.syncSystemFields().catch(console.error)
|
||||
assetService.startCleanupJob()
|
||||
workflowService.start()
|
||||
if (process.env.WORKFLOW_EXECUTOR === 'temporal') {
|
||||
const args = process.argv.slice(2)
|
||||
|
||||
@@ -756,6 +756,60 @@ export class AssetService {
|
||||
}
|
||||
}
|
||||
|
||||
startCleanupJob() {
|
||||
// Run cleanup every 1 second
|
||||
setInterval(async () => {
|
||||
try {
|
||||
await this.cleanupOldAssets()
|
||||
} catch (e: unknown) {
|
||||
console.error('Error in asset cleanup job:', e)
|
||||
}
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
private async cleanupOldAssets() {
|
||||
const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000)
|
||||
|
||||
// 1. Lock 100 assets using raw SQL FOR UPDATE SKIP LOCKED and mark them as 'removed'
|
||||
// This immediately releases the DB lock as soon as the status is updated.
|
||||
const lockedAssets = await this.prismaClient.$queryRaw<{ id: string; key: string | null }[]>`
|
||||
WITH to_delete AS (
|
||||
SELECT id FROM assets
|
||||
WHERE removed = true
|
||||
AND deleted_at < ${thirtyDaysAgo}
|
||||
AND status != 'removed'
|
||||
LIMIT 100
|
||||
FOR UPDATE SKIP LOCKED
|
||||
)
|
||||
UPDATE assets SET status = 'removed', updated_at = NOW()
|
||||
WHERE id IN (SELECT id FROM to_delete)
|
||||
RETURNING id, key;
|
||||
`
|
||||
|
||||
if (lockedAssets.length === 0) return
|
||||
|
||||
// 2. Physical file deletion from storage
|
||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||
await Promise.allSettled(
|
||||
lockedAssets.map(async (a) => {
|
||||
if (a.key) {
|
||||
try {
|
||||
await s3Service.deleteObject(bucket, a.key)
|
||||
} catch (e: unknown) {
|
||||
console.error(`Failed to delete storage object ${a.key}:`, e)
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
// 3. Hard delete from database
|
||||
// Cascade FKs will handle related comments, metadata, etc.
|
||||
const ids = lockedAssets.map((a) => a.id)
|
||||
await this.prismaClient.asset.deleteMany({
|
||||
where: { id: { in: ids } },
|
||||
})
|
||||
}
|
||||
|
||||
async restoreAssets(ids: string[]): Promise<void> {
|
||||
for (const id of ids) {
|
||||
await this.prismaClient.$transaction(async (tx) => {
|
||||
|
||||
@@ -3,10 +3,12 @@ import { prisma } from '@/db'
|
||||
import { setupTestDbHooks } from '@/db-test-hooks'
|
||||
import { ProjectService } from './project'
|
||||
import { s3Service } from '@/services/s3/s3'
|
||||
import { assetService } from '@/services/asset/asset'
|
||||
|
||||
vi.mock('@/services/s3/s3', () => ({
|
||||
s3Service: {
|
||||
presign: vi.fn(),
|
||||
deleteObject: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
@@ -218,4 +220,140 @@ describe('ProjectService', () => {
|
||||
expect(p?.shareRoot?.name).toBe('share_root')
|
||||
})
|
||||
})
|
||||
|
||||
describe('deleteProject', () => {
|
||||
it('detaches and soft-deletes project assets', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Delete Team' } })
|
||||
const u = await prisma.user.create({ data: { name: 'u', email: 'delete@example.com' } })
|
||||
await prisma.teamMember.create({
|
||||
data: { teamId: team.id, userId: u.id, role: 'owner' },
|
||||
})
|
||||
|
||||
const project = await projectService.createProject(u, {
|
||||
name: 'Project to Delete',
|
||||
teamId: team.id,
|
||||
})
|
||||
|
||||
// Add some related data
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Test Asset',
|
||||
type: 'file',
|
||||
status: 'processed',
|
||||
projectId: project.id,
|
||||
key: 'asset-key',
|
||||
},
|
||||
})
|
||||
|
||||
// Set cover image key
|
||||
await prisma.project.update({
|
||||
where: { id: project.id },
|
||||
data: { coverImageKey: 'cover-key' },
|
||||
})
|
||||
|
||||
// Delete project
|
||||
await projectService.deleteProject(project.id)
|
||||
|
||||
// Verify project is deleted
|
||||
const p = await prisma.project.findUnique({ where: { id: project.id } })
|
||||
expect(p).toBeNull()
|
||||
|
||||
// Verify asset is soft-deleted and detached
|
||||
const a = await prisma.asset.findUnique({ where: { id: asset.id } })
|
||||
expect(a).toBeDefined()
|
||||
expect(a?.removed).toBe(true)
|
||||
expect(a?.projectId).toBeNull()
|
||||
expect(a?.parentId).toBeNull()
|
||||
|
||||
// Verify S3 project cover cleanup (synchronous)
|
||||
expect(s3Service.deleteObject).toHaveBeenCalledWith(expect.any(String), 'cover-key')
|
||||
// S3 asset key cleanup is NOT called synchronously
|
||||
expect(s3Service.deleteObject).not.toHaveBeenCalledWith(expect.any(String), 'asset-key')
|
||||
})
|
||||
|
||||
it('cleans up nested hierarchy asynchronously in background job', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Nested Team' } })
|
||||
const u = await prisma.user.create({ data: { name: 'u', email: 'nested@example.com' } })
|
||||
await prisma.teamMember.create({
|
||||
data: { teamId: team.id, userId: u.id, role: 'owner' },
|
||||
})
|
||||
|
||||
const project = await projectService.createProject(u, {
|
||||
name: 'Nested Project',
|
||||
teamId: team.id,
|
||||
})
|
||||
|
||||
const projectDb = await prisma.project.findUnique({ where: { id: project.id } })
|
||||
|
||||
// Create hierarchy: Folder A -> Folder B -> File C
|
||||
const folderA = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Folder A',
|
||||
type: 'folder',
|
||||
status: 'processed',
|
||||
projectId: project.id,
|
||||
parentId: projectDb!.rootFolderId,
|
||||
},
|
||||
})
|
||||
const folderB = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Folder B',
|
||||
type: 'folder',
|
||||
status: 'processed',
|
||||
projectId: project.id,
|
||||
parentId: folderA.id,
|
||||
},
|
||||
})
|
||||
const fileC = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'File C',
|
||||
type: 'file',
|
||||
status: 'processed',
|
||||
projectId: project.id,
|
||||
parentId: folderB.id,
|
||||
key: 'file-c-key',
|
||||
},
|
||||
})
|
||||
|
||||
// Add metadata and comments to verify cascade
|
||||
await prisma.assetMetadataValue.create({
|
||||
data: {
|
||||
assetId: fileC.id,
|
||||
fieldId: 'test-field',
|
||||
stringValue: 'test-value',
|
||||
},
|
||||
})
|
||||
const comment = await prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: fileC.id,
|
||||
message: 'Test comment',
|
||||
},
|
||||
})
|
||||
|
||||
// Delete project
|
||||
await projectService.deleteProject(project.id)
|
||||
|
||||
// Manually trigger the private cleanup method for testing
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
await (assetService as any).cleanupOldAssets()
|
||||
|
||||
// Verify everything is wiped from DB
|
||||
expect(await prisma.project.findUnique({ where: { id: project.id } })).toBeNull()
|
||||
expect(await prisma.asset.findUnique({ where: { id: folderA.id } })).toBeNull()
|
||||
expect(await prisma.asset.findUnique({ where: { id: folderB.id } })).toBeNull()
|
||||
expect(await prisma.asset.findUnique({ where: { id: fileC.id } })).toBeNull()
|
||||
|
||||
// Verify cascade
|
||||
expect(
|
||||
await prisma.assetMetadataValue.findUnique({
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
where: { assetId_fieldId: { assetId: fileC.id, fieldId: 'test-field' } },
|
||||
}),
|
||||
).toBeNull()
|
||||
expect(await prisma.assetComment.findUnique({ where: { id: comment.id } })).toBeNull()
|
||||
|
||||
// Verify S3 cleanup for file
|
||||
expect(s3Service.deleteObject).toHaveBeenCalledWith(expect.any(String), 'file-c-key')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -130,7 +130,7 @@ export class ProjectService {
|
||||
const direction = req.sortDirection === 'desc' ? 'desc' : 'asc'
|
||||
if (req.sortBy) {
|
||||
if (req.sortBy === 'created_at') {
|
||||
orderBy.id = direction
|
||||
orderBy.createdAt = direction
|
||||
} else if (req.sortBy === 'updated_at') {
|
||||
orderBy.updatedAt = direction
|
||||
} else {
|
||||
@@ -224,6 +224,53 @@ export class ProjectService {
|
||||
}))
|
||||
}
|
||||
|
||||
async deleteProject(projectId: string): Promise<void> {
|
||||
const project = await prisma.project.findUnique({
|
||||
where: { id: projectId },
|
||||
})
|
||||
if (!project) throw new Error('Project not found')
|
||||
|
||||
const sixtyDaysAgo = new Date(Date.now() - 60 * 24 * 60 * 60 * 1000)
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
// 1. Unlink root folders from project to avoid cycle during deletion
|
||||
await tx.project.update({
|
||||
where: { id: projectId },
|
||||
data: { rootFolderId: null, shareRootId: null },
|
||||
})
|
||||
|
||||
// 2. Detach ALL assets (files and folders) from project and soft-delete them
|
||||
// This queues them for the background cleanup job.
|
||||
await tx.asset.updateMany({
|
||||
where: { projectId },
|
||||
data: {
|
||||
projectId: null,
|
||||
parentId: null,
|
||||
targetId: null,
|
||||
removed: true,
|
||||
deletedAt: sixtyDaysAgo,
|
||||
status: 'trashed',
|
||||
},
|
||||
})
|
||||
|
||||
// 3. Delete the project itself
|
||||
// Thanks to cascade deletes, project members, invites, etc. will be cleaned up.
|
||||
await tx.project.delete({
|
||||
where: { id: projectId },
|
||||
})
|
||||
})
|
||||
|
||||
// Synchronously delete the project's cover image if it exists
|
||||
if (project.coverImageKey) {
|
||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||
try {
|
||||
await s3Service.deleteObject(bucket, project.coverImageKey)
|
||||
} catch (e: unknown) {
|
||||
console.error(`Failed to delete project cover image ${project.coverImageKey}:`, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getProjectRootFolder(projectId: string): Promise<string> {
|
||||
const project = await prisma.project.findUnique({
|
||||
where: { id: projectId },
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
HeadObjectCommand,
|
||||
ListObjectsV2Command,
|
||||
PutObjectCommand,
|
||||
DeleteObjectCommand,
|
||||
S3Client,
|
||||
} from '@aws-sdk/client-s3'
|
||||
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
|
||||
@@ -49,6 +50,7 @@ export interface S3Service {
|
||||
): Promise<void>
|
||||
getObject(bucket: string, key: string): Promise<S3Object>
|
||||
downloadToFile(bucket: string, key: string, filePath: string): Promise<void>
|
||||
deleteObject(bucket: string, key: string): Promise<void>
|
||||
headObject(bucket: string, key: string): Promise<ObjectInfo>
|
||||
listObjects(bucket: string, prefix: string): Promise<string[]>
|
||||
uploadFile(filePath: string, contentType: string): Promise<string>
|
||||
@@ -157,6 +159,14 @@ export class S3StorageService implements S3Service {
|
||||
})
|
||||
}
|
||||
|
||||
async deleteObject(bucket: string, key: string): Promise<void> {
|
||||
const command = new DeleteObjectCommand({
|
||||
Bucket: bucket,
|
||||
Key: key,
|
||||
})
|
||||
await this.client.send(command)
|
||||
}
|
||||
|
||||
async headObject(bucket: string, key: string): Promise<ObjectInfo> {
|
||||
const command = new HeadObjectCommand({
|
||||
Bucket: bucket,
|
||||
@@ -311,6 +321,19 @@ export class LocalStorageService implements S3Service {
|
||||
await fs.promises.copyFile(srcPath, filePath)
|
||||
}
|
||||
|
||||
async deleteObject(bucket: string, key: string): Promise<void> {
|
||||
const filePath = this.getFilePath(bucket, key)
|
||||
try {
|
||||
await fs.promises.unlink(filePath)
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error && (e as NodeJS.ErrnoException).code === 'ENOENT') {
|
||||
// Ignore if file doesn't exist
|
||||
return
|
||||
}
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
async headObject(bucket: string, key: string): Promise<ObjectInfo> {
|
||||
const filePath = this.getFilePath(bucket, key)
|
||||
try {
|
||||
|
||||
@@ -26,14 +26,16 @@ import {
|
||||
import { ShumaiLogo } from '@/ui/components/ui/icons'
|
||||
import { formatDateAgo } from '@/ui/lib/time'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { useQuery, useSuspenseQuery } from '@tanstack/react-query'
|
||||
import { useQuery, useSuspenseQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { createFileRoute, useNavigate } from '@tanstack/react-router'
|
||||
import { MoreHorizontal, PlusIcon } from 'lucide-react'
|
||||
import { useState } from 'react'
|
||||
import { Input } from '@/ui/components/ui/input'
|
||||
|
||||
function TeamPage() {
|
||||
const { teamId } = Route.useParams()
|
||||
const navigate = useNavigate()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
// State for clicked project for animation
|
||||
const [clickedProjectId, setClickedProjectId] = useState<string | null>(null)
|
||||
@@ -46,6 +48,7 @@ function TeamPage() {
|
||||
// State for Delete Alert
|
||||
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false)
|
||||
const [projectToDelete, setProjectToDelete] = useState<ProjectInfo | undefined>(undefined)
|
||||
const [deleteConfirmText, setDeleteConfirmText] = useState('')
|
||||
|
||||
// State for Members Dialog
|
||||
const [isMembersDialogOpen, setIsMembersDialogOpen] = useState(false)
|
||||
@@ -105,6 +108,18 @@ function TeamPage() {
|
||||
},
|
||||
})
|
||||
|
||||
const deleteProjectMutation = useMutation({
|
||||
mutationFn: async (projectId: string) => {
|
||||
const res = await client.api.projects[':projectId'].$delete({
|
||||
param: { projectId },
|
||||
})
|
||||
if (!res.ok) throw new Error('Failed to delete project')
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'projects'] })
|
||||
},
|
||||
})
|
||||
|
||||
const handleCreateProjectClick = () => {
|
||||
setProjectDialogMode('create')
|
||||
setSelectedProject(undefined)
|
||||
@@ -130,14 +145,19 @@ function TeamPage() {
|
||||
|
||||
const handleDeleteProjectClick = (project: ProjectInfo) => {
|
||||
setProjectToDelete(project)
|
||||
setDeleteConfirmText('')
|
||||
setIsDeleteDialogOpen(true)
|
||||
}
|
||||
|
||||
const handleConfirmDelete = () => {
|
||||
if (projectToDelete) {
|
||||
console.log('Delete project', projectToDelete.id)
|
||||
const handleConfirmDelete = async () => {
|
||||
if (projectToDelete && deleteConfirmText === 'delete') {
|
||||
try {
|
||||
await deleteProjectMutation.mutateAsync(projectToDelete.id!)
|
||||
setIsDeleteDialogOpen(false)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
setIsDeleteDialogOpen(false)
|
||||
}
|
||||
|
||||
const handleCardClick = (projectId: string, e: React.MouseEvent) => {
|
||||
@@ -317,16 +337,34 @@ function TeamPage() {
|
||||
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
This action cannot be undone. This will permanently delete the project "
|
||||
{projectToDelete?.name}".
|
||||
{projectToDelete?.name}" and all its assets.
|
||||
<div className="mt-4 flex flex-col gap-2 text-foreground">
|
||||
<span className="text-sm font-medium">
|
||||
Type <strong>delete</strong> to confirm:
|
||||
</span>
|
||||
<Input
|
||||
value={deleteConfirmText}
|
||||
onChange={(e) => setDeleteConfirmText(e.target.value)}
|
||||
placeholder="delete"
|
||||
className="h-9"
|
||||
autoFocus
|
||||
/>
|
||||
</div>
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogCancel disabled={deleteProjectMutation.isPending}>
|
||||
Cancel
|
||||
</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
onClick={handleConfirmDelete}
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
handleConfirmDelete()
|
||||
}}
|
||||
disabled={deleteConfirmText !== 'delete' || deleteProjectMutation.isPending}
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
>
|
||||
Delete
|
||||
{deleteProjectMutation.isPending ? 'Deleting...' : 'Delete'}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
|
||||
Reference in New Issue
Block a user