feat: impl multi file/dir moving
Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
@@ -4,7 +4,7 @@ import type {
|
||||
AuthenticatedQueryCtx,
|
||||
} from "../functions"
|
||||
import * as Err from "./error"
|
||||
import type { FilePath, ReverseFilePath } from "./filesystem"
|
||||
import type { DirectoryHandle, FilePath, ReverseFilePath } from "./filesystem"
|
||||
import { newDirectoryHandle } from "./filesystem"
|
||||
|
||||
type Directory = {
|
||||
@@ -31,6 +31,20 @@ export async function fetchRoot(ctx: AuthenticatedQueryCtx) {
|
||||
.first()
|
||||
}
|
||||
|
||||
export async function fetchHandle(
|
||||
ctx: AuthenticatedQueryCtx,
|
||||
handle: DirectoryHandle,
|
||||
): Promise<Doc<"directories">> {
|
||||
const directory = await ctx.db.get(handle.id)
|
||||
if (!directory || directory.userId !== ctx.user._id) {
|
||||
throw Err.create(
|
||||
Err.Code.DirectoryNotFound,
|
||||
`Directory ${handle.id} not found`,
|
||||
)
|
||||
}
|
||||
return directory
|
||||
}
|
||||
|
||||
export async function fetch(
|
||||
ctx: AuthenticatedQueryCtx,
|
||||
{ directoryId }: { directoryId: Id<"directories"> },
|
||||
@@ -147,6 +161,23 @@ export async function create(
|
||||
})
|
||||
}
|
||||
|
||||
export async function move(
|
||||
ctx: AuthenticatedMutationCtx,
|
||||
{
|
||||
targetDirectory,
|
||||
sourceDirectories,
|
||||
}: {
|
||||
targetDirectory: DirectoryHandle
|
||||
sourceDirectories: DirectoryHandle[]
|
||||
},
|
||||
): Promise<void> {
|
||||
await Promise.all(
|
||||
sourceDirectories.map((directory) =>
|
||||
ctx.db.patch(directory.id, { parentId: targetDirectory.id }),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
export async function moveToTrashRecursive(
|
||||
ctx: AuthenticatedMutationCtx,
|
||||
directoryId: Id<"directories">,
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import type { Id } from "../_generated/dataModel"
|
||||
import type { AuthenticatedMutationCtx } from "../functions"
|
||||
import * as Err from "./error"
|
||||
import type { DirectoryHandle, FileHandle } from "./filesystem"
|
||||
|
||||
export async function renameFile(
|
||||
ctx: AuthenticatedMutationCtx,
|
||||
@@ -35,27 +36,19 @@ export async function renameFile(
|
||||
await ctx.db.patch(itemId, { name: newName })
|
||||
}
|
||||
|
||||
export async function moveFiles(
|
||||
export async function move(
|
||||
ctx: AuthenticatedMutationCtx,
|
||||
{
|
||||
targetDirectoryId,
|
||||
targetDirectory: targetDirectoryHandle,
|
||||
items,
|
||||
}: {
|
||||
targetDirectoryId: Id<"directories">
|
||||
items: Id<"files">[]
|
||||
targetDirectory: DirectoryHandle
|
||||
items: FileHandle[]
|
||||
},
|
||||
) {
|
||||
const targetDirectory = await ctx.db.get(targetDirectoryId)
|
||||
if (!targetDirectory) {
|
||||
throw Err.create(
|
||||
Err.Code.DirectoryNotFound,
|
||||
"Target directory not found",
|
||||
)
|
||||
}
|
||||
await Promise.all(
|
||||
items.map((itemId) =>
|
||||
ctx.db.patch(itemId, { directoryId: targetDirectoryId }),
|
||||
items.map((item) =>
|
||||
ctx.db.patch(item.id, { directoryId: targetDirectoryHandle.id }),
|
||||
),
|
||||
)
|
||||
return { items, targetDirectory }
|
||||
}
|
||||
|
Reference in New Issue
Block a user