fix minor bug of onedrive

This commit is contained in:
fyears 2024-01-10 02:48:23 +08:00
parent 7b67ba5b26
commit 7422956621
2 changed files with 66 additions and 50 deletions

View File

@ -674,10 +674,16 @@ export default class RemotelySavePlugin extends Plugin {
this.settings.onedrive.clientID, this.settings.onedrive.clientID,
this.settings.onedrive.authority, this.settings.onedrive.authority,
inputParams.code, inputParams.code,
this.oauth2Info.verifier this.oauth2Info.verifier,
async (e: any) => {
new Notice(t("protocol_onedrive_connect_fail"));
new Notice(`${e}`);
return; // throw?
}
); );
if ((rsp as any).error !== undefined) { if ((rsp as any).error !== undefined) {
new Notice(`${JSON.stringify(rsp)}`);
throw Error(`${JSON.stringify(rsp)}`); throw Error(`${JSON.stringify(rsp)}`);
} }

View File

@ -105,7 +105,8 @@ export const sendAuthReq = async (
clientID: string, clientID: string,
authority: string, authority: string,
authCode: string, authCode: string,
verifier: string verifier: string,
errorCallBack: any
) => { ) => {
// // original code snippets for references // // original code snippets for references
// const authResponse = await pca.acquireTokenByCode({ // const authResponse = await pca.acquireTokenByCode({
@ -123,6 +124,7 @@ export const sendAuthReq = async (
// instead of using msal // instead of using msal
// https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow // https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/graph-oauth?view=odsp-graph-online#code-flow // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/graph-oauth?view=odsp-graph-online#code-flow
try {
const rsp1 = await request({ const rsp1 = await request({
url: `${authority}/oauth2/v2.0/token`, url: `${authority}/oauth2/v2.0/token`,
method: "POST", method: "POST",
@ -146,6 +148,10 @@ export const sendAuthReq = async (
} else { } else {
return rsp2 as AccessCodeResponseSuccessfulType; return rsp2 as AccessCodeResponseSuccessfulType;
} }
} catch (e) {
log.error(e);
await errorCallBack(e);
}
}; };
export const sendRefreshTokenReq = async ( export const sendRefreshTokenReq = async (
@ -154,6 +160,7 @@ export const sendRefreshTokenReq = async (
refreshToken: string refreshToken: string
) => { ) => {
// also use Obsidian request to bypass CORS issue. // also use Obsidian request to bypass CORS issue.
try {
const rsp1 = await request({ const rsp1 = await request({
url: `${authority}/oauth2/v2.0/token`, url: `${authority}/oauth2/v2.0/token`,
method: "POST", method: "POST",
@ -175,6 +182,10 @@ export const sendRefreshTokenReq = async (
} else { } else {
return rsp2 as AccessCodeResponseSuccessfulType; return rsp2 as AccessCodeResponseSuccessfulType;
} }
} catch (e) {
log.error(e);
throw e;
}
}; };
export const setConfigBySuccessfullAuthInplace = async ( export const setConfigBySuccessfullAuthInplace = async (
@ -207,10 +218,6 @@ export const setConfigBySuccessfullAuthInplace = async (
const getOnedrivePath = (fileOrFolderPath: string, remoteBaseDir: string) => { const getOnedrivePath = (fileOrFolderPath: string, remoteBaseDir: string) => {
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/special-folders-appfolder?view=odsp-graph-online // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/special-folders-appfolder?view=odsp-graph-online
const prefix = `/drive/special/approot:/${remoteBaseDir}`; const prefix = `/drive/special/approot:/${remoteBaseDir}`;
if (fileOrFolderPath.startsWith(prefix)) {
// already transformed, return as is
return fileOrFolderPath;
}
let key = fileOrFolderPath; let key = fileOrFolderPath;
if (fileOrFolderPath === "/" || fileOrFolderPath === "") { if (fileOrFolderPath === "/" || fileOrFolderPath === "") {
@ -221,7 +228,12 @@ const getOnedrivePath = (fileOrFolderPath: string, remoteBaseDir: string) => {
key = key.slice(0, key.length - 1); key = key.slice(0, key.length - 1);
} }
if (key.startsWith("/")) {
log.warn(`why the path ${key} starts with '/'? but we just go on.`);
key = `${prefix}${key}`;
} else {
key = `${prefix}/${key}`; key = `${prefix}/${key}`;
}
return key; return key;
}; };
@ -638,10 +650,9 @@ export const listAllFromRemote = async (client: WrappedOnedriveClient) => {
export const getRemoteMeta = async ( export const getRemoteMeta = async (
client: WrappedOnedriveClient, client: WrappedOnedriveClient,
fileOrFolderPath: string remotePath: string
) => { ) => {
await client.init(); await client.init();
const remotePath = getOnedrivePath(fileOrFolderPath, client.remoteBaseDir);
// log.info(`remotePath=${remotePath}`); // log.info(`remotePath=${remotePath}`);
const rsp = await client.getJson( const rsp = await client.getJson(
`${remotePath}?$select=cTag,eTag,fileSystemInfo,folder,file,name,parentReference,size` `${remotePath}?$select=cTag,eTag,fileSystemInfo,folder,file,name,parentReference,size`
@ -796,12 +807,11 @@ export const uploadToRemote = async (
const downloadFromRemoteRaw = async ( const downloadFromRemoteRaw = async (
client: WrappedOnedriveClient, client: WrappedOnedriveClient,
fileOrFolderPath: string remotePath: string
): Promise<ArrayBuffer> => { ): Promise<ArrayBuffer> => {
await client.init(); await client.init();
const key = getOnedrivePath(fileOrFolderPath, client.remoteBaseDir);
const rsp = await client.getJson( const rsp = await client.getJson(
`${key}?$select=@microsoft.graph.downloadUrl` `${remotePath}?$select=@microsoft.graph.downloadUrl`
); );
const downloadUrl: string = rsp["@microsoft.graph.downloadUrl"]; const downloadUrl: string = rsp["@microsoft.graph.downloadUrl"];
if (VALID_REQURL) { if (VALID_REQURL) {