mirror of
https://github.com/get-drexa/drive.git
synced 2025-12-01 22:11:39 +00:00
impl: permanent file deletion
implement trash page and permanent file deletion logic Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
@@ -307,3 +307,43 @@ export async function moveToTrashRecursive(
|
||||
|
||||
await Promise.all([...filePatches, ...directoryPatches])
|
||||
}
|
||||
|
||||
export async function deletePermanently(
|
||||
ctx: AuthenticatedMutationCtx,
|
||||
{
|
||||
items,
|
||||
}: {
|
||||
items: DirectoryHandle[]
|
||||
},
|
||||
) {
|
||||
if (items.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
const itemsToBeDeleted = await Promise.allSettled(
|
||||
items.map((item) => ctx.db.get(item.id)),
|
||||
).then((results) =>
|
||||
results.filter(
|
||||
(result): result is PromiseFulfilledResult<Doc<"directories">> =>
|
||||
result.status === "fulfilled" && result.value !== null,
|
||||
),
|
||||
)
|
||||
|
||||
const deleteDirectoryPromises = itemsToBeDeleted.map((item) =>
|
||||
ctx.db.delete(item.value._id),
|
||||
)
|
||||
|
||||
const deleteResults = await Promise.allSettled(deleteDirectoryPromises)
|
||||
|
||||
const errors: Err.ApplicationErrorData[] = []
|
||||
let successfulDeletions = 0
|
||||
for (const result of deleteResults) {
|
||||
if (result.status === "rejected") {
|
||||
errors.push(Err.createJson(Err.Code.Internal))
|
||||
} else {
|
||||
successfulDeletions += 1
|
||||
}
|
||||
}
|
||||
|
||||
return { deleted: successfulDeletions, errors }
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Id } from "../_generated/dataModel"
|
||||
import type { Doc, Id } from "../_generated/dataModel"
|
||||
import type { AuthenticatedMutationCtx } from "../functions"
|
||||
import * as Err from "./error"
|
||||
import type { DirectoryHandle, FileHandle } from "./filesystem"
|
||||
@@ -90,7 +90,10 @@ export async function move(
|
||||
|
||||
const results = await Promise.allSettled(
|
||||
okFiles.map((handle) =>
|
||||
ctx.db.patch(handle.id, { directoryId: targetDirectoryHandle.id, updatedAt: Date.now() }),
|
||||
ctx.db.patch(handle.id, {
|
||||
directoryId: targetDirectoryHandle.id,
|
||||
updatedAt: Date.now(),
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -102,3 +105,46 @@ export async function move(
|
||||
|
||||
return { moved: okFiles, errors }
|
||||
}
|
||||
|
||||
export async function deletePermanently(
|
||||
ctx: AuthenticatedMutationCtx,
|
||||
{
|
||||
items,
|
||||
}: {
|
||||
items: FileHandle[]
|
||||
},
|
||||
) {
|
||||
if (items.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
const itemsToBeDeleted = await Promise.allSettled(
|
||||
items.map((item) => ctx.db.get(item.id)),
|
||||
).then((results) =>
|
||||
results.filter(
|
||||
(result): result is PromiseFulfilledResult<Doc<"files">> =>
|
||||
result.status === "fulfilled" && result.value !== null,
|
||||
),
|
||||
)
|
||||
|
||||
const deleteFilePromises = itemsToBeDeleted.map((item) =>
|
||||
Promise.all([
|
||||
ctx.db.delete(item.value._id),
|
||||
ctx.storage.delete(item.value.storageId),
|
||||
]),
|
||||
)
|
||||
|
||||
const deleteResults = await Promise.allSettled(deleteFilePromises)
|
||||
|
||||
const errors: Err.ApplicationErrorData[] = []
|
||||
let successfulDeletions = 0
|
||||
for (const result of deleteResults) {
|
||||
if (result.status === "rejected") {
|
||||
errors.push(Err.createJson(Err.Code.Internal))
|
||||
} else {
|
||||
successfulDeletions += 1
|
||||
}
|
||||
}
|
||||
|
||||
return { deleted: successfulDeletions, errors }
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { v } from "convex/values"
|
||||
import type { Doc, Id } from "../_generated/dataModel"
|
||||
import type { AuthenticatedMutationCtx } from "../functions"
|
||||
import * as Directories from "./directories"
|
||||
import * as Files from "./files"
|
||||
|
||||
export enum FileType {
|
||||
File = "File",
|
||||
@@ -73,3 +76,84 @@ export const VFileHandle = v.object({
|
||||
id: v.id("files"),
|
||||
})
|
||||
export const VFileSystemHandle = v.union(VFileHandle, VDirectoryHandle)
|
||||
|
||||
export async function deleteItemsPermanently(
|
||||
ctx: AuthenticatedMutationCtx,
|
||||
{ handles }: { handles: FileSystemHandle[] },
|
||||
) {
|
||||
// Collect all items to delete (including nested items)
|
||||
const fileHandlesToDelete: FileHandle[] = []
|
||||
const directoryHandlesToDelete: DirectoryHandle[] = []
|
||||
|
||||
// Process each handle to collect files and directories
|
||||
for (const handle of handles) {
|
||||
// Use a queue to process items iteratively instead of recursively
|
||||
const queue: FileSystemHandle[] = [handle]
|
||||
|
||||
while (queue.length > 0) {
|
||||
const currentHandle = queue.shift()!
|
||||
|
||||
// Add current item to appropriate deletion collection
|
||||
if (currentHandle.kind === FileType.File) {
|
||||
fileHandlesToDelete.push(currentHandle)
|
||||
} else {
|
||||
directoryHandlesToDelete.push(currentHandle)
|
||||
}
|
||||
|
||||
// If it's a directory, collect all children and add them to the queue
|
||||
if (currentHandle.kind === FileType.Directory) {
|
||||
// Get all child directories that are in trash (deletedAt > 0)
|
||||
const childDirectories = await ctx.db
|
||||
.query("directories")
|
||||
.withIndex("byParentId", (q) =>
|
||||
q
|
||||
.eq("userId", ctx.user._id)
|
||||
.eq("parentId", currentHandle.id)
|
||||
.gte("deletedAt", 0),
|
||||
)
|
||||
.collect()
|
||||
|
||||
// Get all child files that are in trash (deletedAt > 0)
|
||||
const childFiles = await ctx.db
|
||||
.query("files")
|
||||
.withIndex("byDirectoryId", (q) =>
|
||||
q
|
||||
.eq("userId", ctx.user._id)
|
||||
.eq("directoryId", currentHandle.id)
|
||||
.gte("deletedAt", 0),
|
||||
)
|
||||
.collect()
|
||||
|
||||
// Add child directories to queue for processing
|
||||
for (const childDir of childDirectories) {
|
||||
const childHandle = newDirectoryHandle(childDir._id)
|
||||
queue.push(childHandle)
|
||||
}
|
||||
|
||||
// Add child files to file handles collection
|
||||
for (const childFile of childFiles) {
|
||||
const childFileHandle = newFileHandle(childFile._id)
|
||||
fileHandlesToDelete.push(childFileHandle)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete files and directories using their respective models
|
||||
const [filesResult, directoriesResult] = await Promise.all([
|
||||
Files.deletePermanently(ctx, { items: fileHandlesToDelete }),
|
||||
Directories.deletePermanently(ctx, { items: directoryHandlesToDelete }),
|
||||
])
|
||||
|
||||
// Combine results, handling null responses
|
||||
return {
|
||||
deleted: {
|
||||
files: filesResult?.deleted || 0,
|
||||
directories: directoriesResult?.deleted || 0,
|
||||
},
|
||||
errors: [
|
||||
...(filesResult?.errors || []),
|
||||
...(directoriesResult?.errors || []),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user