Skip to content

Commit

Permalink
fix: fix webdav file operator
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanyxh committed May 9, 2024
1 parent 084b8fb commit 93da630
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 56 deletions.
4 changes: 0 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
crossorigin
/>

<meta
http-equiv="Content-Security-Policy"
content="upgrade-insecure-requests"
/>
<meta property="og:site_name" content="<%= title %>" />
<meta property="og:locale" content="zh-CN" />
<meta name="theme-color" content="#d3e3fd" />
Expand Down
109 changes: 64 additions & 45 deletions src/filehandle/WebdavFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,55 @@ function createWebdavFileSystemHandle(
if (file.type == 'directory') {
return new WebdavFileSystemDirectoryHandle(
webdav,
parentPath,
parentPath + '/',
file.basename
);
} else {
return new WebdavFileSystemFileHandle(webdav, parentPath, file.basename);
}
}

class WebdavFileSystemWritableFileStream
implements FileSystemWritableFileStream
{
locked: boolean = false;

private webdav: WebDAVClient;
private fullPath: string;

constructor(webdav: WebDAVClient, fullPath: string) {
this.webdav = webdav;
this.fullPath = fullPath;
}

seek(): Promise<void> {
throw new Error('Method not implemented.');
}
truncate(): Promise<void> {
throw new Error('Method not implemented.');
}

abort(): Promise<void> {
throw new Error('Method not implemented.');
}

getWriter(): WritableStreamDefaultWriter<any> {
throw new Error('Method not implemented.');
}

async write(data: FileSystemWriteChunkType): Promise<void> {
if (data instanceof Blob) {
data = await data.arrayBuffer();
}

await this.webdav.putFileContents(this.fullPath, data as string);
}

async close(): Promise<void> {
return void 0;
}
}

class WebdavFileSystemFileHandle implements FileSystemFileHandle {
readonly kind = 'file';

Expand All @@ -49,29 +90,19 @@ class WebdavFileSystemFileHandle implements FileSystemFileHandle {

isSameEntry(other: FileSystemHandle): Promise<boolean>;
isSameEntry(arg: FileSystemHandle): boolean;
isSameEntry(arg: unknown): boolean | Promise<boolean> {
arg;
isSameEntry(): boolean | Promise<boolean> {
throw new Error('Method not implemented.');
}

queryPermission(
arg: FileSystemHandlePermissionDescriptor
): Promise<PermissionStatus> {
arg;
queryPermission(): Promise<PermissionStatus> {
throw new Error('Method not implemented.');
}

requestPermission(
arg: FileSystemHandlePermissionDescriptor
): Promise<PermissionStatus> {
arg;
requestPermission(): Promise<PermissionStatus> {
throw new Error('Method not implemented.');
}

remove(
options?: FileSystemHandleRecursiveOptions | undefined
): Promise<undefined> {
options;
remove(): Promise<undefined> {
throw new Error('Method not implemented.');
}

Expand All @@ -94,14 +125,7 @@ class WebdavFileSystemFileHandle implements FileSystemFileHandle {

const { webdav, fullPath } = this;

return {
async write(data) {
webdav.putFileContents(fullPath, data as string);
},
async close() {
return void 0;
}
} as FileSystemWritableFileStream;
return new WebdavFileSystemWritableFileStream(webdav, fullPath);
}
}

Expand All @@ -127,8 +151,7 @@ class WebdavFileSystemDirectoryHandle implements FileSystemDirectoryHandle {

resolve(possibleDescendant: FileSystemHandle): Promise<string[] | null>;
resolve(possibleDescendant: FileSystemHandle): Promise<string[] | null>;
resolve(possibleDescendant: unknown): Promise<string[] | null> {
possibleDescendant;
resolve(): Promise<string[] | null> {
throw new Error('Method not implemented.');
}

Expand All @@ -144,29 +167,19 @@ class WebdavFileSystemDirectoryHandle implements FileSystemDirectoryHandle {

isSameEntry(other: FileSystemHandle): Promise<boolean>;
isSameEntry(arg: FileSystemHandle): boolean;
isSameEntry(arg: unknown): boolean | Promise<boolean> {
arg;
isSameEntry(): boolean | Promise<boolean> {
throw new Error('Method not implemented.');
}

queryPermission(
arg: FileSystemHandlePermissionDescriptor
): Promise<PermissionStatus> {
arg;
queryPermission(): Promise<PermissionStatus> {
throw new Error('Method not implemented.');
}

requestPermission(
arg: FileSystemHandlePermissionDescriptor
): Promise<PermissionStatus> {
arg;
requestPermission(): Promise<PermissionStatus> {
throw new Error('Method not implemented.');
}

remove(
options?: FileSystemHandleRecursiveOptions | undefined
): Promise<undefined> {
options;
remove(): Promise<undefined> {
throw new Error('Method not implemented.');
}

Expand Down Expand Up @@ -201,7 +214,7 @@ class WebdavFileSystemDirectoryHandle implements FileSystemDirectoryHandle {
createWebdavFileSystemHandle(
curr,
webdav,
fullPath + curr.basename + '/'
fullPath + curr.basename
)
],
done: false
Expand All @@ -217,9 +230,11 @@ class WebdavFileSystemDirectoryHandle implements FileSystemDirectoryHandle {
): Promise<WebdavFileSystemDirectoryHandle> {
const { create = false } = options || {};

const subFullPath = this.fullPath + name + '/';
const subFullPath = this.fullPath + name;

if (!(await this.webdav.exists(subFullPath))) {
const isNotExists = await this.webdav.exists(subFullPath);

if (isNotExists === false) {
if (create === false) {
throw new FilePathNotExistsError(subFullPath);
} else {
Expand All @@ -233,7 +248,11 @@ class WebdavFileSystemDirectoryHandle implements FileSystemDirectoryHandle {
}
}

return new WebdavFileSystemDirectoryHandle(this.webdav, subFullPath, name);
return new WebdavFileSystemDirectoryHandle(
this.webdav,
subFullPath + '/',
name
);
}

async getFileHandle(
Expand All @@ -242,13 +261,13 @@ class WebdavFileSystemDirectoryHandle implements FileSystemDirectoryHandle {
): Promise<WebdavFileSystemFileHandle> {
const { create = false } = options || {};

const subFullPath = this.fullPath + name + '/';
const subFullPath = this.fullPath + name;

if (!(await this.webdav.exists(subFullPath))) {
if (create === false) {
throw new FilePathNotExistsError(subFullPath);
} else {
await this.webdav.createDirectory(subFullPath);
await this.webdav.putFileContents(subFullPath, '');
}
} else {
const stat = (await this.webdav.stat(subFullPath)) as FileStat;
Expand Down
7 changes: 6 additions & 1 deletion src/filehandle/components/FileContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ function AddFileModal({
status: 'error',
message: '无效的文件名'
});
} else if (await isAlwaysExist(current, value)) {
} else if (
/** TODO: When we use webdav this will always initiate the request */ await isAlwaysExist(
current,
value
)
) {
setInputStatus({
name: value,
status: 'error',
Expand Down
14 changes: 9 additions & 5 deletions src/store/useUserStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ const useAppStore = create<UserState & UserActions>((set) => ({
}),
addWebdav(webdav) {
set((prev) => {
const webdavs = { webdavs: [...prev.webdavs, webdav] };
const store = { ...prev, webdavs: [...prev.webdavs, webdav] };

setStorage('user', webdavs);
setStorage('user', { ...prev, ...store });

return webdavs;
return store;
});
},
setWebdavs(webdavs) {
set({ webdavs });
set((prev) => {
const store = { ...prev, webdavs: webdavs };

setStorage('user', { ...prev, ...store });

setStorage('user', webdavs);
return store;
});
}
}));

Expand Down
2 changes: 1 addition & 1 deletion src/utils/localStorageTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function getStorage<T>(key: TKEYS, fallback?: T): T | undefined {
}

try {
return merge({}, fallback, window.JSON.parse(value));
return merge(fallback, window.JSON.parse(value));
} catch (err) {
return fallback;
}
Expand Down

0 comments on commit 93da630

Please sign in to comment.