Compare commits

..

1 Commits

Author SHA1 Message Date
Tiger Ren 0760fb2c2a Update dependencies and refactor Graph API integration
- Added isomorphic-fetch to package.json and package-lock.json for improved fetch capabilities.
- Removed the deprecated graphApi.js file and refactored the renderer to utilize a new graph service structure.
- Updated renderer.js to enhance folder content retrieval and authentication flow, ensuring better management of access tokens.
- Improved logging for better visibility during the sync process and error handling.
2025-01-22 17:26:01 +08:00
9 changed files with 138 additions and 37 deletions

53
package-lock.json generated
View File

@ -13,6 +13,7 @@
"@electron/remote": "^2.1.2",
"@microsoft/microsoft-graph-client": "^3.0.2",
"electron-store": "^10.0.0",
"isomorphic-fetch": "^3.0.0",
"node-fetch": "^3.3.0",
"uuid": "^11.0.4"
},
@ -787,6 +788,34 @@
"node": ">=10.19.0"
}
},
"node_modules/isomorphic-fetch": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-3.0.0.tgz",
"integrity": "sha512-qvUtwJ3j6qwsF3jLxkZ72qCgjMysPzDfeV240JHiGZsANBYd+EEuu35v7dfrJ9Up0Ak07D7GGSkGhCHTqg/5wA==",
"dependencies": {
"node-fetch": "^2.6.1",
"whatwg-fetch": "^3.4.1"
}
},
"node_modules/isomorphic-fetch/node_modules/node-fetch": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
"integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==",
"dependencies": {
"whatwg-url": "^5.0.0"
},
"engines": {
"node": "4.x || >=6.0.0"
},
"peerDependencies": {
"encoding": "^0.1.0"
},
"peerDependenciesMeta": {
"encoding": {
"optional": true
}
}
},
"node_modules/json-buffer": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz",
@ -1174,6 +1203,11 @@
"node": ">= 8.0"
}
},
"node_modules/tr46": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw=="
},
"node_modules/tslib": {
"version": "2.8.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
@ -1235,6 +1269,25 @@
"node": ">= 8"
}
},
"node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ=="
},
"node_modules/whatwg-fetch": {
"version": "3.6.20",
"resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.6.20.tgz",
"integrity": "sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg=="
},
"node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
}
},
"node_modules/when-exit": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/when-exit/-/when-exit-2.1.3.tgz",

View File

@ -14,11 +14,12 @@
"electron": "^33.2.1"
},
"dependencies": {
"@electron/remote": "^2.1.2",
"electron-store": "^10.0.0",
"node-fetch": "^3.3.0",
"uuid": "^11.0.4",
"@azure/msal-node": "^1.18.4",
"@microsoft/microsoft-graph-client": "^3.0.2"
"@electron/remote": "^2.1.2",
"@microsoft/microsoft-graph-client": "^3.0.2",
"electron-store": "^10.0.0",
"isomorphic-fetch": "^3.0.0",
"node-fetch": "^3.3.0",
"uuid": "^11.0.4"
}
}

View File

@ -1,22 +0,0 @@
const { Client } = require('@microsoft/microsoft-graph-client');
require('isomorphic-fetch');
/**
* Creating a Graph client instance via options method. For more information, visit:
* https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/CreatingClientInstance.md#2-create-with-options
* @param {String} accessToken
* @returns
*/
const getGraphClient = (accessToken) => {
// Initialize Graph client
const graphClient = Client.init({
// Use the provided access token to authenticate requests
authProvider: (done) => {
done(null, accessToken);
},
});
return graphClient;
};
module.exports = getGraphClient;

View File

@ -37,8 +37,7 @@ const GRAPH_ENDPOINT_HOST = "https://graph.microsoft.com/"; // include the trail
const protectedResources = {
graphMe: {
endpoint: `${GRAPH_ENDPOINT_HOST}v1.0/me`,
// scopes: ["User.Read"],
scopes: ["OneDrive.ReadWrite", "offline_access", "openid", "profile", "User.Read"]
scopes: ["User.Read"],
}
};

61
renderer/graph/graph.js Normal file
View File

@ -0,0 +1,61 @@
const { Client } = require('@microsoft/microsoft-graph-client');
const { msalConfig } = require('./authConfig');
const AuthProvider = require('./AuthProvider');
require('isomorphic-fetch');
class GraphService {
constructor() {
this.authProvider = new AuthProvider(msalConfig);
this.graphClient = null;
}
async initialize() {
try {
// Get account
await this.authProvider.login();
// Get token with OneDrive scopes
const tokenRequest = {
scopes: ['Files.Read', 'Files.Read.All', 'Sites.Read.All']
};
const authResponse = await this.authProvider.getToken(tokenRequest);
// Initialize Graph client
this.graphClient = Client.init({
authProvider: (done) => {
done(null, authResponse.accessToken);
}
});
return authResponse.accessToken;
} catch (error) {
console.error('Error initializing Graph service:', error);
throw error;
}
}
async listFolderContents(folderPath) {
if (!this.graphClient) {
await this.initialize();
}
try {
// Clean up the path
const cleanPath = folderPath.replace(/^\/+|\/+$/g, '');
const endpoint = `/me/drive/root:/${cleanPath}:/children`;
// Get items from the folder
const response = await this.graphClient.api(endpoint)
.select('id,name,size,file,@microsoft.graph.downloadUrl')
.get();
return response.value;
} catch (error) {
console.error('Error listing folder contents:', error);
throw error;
}
}
}
module.exports = new GraphService();

View File

@ -1,5 +1,8 @@
const { ipcRenderer, session } = require('electron');
const graphApi = require('./graphApi');
const graphApi = require('./graph/graphApi');
// Wait for webview to load
const webview = document.getElementById('main-content');
@ -82,17 +85,23 @@ document.getElementById('sync-button').addEventListener('click', async () => {
syncButton.textContent = 'Syncing...';
syncButton.disabled = true;
// Get folder contents using GraphApiClient
console.log('renderer: Getting folder contents:', onedriveSource);
const items = await graphApi.listFolderContents(onedriveSource);
console.log('renderer: Found items:', items.length);
// Get folder contents using GraphService
console.log('Getting folder contents:', onedriveSource);
const graphService = require('./graph/graph');
// This will handle authentication and get folder contents
const items = await graphService.listFolderContents(onedriveSource);
console.log('Found items:', items.length);
// Process the items
// TODO: Handle the items as needed
showStatus(`Found ${items.length} items`);
// Send the items to the main process
ipcRenderer.send('start-sync', { destFolder, onedriveSource , items});
ipcRenderer.send('start-sync', {
destFolder,
onedriveSource,
items,
accessToken: await graphService.initialize() // Get fresh token for download
});
} catch (error) {
console.error('Sync error:', error);