Compare commits

...

2 Commits

Author SHA1 Message Date
Tiger Ren dec8e60a2f 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.
2025-01-08 01:31:06 +08:00
Tiger Ren f149804dba Update package dependencies: add node-fetch and related modules
- Added "node-fetch" version 3.3.2 to both package.json and package-lock.json for improved HTTP request handling.
- Included additional dependencies such as "data-uri-to-buffer", "fetch-blob", and "formdata-polyfill" in package-lock.json to support new features.
- Updated existing dependencies to ensure compatibility with the latest versions.
2025-01-08 01:14:13 +08:00
3 changed files with 157 additions and 8 deletions

75
main.js
View File

@ -5,12 +5,17 @@ const https = require('https');
let store; let store;
let mainWindow; let mainWindow;
let fetch;
// Initialize store using dynamic import // Initialize store and fetch using dynamic import
(async () => { (async () => {
const Store = await import('electron-store'); const [Store, { default: nodeFetch }] = await Promise.all([
import('electron-store'),
import('node-fetch')
]);
store = new Store.default(); store = new Store.default();
console.log('Store initialized'); fetch = nodeFetch;
console.log('Store and fetch initialized');
})(); })();
function createWindow() { 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); 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);
}
}); });

87
package-lock.json generated
View File

@ -9,7 +9,8 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"electron-store": "^10.0.0" "electron-store": "^10.0.0",
"node-fetch": "^3.3.2"
}, },
"devDependencies": { "devDependencies": {
"electron": "^33.2.1" "electron": "^33.2.1"
@ -255,6 +256,14 @@
"node": ">=10" "node": ">=10"
} }
}, },
"node_modules/data-uri-to-buffer": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz",
"integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==",
"engines": {
"node": ">= 12"
}
},
"node_modules/debounce-fn": { "node_modules/debounce-fn": {
"version": "6.0.0", "version": "6.0.0",
"resolved": "https://registry.npmjs.org/debounce-fn/-/debounce-fn-6.0.0.tgz", "resolved": "https://registry.npmjs.org/debounce-fn/-/debounce-fn-6.0.0.tgz",
@ -541,6 +550,39 @@
"pend": "~1.2.0" "pend": "~1.2.0"
} }
}, },
"node_modules/fetch-blob": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz",
"integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/jimmywarting"
},
{
"type": "paypal",
"url": "https://paypal.me/jimmywarting"
}
],
"dependencies": {
"node-domexception": "^1.0.0",
"web-streams-polyfill": "^3.0.3"
},
"engines": {
"node": "^12.20 || >= 14.13"
}
},
"node_modules/formdata-polyfill": {
"version": "4.0.10",
"resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
"integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==",
"dependencies": {
"fetch-blob": "^3.1.2"
},
"engines": {
"node": ">=12.20.0"
}
},
"node_modules/fs-extra": { "node_modules/fs-extra": {
"version": "8.1.0", "version": "8.1.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz",
@ -783,6 +825,41 @@
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
"dev": true "dev": true
}, },
"node_modules/node-domexception": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
"integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/jimmywarting"
},
{
"type": "github",
"url": "https://paypal.me/jimmywarting"
}
],
"engines": {
"node": ">=10.5.0"
}
},
"node_modules/node-fetch": {
"version": "3.3.2",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz",
"integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==",
"dependencies": {
"data-uri-to-buffer": "^4.0.0",
"fetch-blob": "^3.1.4",
"formdata-polyfill": "^4.0.10"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/node-fetch"
}
},
"node_modules/normalize-url": { "node_modules/normalize-url": {
"version": "6.1.0", "version": "6.1.0",
"resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-6.1.0.tgz", "resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-6.1.0.tgz",
@ -999,6 +1076,14 @@
"node": ">= 4.0.0" "node": ">= 4.0.0"
} }
}, },
"node_modules/web-streams-polyfill": {
"version": "3.3.3",
"resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz",
"integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==",
"engines": {
"node": ">= 8"
}
},
"node_modules/when-exit": { "node_modules/when-exit": {
"version": "2.1.3", "version": "2.1.3",
"resolved": "https://registry.npmjs.org/when-exit/-/when-exit-2.1.3.tgz", "resolved": "https://registry.npmjs.org/when-exit/-/when-exit-2.1.3.tgz",

View File

@ -14,6 +14,7 @@
"electron": "^33.2.1" "electron": "^33.2.1"
}, },
"dependencies": { "dependencies": {
"electron-store": "^10.0.0" "electron-store": "^10.0.0",
"node-fetch": "^3.3.0"
} }
} }