Skip to content

Commit

Permalink
Merge pull request ChatGPTNextWeb#4507 from ChatGPTNextWeb/chore-fix
Browse files Browse the repository at this point in the history
feat: fix codes of joining webdav url in client & webdav proxy
  • Loading branch information
Dean-YZG authored Apr 12, 2024
2 parents 55a93e7 + 55d7014 commit af3ebac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
25 changes: 18 additions & 7 deletions app/api/webdav/[...path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function handle(
}

const endpointPath = params.path.join("/");
const targetPath = `${endpoint}/${endpointPath}`;
const targetPath = `${endpoint}${endpointPath}`;

// only allow MKCOL, GET, PUT
if (req.method !== "MKCOL" && req.method !== "GET" && req.method !== "PUT") {
Expand Down Expand Up @@ -96,7 +96,7 @@ async function handle(
);
}

const targetUrl = `${endpoint}/${endpointPath}`;
const targetUrl = targetPath;

const method = req.method;
const shouldNotHaveBody = ["get", "head"].includes(
Expand All @@ -114,12 +114,23 @@ async function handle(
duplex: "half",
};

const fetchResult = await fetch(targetUrl, fetchOptions);
let fetchResult;

console.log("[Any Proxy]", targetUrl, {
status: fetchResult.status,
statusText: fetchResult.statusText,
});
try {
fetchResult = await fetch(targetUrl, fetchOptions);
} finally {
console.log(
"[Any Proxy]",
targetUrl,
{
method: req.method,
},
{
status: fetchResult?.status,
statusText: fetchResult?.statusText,
},
);
}

return fetchResult;
}
Expand Down
1 change: 1 addition & 0 deletions app/store/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const useSyncStore = createPersistStore(
setLocalAppState(localState);
} catch (e) {
console.log("[Sync] failed to get remote state", e);
throw e;
}

await client.set(config.username, JSON.stringify(localState));
Expand Down
8 changes: 4 additions & 4 deletions app/utils/cloud/webdav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export function createWebDavClient(store: SyncStore) {
};
},
path(path: string, proxyUrl: string = "") {
if (!path.endsWith("/")) {
path += "/";
}
// if (!path.endsWith("/")) {
// path += "/";
// }
if (path.startsWith("/")) {
path = path.slice(1);
}
Expand All @@ -76,7 +76,7 @@ export function createWebDavClient(store: SyncStore) {

let url;
if (proxyUrl.length > 0 || proxyUrl === "/") {
let u = new URL(proxyUrl + "/api/webdav/" + path);
let u = new URL(proxyUrl + "api/webdav/" + path);
// add query params
u.searchParams.append("endpoint", config.endpoint);
url = u.toString();
Expand Down

0 comments on commit af3ebac

Please sign in to comment.