move file to api dir
This commit is contained in:
parent
7b174fe402
commit
82284d27f3
9
src/api/common.ts
Normal file
9
src/api/common.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
export const sendNotification = (content: string) => {
|
||||||
|
ipcRenderer.send('send-notification', content);
|
||||||
|
};
|
||||||
|
export const showOnTray = (content: string) => {
|
||||||
|
ipcRenderer.send('update-tray', content);
|
||||||
|
};
|
||||||
|
export const reloadWindow = () => {
|
||||||
|
ipcRenderer.send('reload-window');
|
||||||
|
};
|
|
@ -90,3 +90,11 @@ export const setExportRecord = async (filePath: string, data: string) => {
|
||||||
const filepath = `${filePath}`;
|
const filepath = `${filePath}`;
|
||||||
await fs.writeFile(filepath, data);
|
await fs.writeFile(filepath, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const selectRootDirectory = async () => {
|
||||||
|
try {
|
||||||
|
return await ipcRenderer.invoke('select-dir');
|
||||||
|
} catch (e) {
|
||||||
|
logError(e, 'error while selecting root directory');
|
||||||
|
}
|
||||||
|
};
|
|
@ -2,9 +2,10 @@ import path from 'path';
|
||||||
import StreamZip from 'node-stream-zip';
|
import StreamZip from 'node-stream-zip';
|
||||||
import * as fs from 'promise-fs';
|
import * as fs from 'promise-fs';
|
||||||
import { FILE_STREAM_CHUNK_SIZE } from '../config';
|
import { FILE_STREAM_CHUNK_SIZE } from '../config';
|
||||||
import { uploadStatusStore } from './store';
|
import { uploadStatusStore } from '../services/store';
|
||||||
import { ElectronFile, FILE_PATH_KEYS, FILE_PATH_TYPE } from '../types';
|
import { ElectronFile, FILE_PATH_KEYS, FILE_PATH_TYPE } from '../types';
|
||||||
import { logError } from '../utils/logging';
|
import { logError } from '../utils/logging';
|
||||||
|
import { ipcRenderer } from 'electron';
|
||||||
|
|
||||||
// https://stackoverflow.com/a/63111390
|
// https://stackoverflow.com/a/63111390
|
||||||
export const getFilesFromDir = async (dirPath: string) => {
|
export const getFilesFromDir = async (dirPath: string) => {
|
||||||
|
@ -236,3 +237,47 @@ export const getElectronFilesFromGoogleZip = async (filePath: string) => {
|
||||||
|
|
||||||
return files;
|
return files;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const showUploadDirsDialog = async () => {
|
||||||
|
try {
|
||||||
|
const filePaths: string[] = await ipcRenderer.invoke(
|
||||||
|
'show-upload-dirs-dialog'
|
||||||
|
);
|
||||||
|
const files = await Promise.all(filePaths.map(getElectronFile));
|
||||||
|
return files;
|
||||||
|
} catch (e) {
|
||||||
|
logError(e, 'error while selecting folders');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const showUploadFilesDialog = async () => {
|
||||||
|
try {
|
||||||
|
const filePaths: string[] = await ipcRenderer.invoke(
|
||||||
|
'show-upload-files-dialog'
|
||||||
|
);
|
||||||
|
const files = await Promise.all(filePaths.map(getElectronFile));
|
||||||
|
return files;
|
||||||
|
} catch (e) {
|
||||||
|
logError(e, 'error while selecting files');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const showUploadZipDialog = async () => {
|
||||||
|
try {
|
||||||
|
const filePaths: string[] = await ipcRenderer.invoke(
|
||||||
|
'show-upload-zip-dialog'
|
||||||
|
);
|
||||||
|
const files: ElectronFile[] = [];
|
||||||
|
|
||||||
|
for (const filePath of filePaths) {
|
||||||
|
files.push(...(await getElectronFilesFromGoogleZip(filePath)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
zipPaths: filePaths,
|
||||||
|
files,
|
||||||
|
};
|
||||||
|
} catch (e) {
|
||||||
|
logError(e, 'error while selecting zips');
|
||||||
|
}
|
||||||
|
};
|
|
@ -1,13 +1,14 @@
|
||||||
import { webFrame, ipcRenderer } from 'electron';
|
import { reloadWindow, sendNotification, showOnTray } from './api/common';
|
||||||
import {
|
import {
|
||||||
|
showUploadDirsDialog,
|
||||||
|
showUploadFilesDialog,
|
||||||
|
showUploadZipDialog,
|
||||||
getElectronFile,
|
getElectronFile,
|
||||||
getPendingUploads,
|
getPendingUploads,
|
||||||
setToUploadFiles,
|
setToUploadFiles,
|
||||||
getElectronFilesFromGoogleZip,
|
getElectronFilesFromGoogleZip,
|
||||||
setToUploadCollection,
|
setToUploadCollection,
|
||||||
} from './services/upload';
|
} from './api/upload';
|
||||||
import { logError } from './utils/logging';
|
|
||||||
import { ElectronFile } from './types';
|
|
||||||
import { getEncryptionKey, setEncryptionKey } from './utils/safeStorage';
|
import { getEncryptionKey, setEncryptionKey } from './utils/safeStorage';
|
||||||
import { clearElectronStore } from './utils/electronStore';
|
import { clearElectronStore } from './utils/electronStore';
|
||||||
import { openDiskCache, deleteDiskCache } from './utils/cache';
|
import { openDiskCache, deleteDiskCache } from './utils/cache';
|
||||||
|
@ -23,85 +24,14 @@ import {
|
||||||
getExportRecord,
|
getExportRecord,
|
||||||
setExportRecord,
|
setExportRecord,
|
||||||
exists,
|
exists,
|
||||||
} from './services/export';
|
selectRootDirectory,
|
||||||
|
} from './api/export';
|
||||||
// Patch the global WebSocket constructor to use the correct DevServer url
|
import { fixHotReloadNext12 } from './utils/preload';
|
||||||
const fixHotReloadNext12 = () => {
|
|
||||||
webFrame.executeJavaScript(`Object.defineProperty(globalThis, 'WebSocket', {
|
|
||||||
value: new Proxy(WebSocket, {
|
|
||||||
construct: (Target, [url, protocols]) => {
|
|
||||||
if (url.endsWith('/_next/webpack-hmr')) {
|
|
||||||
// Fix the Next.js hmr client url
|
|
||||||
return new Target("ws://localhost:3000/_next/webpack-hmr", protocols)
|
|
||||||
} else {
|
|
||||||
return new Target(url, protocols)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});`);
|
|
||||||
};
|
|
||||||
|
|
||||||
fixHotReloadNext12();
|
fixHotReloadNext12();
|
||||||
|
|
||||||
const selectRootDirectory = async () => {
|
|
||||||
try {
|
|
||||||
return await ipcRenderer.invoke('select-dir');
|
|
||||||
} catch (e) {
|
|
||||||
logError(e, 'error while selecting root directory');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const sendNotification = (content: string) => {
|
|
||||||
ipcRenderer.send('send-notification', content);
|
|
||||||
};
|
|
||||||
const showOnTray = (content: string) => {
|
|
||||||
ipcRenderer.send('update-tray', content);
|
|
||||||
};
|
|
||||||
|
|
||||||
const reloadWindow = () => {
|
|
||||||
ipcRenderer.send('reload-window');
|
|
||||||
};
|
|
||||||
|
|
||||||
const showUploadFilesDialog = async () => {
|
|
||||||
try {
|
|
||||||
const filePaths: string[] = await ipcRenderer.invoke(
|
|
||||||
'show-upload-files-dialog'
|
|
||||||
);
|
|
||||||
const files = await Promise.all(filePaths.map(getElectronFile));
|
|
||||||
return files;
|
|
||||||
} catch (e) {
|
|
||||||
logError(e, 'error while selecting files');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const showUploadDirsDialog = async () => {
|
|
||||||
try {
|
|
||||||
const filePaths: string[] = await ipcRenderer.invoke(
|
|
||||||
'show-upload-dirs-dialog'
|
|
||||||
);
|
|
||||||
const files = await Promise.all(filePaths.map(getElectronFile));
|
|
||||||
return files;
|
|
||||||
} catch (e) {
|
|
||||||
logError(e, 'error while selecting folders');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const showUploadZipDialog = async () => {
|
|
||||||
try {
|
|
||||||
const filePaths: string[] = await ipcRenderer.invoke(
|
|
||||||
'show-upload-zip-dialog'
|
|
||||||
);
|
|
||||||
const files: ElectronFile[] = [];
|
|
||||||
for (const filePath of filePaths) {
|
|
||||||
files.push(...(await getElectronFilesFromGoogleZip(filePath)));
|
|
||||||
}
|
|
||||||
return { zipPaths: filePaths, files };
|
|
||||||
} catch (e) {
|
|
||||||
logError(e, 'error while selecting zips');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const windowObject: any = window;
|
const windowObject: any = window;
|
||||||
|
|
||||||
windowObject['ElectronAPIs'] = {
|
windowObject['ElectronAPIs'] = {
|
||||||
exists,
|
exists,
|
||||||
checkExistsAndCreateCollectionDir,
|
checkExistsAndCreateCollectionDir,
|
||||||
|
|
|
@ -10,7 +10,7 @@ import {
|
||||||
import { createWindow } from './createWindow';
|
import { createWindow } from './createWindow';
|
||||||
import { buildContextMenu } from './menu';
|
import { buildContextMenu } from './menu';
|
||||||
import { logErrorSentry } from './sentry';
|
import { logErrorSentry } from './sentry';
|
||||||
import { getFilesFromDir } from '../services/upload';
|
import { getFilesFromDir } from '../api/upload';
|
||||||
|
|
||||||
export default function setupIpcComs(
|
export default function setupIpcComs(
|
||||||
tray: Tray,
|
tray: Tray,
|
||||||
|
|
16
src/utils/preload.ts
Normal file
16
src/utils/preload.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import { webFrame } from 'electron';
|
||||||
|
|
||||||
|
export const fixHotReloadNext12 = () => {
|
||||||
|
webFrame.executeJavaScript(`Object.defineProperty(globalThis, 'WebSocket', {
|
||||||
|
value: new Proxy(WebSocket, {
|
||||||
|
construct: (Target, [url, protocols]) => {
|
||||||
|
if (url.endsWith('/_next/webpack-hmr')) {
|
||||||
|
// Fix the Next.js hmr client url
|
||||||
|
return new Target("ws://localhost:3000/_next/webpack-hmr", protocols)
|
||||||
|
} else {
|
||||||
|
return new Target(url, protocols)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});`);
|
||||||
|
};
|
Loading…
Reference in a new issue