Skip to content

Commit

Permalink
Add move method
Browse files Browse the repository at this point in the history
  • Loading branch information
longern committed Jul 10, 2024
1 parent b634b2f commit de3979b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
3 changes: 2 additions & 1 deletion functions/webdav/[[path]].ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { handleRequestMkcol } from "./mkcol";
import { handleRequestPut } from "./put";
import { handleRequestCopy } from "./copy";
import { handleRequestDelete } from "./delete";
import { handleRequestMove } from "./move";

async function handleRequestOptions() {
return new Response(null, {
Expand All @@ -27,7 +28,7 @@ const HANDLERS: Record<
GET: handleRequestGet,
PUT: handleRequestPut,
COPY: handleRequestCopy,
MOVE: handleRequestGet,
MOVE: handleRequestMove,
DELETE: handleRequestDelete,
};

Expand Down
8 changes: 8 additions & 0 deletions functions/webdav/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export async function handleRequestCopy({
return new Response("Bad Request", { status: 400 });
const destination = destPathname.slice(WEBDAV_ENDPOINT.length);

if (
destination === path ||
(src.httpMetadata?.contentType === "application/x-directory" &&
destination.startsWith(path + "/"))
)
return new Response("Bad Request", { status: 400 });

// Check if the destination already exists
const destinationExists = await bucket.head(destination);
if (dontOverwrite && destinationExists)
Expand Down Expand Up @@ -51,6 +58,7 @@ export async function handleRequestCopy({
promise_array.push(copy(object));
}
await Promise.all(promise_array);
break;
}
default:
return new Response("Bad Request", { status: 400 });
Expand Down
14 changes: 7 additions & 7 deletions functions/webdav/mkcol.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { RequestHandlerParams } from "./utils";
import { RequestHandlerParams, ROOT_OBJECT } from "./utils";

export async function handleRequestMkcol({
bucket,
path,
request,
}: RequestHandlerParams) {
// Check if the resource already exists
let resource = await bucket.head(path);
const resource = await bucket.head(path);
if (resource !== null) {
return new Response("Method Not Allowed", { status: 405 });
}

// Check if the parent directory exists
let parent_dir = path.replace(/(\/|^)[^/]*$/, "");

if (parent_dir !== "" && !(await bucket.head(parent_dir))) {
return new Response("Conflict", { status: 409 });
}
const parentPath = path.replace(/(\/|^)[^/]*$/, "");
const parentDir =
parentPath === "" ? ROOT_OBJECT : await bucket.head(parentPath);
if (parentDir === null) return new Response("Conflict", { status: 409 });

await bucket.put(path, "", {
httpMetadata: { contentType: "application/x-directory" },
Expand Down
13 changes: 13 additions & 0 deletions functions/webdav/move.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { RequestHandlerParams } from "./utils";
import { handleRequestCopy } from "./copy";
import { handleRequestDelete } from "./delete";

export async function handleRequestMove({
bucket,
path,
request,
}: RequestHandlerParams) {
const response = await handleRequestCopy({ bucket, path, request });
if (response.status >= 400) return response;
return handleRequestDelete({ bucket, path, request });
}
12 changes: 5 additions & 7 deletions functions/webdav/put.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { notFound } from "@/utils/bucket";
import { RequestHandlerParams } from "./utils";
import { RequestHandlerParams, ROOT_OBJECT } from "./utils";

export async function handleRequestPut({
bucket,
Expand All @@ -11,11 +10,10 @@ export async function handleRequestPut({
}

// Check if the parent directory exists
let parent_dir = path.replace(/(\/|^)[^/]*$/, "");

if (parent_dir !== "" && !(await bucket.head(parent_dir))) {
return new Response("Conflict", { status: 409 });
}
const parentPath = path.replace(/(\/|^)[^/]*$/, "");
const parentDir =
parentPath === "" ? ROOT_OBJECT : await bucket.head(parentPath);
if (parentDir === null) return new Response("Conflict", { status: 409 });

const result = await bucket.put(path, request.body, {
onlyIf: request.headers,
Expand Down
4 changes: 1 addition & 3 deletions functions/webdav/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export async function* listAll(
for await (const obj of r2Objects.objects)
if (!obj.key.startsWith("_$flaredrive$/")) yield obj;

if (r2Objects.truncated) {
cursor = r2Objects.cursor;
}
if (r2Objects.truncated) cursor = r2Objects.cursor;
} while (r2Objects.truncated);
}

Expand Down

0 comments on commit de3979b

Please sign in to comment.