mirror of
https://github.com/get-drexa/drive.git
synced 2025-11-30 21:41:39 +00:00
feat: check for name conflict on file/dir move
Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
@@ -46,7 +46,8 @@ export const moveItems = authenticatedMutation({
|
||||
break
|
||||
}
|
||||
}
|
||||
await Promise.all([
|
||||
|
||||
const [fileMoveResult, directoryMoveResult] = await Promise.all([
|
||||
Files.move(ctx, {
|
||||
targetDirectory: targetDirectoryHandle,
|
||||
items: fileHandles,
|
||||
@@ -57,6 +58,9 @@ export const moveItems = authenticatedMutation({
|
||||
}),
|
||||
])
|
||||
|
||||
return { items, targetDirectory }
|
||||
return {
|
||||
moved: [...directoryMoveResult.moved, ...fileMoveResult.moved],
|
||||
errors: [...fileMoveResult.errors, ...directoryMoveResult.errors],
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
@@ -170,7 +170,7 @@ export async function move(
|
||||
targetDirectory: DirectoryHandle
|
||||
sourceDirectories: DirectoryHandle[]
|
||||
},
|
||||
): Promise<void> {
|
||||
) {
|
||||
const conflictCheckResults = await Promise.allSettled(
|
||||
sourceDirectories.map((directory) =>
|
||||
ctx.db.get(directory.id).then((d) => {
|
||||
@@ -194,25 +194,40 @@ export async function move(
|
||||
),
|
||||
)
|
||||
|
||||
const errors: Err.ApplicationError[] = []
|
||||
for (const result of conflictCheckResults) {
|
||||
if (result.status === "fulfilled" && result.value) {
|
||||
errors.push(
|
||||
Err.create(
|
||||
Err.Code.Conflict,
|
||||
`Directory ${targetDirectory.id} already contains a directory with name ${result.value.name}`,
|
||||
),
|
||||
)
|
||||
console.log(sourceDirectories)
|
||||
|
||||
const errors: Err.ApplicationErrorData[] = []
|
||||
const okDirectories: DirectoryHandle[] = []
|
||||
conflictCheckResults.forEach((result, i) => {
|
||||
if (result.status === "fulfilled") {
|
||||
if (result.value) {
|
||||
errors.push(
|
||||
Err.createJson(
|
||||
Err.Code.Conflict,
|
||||
`Directory ${targetDirectory.id} already contains a directory with name ${result.value.name}`,
|
||||
),
|
||||
)
|
||||
} else {
|
||||
okDirectories.push(sourceDirectories[i])
|
||||
}
|
||||
} else if (result.status === "rejected") {
|
||||
errors.push(Err.create(Err.Code.Internal))
|
||||
errors.push(Err.createJson(Err.Code.Internal))
|
||||
}
|
||||
})
|
||||
|
||||
const results = await Promise.allSettled(
|
||||
okDirectories.map((handle) =>
|
||||
ctx.db.patch(handle.id, { parentId: targetDirectory.id }),
|
||||
),
|
||||
)
|
||||
|
||||
for (const updateResult of results) {
|
||||
if (updateResult.status === "rejected") {
|
||||
errors.push(Err.createJson(Err.Code.Internal))
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
sourceDirectories.map((directory) =>
|
||||
ctx.db.patch(directory.id, { parentId: targetDirectory.id }),
|
||||
),
|
||||
)
|
||||
return { moved: okDirectories, errors }
|
||||
}
|
||||
|
||||
export async function moveToTrashRecursive(
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
import { ConvexError } from "convex/values"
|
||||
|
||||
export enum Code {
|
||||
export enum Code {
|
||||
Conflict = "Conflict",
|
||||
DirectoryExists = "DirectoryExists",
|
||||
DirectoryNotFound = "DirectoryNotFound",
|
||||
FileExists = "FileExists",
|
||||
FileNotFound = "FileNotFound",
|
||||
Internal = "Internal",
|
||||
Unauthenticated = "Unauthenticated",
|
||||
}
|
||||
|
||||
export type ApplicationError = ConvexError<{ code: Code; message?: string }>
|
||||
export type ApplicationErrorData = { code: Code; message?: string }
|
||||
export type ApplicationError = ConvexError<ApplicationErrorData>
|
||||
|
||||
export function isApplicationError(error: unknown): error is ApplicationError {
|
||||
return error instanceof ConvexError && "code" in error.data
|
||||
@@ -22,3 +24,11 @@ export function create(code: Code, message?: string): ApplicationError {
|
||||
code === Code.Internal ? "Internal application error" : message,
|
||||
})
|
||||
}
|
||||
|
||||
export function createJson(code: Code, message?: string): ApplicationErrorData {
|
||||
return {
|
||||
code,
|
||||
message:
|
||||
code === Code.Internal ? "Internal application error" : message,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,9 +46,59 @@ export async function move(
|
||||
items: FileHandle[]
|
||||
},
|
||||
) {
|
||||
await Promise.all(
|
||||
items.map((item) =>
|
||||
ctx.db.patch(item.id, { directoryId: targetDirectoryHandle.id }),
|
||||
const conflictCheckResults = await Promise.allSettled(
|
||||
items.map((fileHandle) =>
|
||||
ctx.db.get(fileHandle.id).then((f) => {
|
||||
if (!f) {
|
||||
throw Err.create(
|
||||
Err.Code.FileNotFound,
|
||||
`File ${fileHandle.id} not found`,
|
||||
)
|
||||
}
|
||||
return ctx.db
|
||||
.query("files")
|
||||
.withIndex("uniqueFileInDirectory", (q) =>
|
||||
q
|
||||
.eq("userId", ctx.user._id)
|
||||
.eq("directoryId", targetDirectoryHandle.id)
|
||||
.eq("name", f.name)
|
||||
.eq("deletedAt", undefined),
|
||||
)
|
||||
.first()
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
const errors: Err.ApplicationErrorData[] = []
|
||||
const okFiles: FileHandle[] = []
|
||||
conflictCheckResults.forEach((result, i) => {
|
||||
if (result.status === "fulfilled") {
|
||||
if (result.value) {
|
||||
errors.push(
|
||||
Err.createJson(
|
||||
Err.Code.Conflict,
|
||||
`Directory ${targetDirectoryHandle.id} already contains a file with name ${result.value.name}`,
|
||||
),
|
||||
)
|
||||
} else {
|
||||
okFiles.push(items[i])
|
||||
}
|
||||
} else if (result.status === "rejected") {
|
||||
errors.push(Err.createJson(Err.Code.Internal))
|
||||
}
|
||||
})
|
||||
|
||||
const results = await Promise.allSettled(
|
||||
okFiles.map((handle) =>
|
||||
ctx.db.patch(handle.id, { directoryId: targetDirectoryHandle.id }),
|
||||
),
|
||||
)
|
||||
|
||||
for (const updateResult of results) {
|
||||
if (updateResult.status === "rejected") {
|
||||
errors.push(Err.createJson(Err.Code.Internal))
|
||||
}
|
||||
}
|
||||
|
||||
return { moved: okFiles, errors }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user