diff --git a/src/langs/en.json b/src/langs/en.json index 60c1644..c033822 100644 --- a/src/langs/en.json +++ b/src/langs/en.json @@ -28,8 +28,9 @@ "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_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_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_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?", diff --git a/src/langs/zh_cn.json b/src/langs/zh_cn.json index 990f214..bfa1303 100644 --- a/src/langs/zh_cn.json +++ b/src/langs/zh_cn.json @@ -28,8 +28,9 @@ "protocol_dropbox_connecting": "正在连接 Dropbox……\n请不要关闭此弹窗。", "protocol_dropbox_connect_succ": "好!我们作为用户 {{username}} 连接上了 Dropbox!", "protocol_dropbox_connect_succ_revoke": "您已作为用户 {{username}} 连接上了。如果您想取消连接,请点击此按钮。", - "protocol_dropbox_connect_fail": "Dropbox 的回调请求有点异常。您是否拒绝了鉴权?", + "protocol_dropbox_connect_fail": "Dropbox 的回调请求有点异常。您是否网络不顺畅?您是否拒绝了鉴权?", "protocol_dropbox_connect_unknown": "不知道如何处理此 callback:{{params}}", + "protocol_dropbox_no_modal": "您不是在设置页的弹窗里触发 Dropbox 的。中止操作。", "protocol_onedrive_connecting": "正在连接 OneDrive……\n请不要关闭此弹窗。", "protocol_onedrive_connect_succ_revoke": "您已作为用户 {{username}} 连接上了。如果您想取消连接,请点击此按钮。", "protocol_onedrive_connect_fail": "OneDrive 的回调请求有点异常。您是否拒绝了鉴权?", diff --git a/src/langs/zh_tw.json b/src/langs/zh_tw.json index 08f6ef1..7645ff7 100644 --- a/src/langs/zh_tw.json +++ b/src/langs/zh_tw.json @@ -28,8 +28,9 @@ "protocol_dropbox_connecting": "正在連線 Dropbox……\n請不要關閉此彈窗。", "protocol_dropbox_connect_succ": "好!我們作為使用者 {{username}} 連線上了 Dropbox!", "protocol_dropbox_connect_succ_revoke": "您已作為使用者 {{username}} 連線上了。如果您想取消連線,請點選此按鈕。", - "protocol_dropbox_connect_fail": "Dropbox 的回撥請求有點異常。您是否拒絕了鑑權?", + "protocol_dropbox_connect_fail": "Dropbox 的回撥請求有點異常。您是否網路不順暢?您是否拒絕了鑑權?", "protocol_dropbox_connect_unknown": "不知道如何處理此 callback:{{params}}", + "protocol_dropbox_no_modal": "您不是在設定頁的彈窗裡觸發 Dropbox 的。中止操作。", "protocol_onedrive_connecting": "正在連線 OneDrive……\n請不要關閉此彈窗。", "protocol_onedrive_connect_succ_revoke": "您已作為使用者 {{username}} 連線上了。如果您想取消連線,請點選此按鈕。", "protocol_onedrive_connect_fail": "OneDrive 的回撥請求有點異常。您是否拒絕了鑑權?", diff --git a/src/main.ts b/src/main.ts index 7531bcc..655932a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -579,12 +579,20 @@ export default class RemotelySavePlugin extends Plugin { text: val, }); }); + } else { + new Notice(t("protocol_dropbox_no_modal")); + return; } let authRes = await sendAuthReqDropbox( this.settings.dropbox.clientID, this.oauth2Info.verifier, - inputParams.code + inputParams.code, + async (e: any) => { + new Notice(t("protocol_dropbox_connect_fail")); + new Notice(`${e}`); + return; + } ); const self = this; diff --git a/src/remoteForDropbox.ts b/src/remoteForDropbox.ts index 94692c6..f8c2713 100644 --- a/src/remoteForDropbox.ts +++ b/src/remoteForDropbox.ts @@ -41,9 +41,12 @@ export const getDropboxPath = ( if (fileOrFolderPath === "/" || fileOrFolderPath === "") { // special key = `/${remoteBaseDir}`; - } - if (!fileOrFolderPath.startsWith("/")) { - // then this is original path in Obsidian + } else if (fileOrFolderPath.startsWith("/")) { + log.warn( + `why the path ${fileOrFolderPath} starts with '/'? but we just go on.` + ); + key = `/${remoteBaseDir}${fileOrFolderPath}`; + } else { key = `/${remoteBaseDir}/${fileOrFolderPath}`; } if (key.endsWith("/")) { @@ -210,38 +213,49 @@ export interface DropboxSuccessAuthRes { export const sendAuthReq = async ( appKey: string, verifier: string, - authCode: string + authCode: string, + errorCallBack: any ) => { - const resp1 = await fetch("https://api.dropboxapi.com/oauth2/token", { - method: "POST", - body: new URLSearchParams({ - code: authCode, - grant_type: "authorization_code", - code_verifier: verifier, - client_id: appKey, - redirect_uri: `obsidian://${COMMAND_CALLBACK_DROPBOX}`, - }), - }); - const resp2 = (await resp1.json()) as DropboxSuccessAuthRes; - return resp2; + try { + const resp1 = await fetch("https://api.dropboxapi.com/oauth2/token", { + method: "POST", + body: new URLSearchParams({ + code: authCode, + grant_type: "authorization_code", + code_verifier: verifier, + client_id: appKey, + redirect_uri: `obsidian://${COMMAND_CALLBACK_DROPBOX}`, + }), + }); + const resp2 = (await resp1.json()) as DropboxSuccessAuthRes; + return resp2; + } catch (e) { + log.error(e); + await errorCallBack(e); + } }; export const sendRefreshTokenReq = async ( appKey: string, refreshToken: string ) => { - log.info("start auto getting refreshed Dropbox access token."); - const resp1 = await fetch("https://api.dropboxapi.com/oauth2/token", { - method: "POST", - body: new URLSearchParams({ - grant_type: "refresh_token", - refresh_token: refreshToken, - client_id: appKey, - }), - }); - const resp2 = (await resp1.json()) as DropboxSuccessAuthRes; - log.info("finish auto getting refreshed Dropbox access token."); - return resp2; + try { + log.info("start auto getting refreshed Dropbox access token."); + const resp1 = await fetch("https://api.dropboxapi.com/oauth2/token", { + method: "POST", + body: new URLSearchParams({ + grant_type: "refresh_token", + refresh_token: refreshToken, + client_id: appKey, + }), + }); + const resp2 = (await resp1.json()) as DropboxSuccessAuthRes; + log.info("finish auto getting refreshed Dropbox access token."); + return resp2; + } catch (e) { + log.error(e); + throw e; + } }; export const setConfigBySuccessfullAuthInplace = async ( @@ -457,36 +471,34 @@ export const getDropboxClient = ( export const getRemoteMeta = async ( client: WrappedDropboxClient, - fileOrFolderPath: string + remotePath: string ) => { await client.init(); - if (fileOrFolderPath === "" || fileOrFolderPath === "/") { - // filesGetMetadata doesn't support root folder - // we instead try to list files - // if no error occurs, we ensemble a fake result. - const rsp = await retryReq(() => - client.dropbox.filesListFolder({ - path: `/${client.remoteBaseDir}`, - recursive: false, // don't need to recursive here - }) - ); - if (rsp.status !== 200) { - throw Error(JSON.stringify(rsp)); - } - return { - key: fileOrFolderPath, - lastModified: undefined, - size: 0, - remoteType: "dropbox", - etag: undefined, - } as RemoteItem; - } - - const key = getDropboxPath(fileOrFolderPath, client.remoteBaseDir); + // if (remotePath === "" || remotePath === "/") { + // // filesGetMetadata doesn't support root folder + // // we instead try to list files + // // if no error occurs, we ensemble a fake result. + // const rsp = await retryReq(() => + // client.dropbox.filesListFolder({ + // path: `/${client.remoteBaseDir}`, + // recursive: false, // don't need to recursive here + // }) + // ); + // if (rsp.status !== 200) { + // throw Error(JSON.stringify(rsp)); + // } + // return { + // key: remotePath, + // lastModified: undefined, + // size: 0, + // remoteType: "dropbox", + // etag: undefined, + // } as RemoteItem; + // } const rsp = await retryReq(() => client.dropbox.filesGetMetadata({ - path: key, + path: remotePath, }) ); if (rsp.status !== 200) { @@ -659,16 +671,15 @@ export const listAllFromRemote = async (client: WrappedDropboxClient) => { const downloadFromRemoteRaw = async ( client: WrappedDropboxClient, - fileOrFolderPath: string + remotePath: string ) => { await client.init(); - const key = getDropboxPath(fileOrFolderPath, client.remoteBaseDir); const rsp = await retryReq( () => client.dropbox.filesDownload({ - path: key, + path: remotePath, }), - fileOrFolderPath + `downloadFromRemoteRaw=${remotePath}` ); if ((rsp.result as any).fileBlob !== undefined) { // we get a Blob @@ -762,7 +773,8 @@ export const checkConnectivity = async ( callbackFunc?: any ) => { try { - const results = await getRemoteMeta(client, "/"); + await client.init(); + const results = await getRemoteMeta(client, `/${client.remoteBaseDir}`); if (results === undefined) { return false; }