2025-09-28 15:45:49 +00:00
|
|
|
import { v } from "convex/values"
|
2025-10-20 00:17:50 +00:00
|
|
|
import type { Doc, Id } from "../_generated/dataModel"
|
2025-10-18 14:02:20 +00:00
|
|
|
import {
|
|
|
|
|
type AuthenticatedMutationCtx,
|
|
|
|
|
type AuthenticatedQueryCtx,
|
|
|
|
|
authorizedGet,
|
2025-10-12 14:31:02 +00:00
|
|
|
} from "../functions"
|
2025-10-18 14:02:20 +00:00
|
|
|
import * as Err from "../shared/error"
|
|
|
|
|
import type {
|
|
|
|
|
DirectoryHandle,
|
|
|
|
|
FileHandle,
|
|
|
|
|
FileSystemHandle,
|
|
|
|
|
} from "../shared/filesystem"
|
|
|
|
|
import {
|
|
|
|
|
FileType,
|
|
|
|
|
newDirectoryHandle,
|
|
|
|
|
newFileHandle,
|
|
|
|
|
} from "../shared/filesystem"
|
2025-10-05 00:41:59 +00:00
|
|
|
import * as Directories from "./directories"
|
|
|
|
|
import * as Files from "./files"
|
2025-09-20 23:23:28 +00:00
|
|
|
|
2025-09-28 15:45:49 +00:00
|
|
|
export const VDirectoryHandle = v.object({
|
|
|
|
|
kind: v.literal(FileType.Directory),
|
|
|
|
|
id: v.id("directories"),
|
|
|
|
|
})
|
|
|
|
|
export const VFileHandle = v.object({
|
|
|
|
|
kind: v.literal(FileType.File),
|
|
|
|
|
id: v.id("files"),
|
|
|
|
|
})
|
|
|
|
|
export const VFileSystemHandle = v.union(VFileHandle, VDirectoryHandle)
|
2025-10-05 00:41:59 +00:00
|
|
|
|
2025-10-12 14:31:02 +00:00
|
|
|
export async function queryRootDirectory(
|
2025-10-18 14:02:20 +00:00
|
|
|
ctx: AuthenticatedQueryCtx | AuthenticatedMutationCtx,
|
2025-10-12 14:31:02 +00:00
|
|
|
): Promise<Doc<"directories"> | null> {
|
|
|
|
|
return await ctx.db
|
2025-10-05 22:14:44 +00:00
|
|
|
.query("directories")
|
|
|
|
|
.withIndex("byParentId", (q) =>
|
|
|
|
|
q.eq("userId", ctx.user._id).eq("parentId", undefined),
|
|
|
|
|
)
|
|
|
|
|
.first()
|
2025-10-12 14:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function ensureRootDirectory(
|
|
|
|
|
ctx: AuthenticatedMutationCtx,
|
|
|
|
|
): Promise<Id<"directories">> {
|
|
|
|
|
const existing = await queryRootDirectory(ctx)
|
2025-10-05 22:14:44 +00:00
|
|
|
|
|
|
|
|
if (existing) {
|
|
|
|
|
return existing._id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const now = Date.now()
|
|
|
|
|
return await ctx.db.insert("directories", {
|
|
|
|
|
name: "",
|
|
|
|
|
createdAt: now,
|
|
|
|
|
updatedAt: now,
|
|
|
|
|
userId: ctx.user._id,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:29:45 +00:00
|
|
|
/**
|
|
|
|
|
* Recursively collects all file and directory handles from the given handles,
|
|
|
|
|
* including all nested items. Only includes items that are in trash (deletedAt >= 0).
|
|
|
|
|
*/
|
|
|
|
|
async function collectAllHandlesRecursively(
|
2025-10-05 00:41:59 +00:00
|
|
|
ctx: AuthenticatedMutationCtx,
|
|
|
|
|
{ handles }: { handles: FileSystemHandle[] },
|
2025-10-05 14:29:45 +00:00
|
|
|
): Promise<{ fileHandles: FileHandle[]; directoryHandles: DirectoryHandle[] }> {
|
|
|
|
|
const fileHandles: FileHandle[] = []
|
|
|
|
|
const directoryHandles: DirectoryHandle[] = []
|
2025-10-05 00:41:59 +00:00
|
|
|
|
|
|
|
|
for (const handle of handles) {
|
|
|
|
|
const queue: FileSystemHandle[] = [handle]
|
|
|
|
|
|
|
|
|
|
while (queue.length > 0) {
|
|
|
|
|
const currentHandle = queue.shift()!
|
|
|
|
|
|
|
|
|
|
if (currentHandle.kind === FileType.File) {
|
2025-10-05 14:29:45 +00:00
|
|
|
fileHandles.push(currentHandle)
|
2025-10-05 00:41:59 +00:00
|
|
|
} else {
|
2025-10-05 14:29:45 +00:00
|
|
|
directoryHandles.push(currentHandle)
|
2025-10-05 00:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentHandle.kind === FileType.Directory) {
|
|
|
|
|
const childDirectories = await ctx.db
|
|
|
|
|
.query("directories")
|
|
|
|
|
.withIndex("byParentId", (q) =>
|
|
|
|
|
q
|
|
|
|
|
.eq("userId", ctx.user._id)
|
|
|
|
|
.eq("parentId", currentHandle.id)
|
|
|
|
|
.gte("deletedAt", 0),
|
|
|
|
|
)
|
|
|
|
|
.collect()
|
|
|
|
|
|
|
|
|
|
const childFiles = await ctx.db
|
|
|
|
|
.query("files")
|
|
|
|
|
.withIndex("byDirectoryId", (q) =>
|
|
|
|
|
q
|
|
|
|
|
.eq("userId", ctx.user._id)
|
|
|
|
|
.eq("directoryId", currentHandle.id)
|
|
|
|
|
.gte("deletedAt", 0),
|
|
|
|
|
)
|
|
|
|
|
.collect()
|
|
|
|
|
|
|
|
|
|
for (const childDir of childDirectories) {
|
2025-10-18 14:02:20 +00:00
|
|
|
queue.push(newDirectoryHandle(childDir._id))
|
2025-10-05 00:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const childFile of childFiles) {
|
2025-10-18 14:02:20 +00:00
|
|
|
fileHandles.push(newFileHandle(childFile._id))
|
2025-10-05 00:41:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:29:45 +00:00
|
|
|
return { fileHandles, directoryHandles }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Restores deleted items by unsetting the deletedAt field recursively.
|
|
|
|
|
* This includes all nested files and directories within the given handles.
|
|
|
|
|
*/
|
|
|
|
|
export async function restoreItems(
|
|
|
|
|
ctx: AuthenticatedMutationCtx,
|
|
|
|
|
{ handles }: { handles: FileSystemHandle[] },
|
|
|
|
|
) {
|
2025-10-05 15:01:55 +00:00
|
|
|
const { fileHandles, directoryHandles } =
|
|
|
|
|
await collectAllHandlesRecursively(ctx, { handles })
|
2025-10-05 14:29:45 +00:00
|
|
|
|
|
|
|
|
const [filesResult, directoriesResult] = await Promise.all([
|
|
|
|
|
Files.restore(ctx, { items: fileHandles }),
|
|
|
|
|
Directories.restore(ctx, { items: directoryHandles }),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
restored: {
|
|
|
|
|
files: filesResult?.restored || 0,
|
|
|
|
|
directories: directoriesResult?.restored || 0,
|
|
|
|
|
},
|
|
|
|
|
errors: [
|
|
|
|
|
...(filesResult?.errors || []),
|
|
|
|
|
...(directoriesResult?.errors || []),
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteItemsPermanently(
|
|
|
|
|
ctx: AuthenticatedMutationCtx,
|
|
|
|
|
{ handles }: { handles: FileSystemHandle[] },
|
2025-10-18 14:02:20 +00:00
|
|
|
) {
|
|
|
|
|
const { fileHandles, directoryHandles } =
|
|
|
|
|
await collectAllHandlesRecursively(ctx, { handles })
|
2025-10-05 14:29:45 +00:00
|
|
|
|
2025-10-05 00:41:59 +00:00
|
|
|
const [filesResult, directoriesResult] = await Promise.all([
|
2025-10-18 14:02:20 +00:00
|
|
|
Files.deletePermanently(ctx, { items: fileHandles }),
|
|
|
|
|
Directories.deletePermanently(ctx, { items: directoryHandles }),
|
2025-10-05 00:41:59 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
deleted: {
|
|
|
|
|
files: filesResult?.deleted || 0,
|
|
|
|
|
directories: directoriesResult?.deleted || 0,
|
|
|
|
|
},
|
|
|
|
|
errors: [
|
|
|
|
|
...(filesResult?.errors || []),
|
|
|
|
|
...(directoriesResult?.errors || []),
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-12 14:31:02 +00:00
|
|
|
|
2025-10-18 14:02:20 +00:00
|
|
|
export async function emptyTrash(ctx: AuthenticatedMutationCtx) {
|
2025-10-12 14:31:02 +00:00
|
|
|
const rootDir = await queryRootDirectory(ctx)
|
|
|
|
|
if (!rootDir) {
|
|
|
|
|
throw Err.create(Err.Code.NotFound, "user root directory not found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dirs = await ctx.db
|
|
|
|
|
.query("directories")
|
|
|
|
|
.withIndex("byParentId", (q) =>
|
|
|
|
|
q
|
|
|
|
|
.eq("userId", ctx.user._id)
|
|
|
|
|
.eq("parentId", rootDir._id)
|
|
|
|
|
.gte("deletedAt", 0),
|
|
|
|
|
)
|
|
|
|
|
.collect()
|
|
|
|
|
|
|
|
|
|
const files = await ctx.db
|
|
|
|
|
.query("files")
|
|
|
|
|
.withIndex("byDirectoryId", (q) =>
|
|
|
|
|
q
|
|
|
|
|
.eq("userId", ctx.user._id)
|
|
|
|
|
.eq("directoryId", rootDir._id)
|
|
|
|
|
.gte("deletedAt", 0),
|
|
|
|
|
)
|
|
|
|
|
.collect()
|
|
|
|
|
|
|
|
|
|
if (dirs.length === 0 && files.length === 0) {
|
|
|
|
|
return {
|
|
|
|
|
deleted: {
|
|
|
|
|
files: 0,
|
|
|
|
|
directories: 0,
|
|
|
|
|
},
|
|
|
|
|
errors: [],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await deleteItemsPermanently(ctx, {
|
|
|
|
|
handles: [
|
|
|
|
|
...dirs.map((it) => newDirectoryHandle(it._id)),
|
|
|
|
|
...files.map((it) => newFileHandle(it._id)),
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-10-16 21:43:23 +00:00
|
|
|
|
|
|
|
|
export async function fetchFileUrl(
|
|
|
|
|
ctx: AuthenticatedQueryCtx,
|
|
|
|
|
{ fileId }: { fileId: Id<"files"> },
|
|
|
|
|
): Promise<string> {
|
|
|
|
|
const file = await authorizedGet(ctx, fileId)
|
|
|
|
|
if (!file) {
|
|
|
|
|
throw Err.create(Err.Code.NotFound, "file not found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const url = await ctx.storage.getUrl(file.storageId)
|
|
|
|
|
if (!url) {
|
|
|
|
|
throw Err.create(Err.Code.NotFound, "file not found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return url
|
|
|
|
|
}
|