Refactor WebDAV path construction and fix note move operations
- Replace Node.js fetch with Electron net.request for better session handling - Extract WebDAV path building into reusable private methods with proper URL encoding - Add helper methods for category path encoding and attachment path construction - Fix note move operations to use remote category/filename from saved snapshots - Add ensureCategoryDirectoryExists to handle nested category creation - Only move/rename attachment folders when note has any
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const fs = require('node:fs/promises');
|
||||
const path = require('node:path');
|
||||
const { app, BrowserWindow, dialog, ipcMain } = require('electron');
|
||||
const { app, BrowserWindow, dialog, ipcMain, net } = require('electron');
|
||||
|
||||
const rendererUrl = process.env.ELECTRON_RENDERER_URL;
|
||||
const isDev = Boolean(rendererUrl);
|
||||
@@ -90,23 +90,55 @@ ipcMain.handle('desktop:http-request', async (_event, payload) => {
|
||||
const body =
|
||||
payload.bodyBase64 != null
|
||||
? Buffer.from(payload.bodyBase64, 'base64')
|
||||
: payload.bodyText;
|
||||
: payload.bodyText != null
|
||||
? Buffer.from(payload.bodyText, 'utf8')
|
||||
: null;
|
||||
|
||||
const response = await fetch(payload.url, {
|
||||
method: payload.method || 'GET',
|
||||
headers: payload.headers,
|
||||
body,
|
||||
return await new Promise((resolve, reject) => {
|
||||
const request = net.request({
|
||||
url: payload.url,
|
||||
method: payload.method || 'GET',
|
||||
session: BrowserWindow.getAllWindows()[0]?.webContents.session,
|
||||
});
|
||||
|
||||
for (const [name, value] of Object.entries(payload.headers || {})) {
|
||||
request.setHeader(name, value);
|
||||
}
|
||||
|
||||
request.on('response', (response) => {
|
||||
const chunks = [];
|
||||
|
||||
response.on('data', (chunk) => {
|
||||
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
||||
});
|
||||
|
||||
response.on('end', () => {
|
||||
const headers = {};
|
||||
for (const [name, value] of Object.entries(response.headers)) {
|
||||
headers[name] = Array.isArray(value) ? value.join(', ') : String(value ?? '');
|
||||
}
|
||||
|
||||
const buffer = Buffer.concat(chunks);
|
||||
resolve({
|
||||
ok: response.statusCode >= 200 && response.statusCode < 300,
|
||||
status: response.statusCode,
|
||||
statusText: response.statusMessage || '',
|
||||
headers,
|
||||
bodyBase64: buffer.toString('base64'),
|
||||
});
|
||||
});
|
||||
|
||||
response.on('error', reject);
|
||||
});
|
||||
|
||||
request.on('error', reject);
|
||||
|
||||
if (body && body.length > 0) {
|
||||
request.write(body);
|
||||
}
|
||||
|
||||
request.end();
|
||||
});
|
||||
|
||||
const buffer = Buffer.from(await response.arrayBuffer());
|
||||
|
||||
return {
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
headers: Object.fromEntries(response.headers.entries()),
|
||||
bodyBase64: buffer.toString('base64'),
|
||||
};
|
||||
});
|
||||
|
||||
ipcMain.handle('desktop:export-pdf', async (event, payload) => {
|
||||
|
||||
Reference in New Issue
Block a user