Enhance sync functionality with OneDrive integration

- Updated 'start-sync' IPC event to include cookie retrieval for authentication.
- Implemented error handling for missing access tokens and Graph API responses.
- Added dynamic import for 'node-fetch' to facilitate HTTP requests.
- Cleaned and encoded OneDrive paths for API queries.
- Logged available cookies and API error details for better debugging.
This commit is contained in:
Tiger Ren 2025-01-08 01:31:06 +08:00
parent f149804dba
commit dec8e60a2f
1 changed files with 69 additions and 6 deletions

75
main.js
View File

@ -5,12 +5,17 @@ const https = require('https');
let store;
let mainWindow;
let fetch;
// Initialize store using dynamic import
// Initialize store and fetch using dynamic import
(async () => {
const Store = await import('electron-store');
const [Store, { default: nodeFetch }] = await Promise.all([
import('electron-store'),
import('node-fetch')
]);
store = new Store.default();
console.log('Store initialized');
fetch = nodeFetch;
console.log('Store and fetch initialized');
})();
function createWindow() {
@ -200,8 +205,66 @@ ipcMain.on('select-folder', (event) => {
});
});
ipcMain.on('start-sync', (event, config) => {
ipcMain.on('start-sync', async (event, config) => {
console.log('Starting sync with config:', config);
// Here you can implement the sync logic using the config.destFolder
// and config.onedriveSource parameters
try {
// Get cookies from all relevant domains
const cookies = await Promise.all([
session.defaultSession.cookies.get({domain: '.onedrive.com'}),
session.defaultSession.cookies.get({domain: '.live.com'}),
session.defaultSession.cookies.get({domain: '.microsoft.com'})
]);
const allCookies = [].concat(...cookies);
console.log('Available cookies:', allCookies.map(c => c.name));
// Try different possible token cookie names
const accessToken = allCookies.find(
cookie =>
cookie.name === 'access_token' ||
cookie.name === 'AccessToken' ||
cookie.name.startsWith('AccessToken-') ||
cookie.name.includes('Graph')
)?.value;
if (!accessToken) {
throw new Error('Access token not found in cookies');
}
// Clean up and encode the OneDrive path
const cleanPath = config.onedriveSource
.replace(/^\/+|\/+$/g, '') // Remove leading/trailing slashes
.split('/')
.map(segment => encodeURIComponent(segment))
.join('/');
const url = `https://graph.microsoft.com/v1.0/me/drive/root:/${cleanPath}:/children`;
console.log('Querying OneDrive items from:', url);
// Query OneDrive items using Graph API
const response = await fetch(url, {
headers: {
'Authorization': `Bearer ${accessToken}`,
'Accept': 'application/json',
'Origin': 'https://onedrive.live.com',
'Referer': 'https://onedrive.live.com/'
}
});
if (!response.ok) {
const errorData = await response.json();
console.error('Graph API error details:', errorData);
throw new Error(`Graph API error: ${response.status} ${response.statusText}`);
}
const data = await response.json();
console.log('Found items:', data.value.length);
event.reply('sync-items-found', data.value);
} catch (error) {
console.error('Sync error:', error);
event.reply('sync-error', error.message);
}
});