Skip to content

Commit

Permalink
fix(WebDAV): fetching file metadata from WebDAV server (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodenn committed Mar 19, 2023
1 parent 6dc4356 commit 8229131
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"request": "launch",
"name": "Debug",
"url": "http://localhost:5173/",
"runtimeArgs": [
"--disable-web-security"
],
"webRoot": "${workspaceFolder}"
},
{
Expand Down
5 changes: 5 additions & 0 deletions src/components/AppThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ const getThemeOptions = (mode: PaletteMode): ThemeOptions => ({
},
},
},
MuiTextField: {
defaultProps: {
spellCheck: false,
},
},
},
palette: {
mode,
Expand Down
2 changes: 1 addition & 1 deletion src/components/FileCreateDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ const WebFileCreateDialog = (props: FileCreateDialogProps) => {
cleanupFileCreateDialog();
setFileName("");
setSkip(undefined);
setSelectedCloudStorage("Dropbox");
setSelectedCloudStorage("no-sync");
};

const init = useCallback(
Expand Down
16 changes: 13 additions & 3 deletions src/components/WebDAVDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LoadingButton } from "@mui/lab";
import {
Alert,
AlertTitle,
Expand Down Expand Up @@ -31,18 +32,21 @@ const WebDavDialog = () => {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState<any>();
const [loading, setLoading] = useState(false);
const { createClient, openStorageConnectedAlert } = useCloudStorage();
const fullScreenDialog = useMediaQuery(theme.breakpoints.down("sm"));

const handleSubmit = async () => {
setError(undefined);
setLoading(true);
try {
await saveWebDAVCredentials({ username, password, url });
closeCloudFileDialog();
await createClient("WebDAV");
await openStorageConnectedAlert("WebDAV");
} catch (error) {
setError(error);
setLoading(false);
}
};

Expand All @@ -51,6 +55,7 @@ const WebDavDialog = () => {
setUsername("");
setPassword("");
setError(undefined);
setLoading(false);
};

const handleEnter = () => {
Expand Down Expand Up @@ -86,7 +91,7 @@ const WebDavDialog = () => {
label={t("Username")}
fullWidth
variant="outlined"
type="url"
type="text"
inputProps={{
"aria-label": "Username",
}}
Expand Down Expand Up @@ -152,9 +157,14 @@ const WebDavDialog = () => {
<DialogContent>{dialogContent}</DialogContent>
<DialogActions>
<Button onClick={handleClose}>{t("Close")}</Button>
<Button aria-label="Connect" onClick={handleSubmit} disabled={disabled}>
<LoadingButton
aria-label="Connect"
onClick={handleSubmit}
disabled={disabled}
loading={loading}
>
{t("Connect")}
</Button>
</LoadingButton>
</DialogActions>
</Dialog>
);
Expand Down
7 changes: 6 additions & 1 deletion src/native-api/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ async function desktopRequest(opt: RequestOptions) {
...(opt.responseType && { responseType: responseType(opt) }),
...(opt.data && { body: desktopBody(opt) }),
};
const response = await client.request(reqOptions);
const response = await client.request(reqOptions).catch((error) => {
if (typeof error === "string") {
throw new Error(error);
}
throw error;
});
const data = response.data;
return {
status: response.status,
Expand Down
10 changes: 7 additions & 3 deletions src/utils/CloudStorage/webdav-storage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getFilenameFromPath } from "../../native-api/filesystem";
import {
getSecureStorageItem,
removeSecureStorageItem,
Expand Down Expand Up @@ -114,13 +115,16 @@ export async function getFileMetaData(
opt: Omit<FileMetaDataOptions<WebDAVClient>, "cloudStorage">
): Promise<CloudFile> {
const { path, client } = opt;
const filename = getFilenameFromPath(path);
const dirname = path.substring(0, path.length - filename.length);
const results = (await client
.getDirectoryContents(path)
.getDirectoryContents(dirname)
.catch(handleError)) as FileStat[];
if (results.length !== 1) {
const metaData = results.find((i) => i.basename === filename);
if (!metaData) {
throw new CloudFileNotFoundError();
}
return mapToCloudFile(results[0]);
return mapToCloudFile(metaData);
}

export async function downloadFile(
Expand Down

1 comment on commit 8229131

@vercel
Copy link

@vercel vercel bot commented on 8229131 Mar 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

2do-txt – ./

2do-txt-sodenn.vercel.app
2do-txt.vercel.app
2do-txt-git-main-sodenn.vercel.app

Please sign in to comment.