webdav.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { STORAGE_KEY } from "@/app/constant";
  2. import { SyncStore } from "@/app/store/sync";
  3. import { corsFetch } from "../cors";
  4. export type WebDAVConfig = SyncStore["webdav"];
  5. export type WebDavClient = ReturnType<typeof createWebDavClient>;
  6. export function createWebDavClient(store: SyncStore) {
  7. const folder = STORAGE_KEY;
  8. const fileName = `${folder}/backup.json`;
  9. const config = store.webdav;
  10. const proxyUrl =
  11. store.useProxy && store.proxyUrl.length > 0 ? store.proxyUrl : undefined;
  12. return {
  13. async check() {
  14. try {
  15. const res = await corsFetch(this.path(folder), {
  16. method: "MKCOL",
  17. headers: this.headers(),
  18. proxyUrl,
  19. });
  20. console.log("[WebDav] check", res.status, res.statusText);
  21. return [201, 200, 404, 301, 302, 307, 308].includes(res.status);
  22. } catch (e) {
  23. console.error("[WebDav] failed to check", e);
  24. }
  25. return false;
  26. },
  27. async get(key: string) {
  28. const res = await corsFetch(this.path(fileName), {
  29. method: "GET",
  30. headers: this.headers(),
  31. proxyUrl,
  32. });
  33. console.log("[WebDav] get key = ", key, res.status, res.statusText);
  34. return await res.text();
  35. },
  36. async set(key: string, value: string) {
  37. const res = await corsFetch(this.path(fileName), {
  38. method: "PUT",
  39. headers: this.headers(),
  40. body: value,
  41. proxyUrl,
  42. });
  43. console.log("[WebDav] set key = ", key, res.status, res.statusText);
  44. },
  45. headers() {
  46. const auth = btoa(config.username + ":" + config.password);
  47. return {
  48. authorization: `Basic ${auth}`,
  49. };
  50. },
  51. path(path: string) {
  52. let url = config.endpoint;
  53. if (!url.endsWith("/")) {
  54. url += "/";
  55. }
  56. if (path.startsWith("/")) {
  57. path = path.slice(1);
  58. }
  59. return url + path;
  60. },
  61. };
  62. }