fix dropbox path

This commit is contained in:
fyears 2024-01-10 02:28:42 +08:00
parent 43cf95c417
commit 7b67ba5b26
5 changed files with 85 additions and 62 deletions

View File

@ -28,8 +28,9 @@
"protocol_dropbox_connecting": "Connecting to Dropbox...\nPlease DO NOT close this modal.", "protocol_dropbox_connecting": "Connecting to Dropbox...\nPlease DO NOT close this modal.",
"protocol_dropbox_connect_succ": "Good! We've connected to Dropbox as user {{username}}!", "protocol_dropbox_connect_succ": "Good! We've connected to Dropbox as user {{username}}!",
"protocol_dropbox_connect_succ_revoke": "You've connected as user {{username}}. If you want to disconnect, click this button.", "protocol_dropbox_connect_succ_revoke": "You've connected as user {{username}}. If you want to disconnect, click this button.",
"protocol_dropbox_connect_fail": "Something went wrong from response from Dropbox. Maybe you rejected the auth?", "protocol_dropbox_connect_fail": "Something went wrong from response from Dropbox. Maybe the network connection is not good. Maybe you rejected the auth?",
"protocol_dropbox_connect_unknown": "Do not know how to deal with the callback: {{params}}", "protocol_dropbox_connect_unknown": "Do not know how to deal with the callback: {{params}}",
"protocol_dropbox_no_modal": "You are not startting Dropbox connection from the settings page. Abort.",
"protocol_onedrive_connecting": "Connecting to OneDrive...\nPlease DO NOT close this modal.", "protocol_onedrive_connecting": "Connecting to OneDrive...\nPlease DO NOT close this modal.",
"protocol_onedrive_connect_succ_revoke": "You've connected as user {{username}}. If you want to disconnect, click this button.", "protocol_onedrive_connect_succ_revoke": "You've connected as user {{username}}. If you want to disconnect, click this button.",
"protocol_onedrive_connect_fail": "Something went wrong from response from OneDrive. Maybe you rejected the auth?", "protocol_onedrive_connect_fail": "Something went wrong from response from OneDrive. Maybe you rejected the auth?",

View File

@ -28,8 +28,9 @@
"protocol_dropbox_connecting": "正在连接 Dropbox……\n请不要关闭此弹窗。", "protocol_dropbox_connecting": "正在连接 Dropbox……\n请不要关闭此弹窗。",
"protocol_dropbox_connect_succ": "好!我们作为用户 {{username}} 连接上了 Dropbox", "protocol_dropbox_connect_succ": "好!我们作为用户 {{username}} 连接上了 Dropbox",
"protocol_dropbox_connect_succ_revoke": "您已作为用户 {{username}} 连接上了。如果您想取消连接,请点击此按钮。", "protocol_dropbox_connect_succ_revoke": "您已作为用户 {{username}} 连接上了。如果您想取消连接,请点击此按钮。",
"protocol_dropbox_connect_fail": "Dropbox 的回调请求有点异常。您是否拒绝了鉴权?", "protocol_dropbox_connect_fail": "Dropbox 的回调请求有点异常。您是否网络不顺畅?您是否拒绝了鉴权?",
"protocol_dropbox_connect_unknown": "不知道如何处理此 callback{{params}}", "protocol_dropbox_connect_unknown": "不知道如何处理此 callback{{params}}",
"protocol_dropbox_no_modal": "您不是在设置页的弹窗里触发 Dropbox 的。中止操作。",
"protocol_onedrive_connecting": "正在连接 OneDrive……\n请不要关闭此弹窗。", "protocol_onedrive_connecting": "正在连接 OneDrive……\n请不要关闭此弹窗。",
"protocol_onedrive_connect_succ_revoke": "您已作为用户 {{username}} 连接上了。如果您想取消连接,请点击此按钮。", "protocol_onedrive_connect_succ_revoke": "您已作为用户 {{username}} 连接上了。如果您想取消连接,请点击此按钮。",
"protocol_onedrive_connect_fail": "OneDrive 的回调请求有点异常。您是否拒绝了鉴权?", "protocol_onedrive_connect_fail": "OneDrive 的回调请求有点异常。您是否拒绝了鉴权?",

View File

@ -28,8 +28,9 @@
"protocol_dropbox_connecting": "正在連線 Dropbox……\n請不要關閉此彈窗。", "protocol_dropbox_connecting": "正在連線 Dropbox……\n請不要關閉此彈窗。",
"protocol_dropbox_connect_succ": "好!我們作為使用者 {{username}} 連線上了 Dropbox", "protocol_dropbox_connect_succ": "好!我們作為使用者 {{username}} 連線上了 Dropbox",
"protocol_dropbox_connect_succ_revoke": "您已作為使用者 {{username}} 連線上了。如果您想取消連線,請點選此按鈕。", "protocol_dropbox_connect_succ_revoke": "您已作為使用者 {{username}} 連線上了。如果您想取消連線,請點選此按鈕。",
"protocol_dropbox_connect_fail": "Dropbox 的回撥請求有點異常。您是否拒絕了鑑權?", "protocol_dropbox_connect_fail": "Dropbox 的回撥請求有點異常。您是否網路不順暢?您是否拒絕了鑑權?",
"protocol_dropbox_connect_unknown": "不知道如何處理此 callback{{params}}", "protocol_dropbox_connect_unknown": "不知道如何處理此 callback{{params}}",
"protocol_dropbox_no_modal": "您不是在設定頁的彈窗裡觸發 Dropbox 的。中止操作。",
"protocol_onedrive_connecting": "正在連線 OneDrive……\n請不要關閉此彈窗。", "protocol_onedrive_connecting": "正在連線 OneDrive……\n請不要關閉此彈窗。",
"protocol_onedrive_connect_succ_revoke": "您已作為使用者 {{username}} 連線上了。如果您想取消連線,請點選此按鈕。", "protocol_onedrive_connect_succ_revoke": "您已作為使用者 {{username}} 連線上了。如果您想取消連線,請點選此按鈕。",
"protocol_onedrive_connect_fail": "OneDrive 的回撥請求有點異常。您是否拒絕了鑑權?", "protocol_onedrive_connect_fail": "OneDrive 的回撥請求有點異常。您是否拒絕了鑑權?",

View File

@ -579,12 +579,20 @@ export default class RemotelySavePlugin extends Plugin {
text: val, text: val,
}); });
}); });
} else {
new Notice(t("protocol_dropbox_no_modal"));
return;
} }
let authRes = await sendAuthReqDropbox( let authRes = await sendAuthReqDropbox(
this.settings.dropbox.clientID, this.settings.dropbox.clientID,
this.oauth2Info.verifier, this.oauth2Info.verifier,
inputParams.code inputParams.code,
async (e: any) => {
new Notice(t("protocol_dropbox_connect_fail"));
new Notice(`${e}`);
return;
}
); );
const self = this; const self = this;

View File

@ -41,9 +41,12 @@ export const getDropboxPath = (
if (fileOrFolderPath === "/" || fileOrFolderPath === "") { if (fileOrFolderPath === "/" || fileOrFolderPath === "") {
// special // special
key = `/${remoteBaseDir}`; key = `/${remoteBaseDir}`;
} } else if (fileOrFolderPath.startsWith("/")) {
if (!fileOrFolderPath.startsWith("/")) { log.warn(
// then this is original path in Obsidian `why the path ${fileOrFolderPath} starts with '/'? but we just go on.`
);
key = `/${remoteBaseDir}${fileOrFolderPath}`;
} else {
key = `/${remoteBaseDir}/${fileOrFolderPath}`; key = `/${remoteBaseDir}/${fileOrFolderPath}`;
} }
if (key.endsWith("/")) { if (key.endsWith("/")) {
@ -210,8 +213,10 @@ export interface DropboxSuccessAuthRes {
export const sendAuthReq = async ( export const sendAuthReq = async (
appKey: string, appKey: string,
verifier: string, verifier: string,
authCode: string authCode: string,
errorCallBack: any
) => { ) => {
try {
const resp1 = await fetch("https://api.dropboxapi.com/oauth2/token", { const resp1 = await fetch("https://api.dropboxapi.com/oauth2/token", {
method: "POST", method: "POST",
body: new URLSearchParams({ body: new URLSearchParams({
@ -224,12 +229,17 @@ export const sendAuthReq = async (
}); });
const resp2 = (await resp1.json()) as DropboxSuccessAuthRes; const resp2 = (await resp1.json()) as DropboxSuccessAuthRes;
return resp2; return resp2;
} catch (e) {
log.error(e);
await errorCallBack(e);
}
}; };
export const sendRefreshTokenReq = async ( export const sendRefreshTokenReq = async (
appKey: string, appKey: string,
refreshToken: string refreshToken: string
) => { ) => {
try {
log.info("start auto getting refreshed Dropbox access token."); log.info("start auto getting refreshed Dropbox access token.");
const resp1 = await fetch("https://api.dropboxapi.com/oauth2/token", { const resp1 = await fetch("https://api.dropboxapi.com/oauth2/token", {
method: "POST", method: "POST",
@ -242,6 +252,10 @@ export const sendRefreshTokenReq = async (
const resp2 = (await resp1.json()) as DropboxSuccessAuthRes; const resp2 = (await resp1.json()) as DropboxSuccessAuthRes;
log.info("finish auto getting refreshed Dropbox access token."); log.info("finish auto getting refreshed Dropbox access token.");
return resp2; return resp2;
} catch (e) {
log.error(e);
throw e;
}
}; };
export const setConfigBySuccessfullAuthInplace = async ( export const setConfigBySuccessfullAuthInplace = async (
@ -457,36 +471,34 @@ export const getDropboxClient = (
export const getRemoteMeta = async ( export const getRemoteMeta = async (
client: WrappedDropboxClient, client: WrappedDropboxClient,
fileOrFolderPath: string remotePath: string
) => { ) => {
await client.init(); await client.init();
if (fileOrFolderPath === "" || fileOrFolderPath === "/") { // if (remotePath === "" || remotePath === "/") {
// filesGetMetadata doesn't support root folder // // filesGetMetadata doesn't support root folder
// we instead try to list files // // we instead try to list files
// if no error occurs, we ensemble a fake result. // // if no error occurs, we ensemble a fake result.
const rsp = await retryReq(() => // const rsp = await retryReq(() =>
client.dropbox.filesListFolder({ // client.dropbox.filesListFolder({
path: `/${client.remoteBaseDir}`, // path: `/${client.remoteBaseDir}`,
recursive: false, // don't need to recursive here // recursive: false, // don't need to recursive here
}) // })
); // );
if (rsp.status !== 200) { // if (rsp.status !== 200) {
throw Error(JSON.stringify(rsp)); // throw Error(JSON.stringify(rsp));
} // }
return { // return {
key: fileOrFolderPath, // key: remotePath,
lastModified: undefined, // lastModified: undefined,
size: 0, // size: 0,
remoteType: "dropbox", // remoteType: "dropbox",
etag: undefined, // etag: undefined,
} as RemoteItem; // } as RemoteItem;
} // }
const key = getDropboxPath(fileOrFolderPath, client.remoteBaseDir);
const rsp = await retryReq(() => const rsp = await retryReq(() =>
client.dropbox.filesGetMetadata({ client.dropbox.filesGetMetadata({
path: key, path: remotePath,
}) })
); );
if (rsp.status !== 200) { if (rsp.status !== 200) {
@ -659,16 +671,15 @@ export const listAllFromRemote = async (client: WrappedDropboxClient) => {
const downloadFromRemoteRaw = async ( const downloadFromRemoteRaw = async (
client: WrappedDropboxClient, client: WrappedDropboxClient,
fileOrFolderPath: string remotePath: string
) => { ) => {
await client.init(); await client.init();
const key = getDropboxPath(fileOrFolderPath, client.remoteBaseDir);
const rsp = await retryReq( const rsp = await retryReq(
() => () =>
client.dropbox.filesDownload({ client.dropbox.filesDownload({
path: key, path: remotePath,
}), }),
fileOrFolderPath `downloadFromRemoteRaw=${remotePath}`
); );
if ((rsp.result as any).fileBlob !== undefined) { if ((rsp.result as any).fileBlob !== undefined) {
// we get a Blob // we get a Blob
@ -762,7 +773,8 @@ export const checkConnectivity = async (
callbackFunc?: any callbackFunc?: any
) => { ) => {
try { try {
const results = await getRemoteMeta(client, "/"); await client.init();
const results = await getRemoteMeta(client, `/${client.remoteBaseDir}`);
if (results === undefined) { if (results === undefined) {
return false; return false;
} }