159 lines
4.9 KiB
JavaScript
159 lines
4.9 KiB
JavaScript
const { app, BrowserWindow, ipcMain, dialog, session } = require('electron');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const https = require('https');
|
|
|
|
let store;
|
|
let mainWindow;
|
|
|
|
// Initialize store using dynamic import
|
|
(async () => {
|
|
const Store = await import('electron-store');
|
|
store = new Store.default();
|
|
console.log('Store initialized');
|
|
})();
|
|
|
|
function createWindow() {
|
|
console.log('Creating main window...');
|
|
mainWindow = new BrowserWindow({
|
|
width: 1200,
|
|
height: 800,
|
|
webPreferences: {
|
|
nodeIntegration: true,
|
|
contextIsolation: false,
|
|
webviewTag: true
|
|
}
|
|
});
|
|
|
|
// Set up network request monitoring
|
|
console.log('Setting up network request monitoring...');
|
|
session.defaultSession.webRequest.onBeforeRequest(
|
|
{ urls: ['*://*.onedrive.com/*', '*://*.1drv.com/*', '*://*.microsoftpersonalcontent.com/*'] },
|
|
(details, callback) => {
|
|
console.log('Intercepted request:', details.url);
|
|
// Check if this is a video request (typically .mov for Live Photos)
|
|
if (details.url.toLowerCase().includes('.mov') || details.url.toLowerCase().includes('mp4')) {
|
|
console.log('✓ Detected video URL:', details.url);
|
|
if (store) {
|
|
const destFolder = store.get('destFolder', '');
|
|
if (destFolder) {
|
|
// Extract item ID from URL
|
|
const urlObj = new URL(details.url);
|
|
const pathParts = urlObj.pathname.split('/');
|
|
const itemIndex = pathParts.findIndex(part => part === 'items');
|
|
const itemId = itemIndex !== -1 ? pathParts[itemIndex + 1] : null;
|
|
|
|
// Create filename with item ID and extension
|
|
const extension = details.url.toLowerCase().includes('.mov') ? '.mov' : '.mp4';
|
|
const filename = itemId ? `${itemId}${extension}` : `video_${Date.now()}${extension}`;
|
|
|
|
console.log('Downloading video as:', filename);
|
|
console.log('To folder:', destFolder);
|
|
downloadVideo(details.url, path.join(destFolder, filename));
|
|
} else {
|
|
console.warn('⚠ No destination folder configured');
|
|
}
|
|
} else {
|
|
console.error('⚠ Store not initialized');
|
|
}
|
|
}
|
|
callback({ cancel: false });
|
|
}
|
|
);
|
|
|
|
mainWindow.loadFile(path.join(__dirname, 'renderer/index.html'));
|
|
mainWindow.setTitle('OneDrive Photos');
|
|
console.log('Main window created and loaded');
|
|
}
|
|
|
|
function downloadVideo(url, filepath) {
|
|
console.log('Starting video download:', url);
|
|
console.log('Save path:', filepath);
|
|
|
|
const dir = path.dirname(filepath);
|
|
if (!fs.existsSync(dir)) {
|
|
console.log('Creating directory:', dir);
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
}
|
|
|
|
const file = fs.createWriteStream(filepath);
|
|
https.get(url, (response) => {
|
|
console.log('Download started, status:', response.statusCode);
|
|
console.log('Content type:', response.headers['content-type']);
|
|
console.log('Content length:', response.headers['content-length']);
|
|
|
|
response.pipe(file);
|
|
file.on('finish', () => {
|
|
file.close();
|
|
console.log('✓ Video saved successfully:', filepath);
|
|
mainWindow.webContents.send('video-saved', {
|
|
filename: path.basename(filepath),
|
|
path: filepath
|
|
});
|
|
});
|
|
}).on('error', (err) => {
|
|
console.error('⚠ Download error:', err);
|
|
fs.unlink(filepath, () => {
|
|
console.log('Cleaned up failed download file');
|
|
});
|
|
mainWindow.webContents.send('video-save-error', err.message);
|
|
});
|
|
}
|
|
|
|
app.whenReady().then(createWindow);
|
|
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit();
|
|
}
|
|
});
|
|
|
|
app.on('activate', () => {
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
createWindow();
|
|
}
|
|
});
|
|
|
|
// IPC Handlers with debug logs
|
|
ipcMain.on('toggle-config', () => {
|
|
console.log('Toggle config panel requested');
|
|
mainWindow.webContents.send('toggle-config');
|
|
});
|
|
|
|
ipcMain.on('set-config', (event, config) => {
|
|
console.log('Saving configuration:', config);
|
|
if (store) {
|
|
store.set('destFolder', config.destFolder);
|
|
store.set('onedriveSource', config.onedriveSource);
|
|
console.log('✓ Configuration saved successfully');
|
|
} else {
|
|
console.error('⚠ Store not initialized, cannot save config');
|
|
}
|
|
});
|
|
|
|
ipcMain.on('load-config', (event) => {
|
|
console.log('Loading configuration...');
|
|
if (store) {
|
|
const config = {
|
|
destFolder: store.get('destFolder', ''),
|
|
onedriveSource: store.get('onedriveSource', '')
|
|
};
|
|
console.log('Configuration loaded:', config);
|
|
event.reply('config-loaded', config);
|
|
} else {
|
|
console.error('⚠ Store not initialized, cannot load config');
|
|
}
|
|
});
|
|
|
|
ipcMain.on('select-folder', (event) => {
|
|
dialog.showOpenDialog({
|
|
properties: ['openDirectory']
|
|
}).then(result => {
|
|
if (!result.canceled) {
|
|
event.reply('folder-selected', result.filePaths[0]);
|
|
}
|
|
}).catch(err => {
|
|
console.log(err);
|
|
});
|
|
});
|