2025-09-21 17:03:50 +00:00
|
|
|
import { v } from "convex/values"
|
|
|
|
|
import { authenticatedMutation } from "./functions"
|
|
|
|
|
import * as Directories from "./model/directories"
|
|
|
|
|
import * as Err from "./model/error"
|
|
|
|
|
import * as Files from "./model/files"
|
|
|
|
|
import type { DirectoryHandle, FileHandle } from "./model/filesystem"
|
2025-09-28 15:45:49 +00:00
|
|
|
import {
|
|
|
|
|
type FileSystemHandle,
|
|
|
|
|
FileType,
|
|
|
|
|
VDirectoryHandle,
|
|
|
|
|
VFileSystemHandle,
|
|
|
|
|
} from "./model/filesystem"
|
2025-09-21 17:03:50 +00:00
|
|
|
|
|
|
|
|
export const moveItems = authenticatedMutation({
|
|
|
|
|
args: {
|
|
|
|
|
targetDirectory: VDirectoryHandle,
|
|
|
|
|
items: v.array(VFileSystemHandle),
|
|
|
|
|
},
|
|
|
|
|
handler: async (ctx, { targetDirectory: targetDirectoryHandle, items }) => {
|
|
|
|
|
const targetDirectory = await Directories.fetchHandle(
|
|
|
|
|
ctx,
|
|
|
|
|
targetDirectoryHandle,
|
|
|
|
|
)
|
|
|
|
|
if (!targetDirectory) {
|
|
|
|
|
throw Err.create(
|
|
|
|
|
Err.Code.DirectoryNotFound,
|
|
|
|
|
`Directory ${targetDirectoryHandle.id} not found`,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const directoryHandles: DirectoryHandle[] = []
|
|
|
|
|
const fileHandles: FileHandle[] = []
|
|
|
|
|
for (const item of items) {
|
|
|
|
|
switch (item.kind) {
|
2025-09-28 15:45:49 +00:00
|
|
|
case FileType.Directory:
|
2025-09-21 17:03:50 +00:00
|
|
|
directoryHandles.push(item)
|
|
|
|
|
break
|
2025-09-28 15:45:49 +00:00
|
|
|
case FileType.File:
|
2025-09-21 17:03:50 +00:00
|
|
|
fileHandles.push(item)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-25 23:12:13 +00:00
|
|
|
|
|
|
|
|
const [fileMoveResult, directoryMoveResult] = await Promise.all([
|
2025-09-21 17:03:50 +00:00
|
|
|
Files.move(ctx, {
|
|
|
|
|
targetDirectory: targetDirectoryHandle,
|
|
|
|
|
items: fileHandles,
|
|
|
|
|
}),
|
|
|
|
|
Directories.move(ctx, {
|
|
|
|
|
targetDirectory: targetDirectoryHandle,
|
|
|
|
|
sourceDirectories: directoryHandles,
|
|
|
|
|
}),
|
|
|
|
|
])
|
|
|
|
|
|
2025-09-25 23:12:13 +00:00
|
|
|
return {
|
|
|
|
|
moved: [...directoryMoveResult.moved, ...fileMoveResult.moved],
|
|
|
|
|
errors: [...fileMoveResult.errors, ...directoryMoveResult.errors],
|
|
|
|
|
}
|
2025-09-21 17:03:50 +00:00
|
|
|
},
|
|
|
|
|
})
|
2025-09-28 15:45:49 +00:00
|
|
|
|
|
|
|
|
export const moveToTrash = authenticatedMutation({
|
|
|
|
|
args: {
|
|
|
|
|
handles: v.array(VFileSystemHandle),
|
|
|
|
|
},
|
|
|
|
|
handler: async (ctx, { handles }) => {
|
|
|
|
|
// biome-ignore lint/suspicious/useIterableCallbackReturn: switch statement is exhaustive
|
|
|
|
|
const promises = handles.map((handle) => {
|
|
|
|
|
switch (handle.kind) {
|
|
|
|
|
case FileType.File:
|
|
|
|
|
return ctx.db
|
|
|
|
|
.patch(handle.id, {
|
2025-10-03 21:23:51 +00:00
|
|
|
deletedAt: Date.now(),
|
2025-09-28 15:45:49 +00:00
|
|
|
})
|
|
|
|
|
.then(() => handle)
|
|
|
|
|
case FileType.Directory:
|
|
|
|
|
return Directories.moveToTrashRecursive(ctx, handle).then(
|
|
|
|
|
() => handle,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const results = await Promise.allSettled(promises)
|
|
|
|
|
const errors: Err.ApplicationErrorData[] = []
|
|
|
|
|
const okHandles: FileSystemHandle[] = []
|
|
|
|
|
for (const result of results) {
|
|
|
|
|
switch (result.status) {
|
|
|
|
|
case "fulfilled":
|
|
|
|
|
okHandles.push(result.value)
|
|
|
|
|
break
|
|
|
|
|
case "rejected":
|
|
|
|
|
errors.push(Err.createJson(Err.Code.Internal))
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
deleted: okHandles,
|
|
|
|
|
errors,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|