Merge branch 'add-electron-types' into watch

This commit is contained in:
Abhinav 2022-08-27 18:27:16 +05:30
commit 7f11029bd3
6 changed files with 40 additions and 87 deletions

2
.gitmodules vendored
View file

@ -1,4 +1,4 @@
[submodule "bada-frame"] [submodule "bada-frame"]
path = ui path = ui
url = https://github.com/ente-io/bada-frame url = https://github.com/ente-io/bada-frame
branch = main branch = add-electron-types

View file

@ -1,7 +1,7 @@
import { ipcRenderer } from 'electron/renderer'; import { ipcRenderer } from 'electron/renderer';
import { logError } from '../utils/logging'; import { logError } from '../utils/logging';
export const selectRootDirectory = async () => { export const selectRootDirectory = async (): Promise<string> => {
try { try {
return await ipcRenderer.invoke('select-dir'); return await ipcRenderer.invoke('select-dir');
} catch (e) { } catch (e) {

View file

@ -1,21 +1,14 @@
import { import { readTextFile, writeStream } from './../services/fs';
createDirectory,
doesPathExists,
readTextFile,
renameDirectory,
writeFile,
writeStream,
} from './../services/fs';
import { ipcRenderer } from 'electron'; import { ipcRenderer } from 'electron';
import { logError } from '../utils/logging'; import * as fs from 'promise-fs';
export const exists = (path: string) => { export const exists = (path: string) => {
return doesPathExists(path); return fs.existsSync(path);
}; };
export const checkExistsAndCreateCollectionDir = async (dirPath: string) => { export const checkExistsAndCreateCollectionDir = async (dirPath: string) => {
if (!doesPathExists(dirPath)) { if (!fs.existsSync(dirPath)) {
await createDirectory(dirPath); await fs.mkdir(dirPath);
} }
}; };
@ -23,8 +16,8 @@ export const checkExistsAndRename = async (
oldDirPath: string, oldDirPath: string,
newDirPath: string newDirPath: string
) => { ) => {
if (doesPathExists(oldDirPath)) { if (fs.existsSync(oldDirPath)) {
await renameDirectory(oldDirPath, newDirPath); await fs.rename(oldDirPath, newDirPath);
} }
}; };
@ -36,24 +29,19 @@ export const saveStreamToDisk = (
}; };
export const saveFileToDisk = async (path: string, fileData: any) => { export const saveFileToDisk = async (path: string, fileData: any) => {
await writeFile(path, fileData); await fs.writeFile(path, fileData);
}; };
export const getExportRecord = async (filePath: string) => { export const getExportRecord = async (filePath: string) => {
try { if (!fs.existsSync(filePath)) {
if (!(await doesPathExists(filePath))) { return null;
return null;
}
const recordFile = await readTextFile(filePath);
return recordFile;
} catch (e) {
// ignore exportFile missing
logError(e, 'error while selecting files');
} }
const recordFile = await readTextFile(filePath);
return recordFile;
}; };
export const setExportRecord = async (filePath: string, data: string) => { export const setExportRecord = async (filePath: string, data: string) => {
await writeFile(filePath, data); await fs.writeFile(filePath, data);
}; };
export const registerResumeExportListener = (resumeExport: () => void) => { export const registerResumeExportListener = (resumeExport: () => void) => {

View file

@ -16,7 +16,7 @@ export async function setEncryptionKey(encryptionKey: string) {
} }
} }
export async function getEncryptionKey() { export async function getEncryptionKey(): Promise<string> {
try { try {
const b64EncryptedKey = safeStorageStore.get('encryptionKey'); const b64EncryptedKey = safeStorageStore.get('encryptionKey');
if (b64EncryptedKey) { if (b64EncryptedKey) {

View file

@ -4,7 +4,6 @@ import path from 'path';
import StreamZip from 'node-stream-zip'; import StreamZip from 'node-stream-zip';
import { uploadStatusStore } from '../stores/upload.store'; import { uploadStatusStore } from '../stores/upload.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 { ipcRenderer } from 'electron'; import { ipcRenderer } from 'electron';
async function getZipEntryAsElectronFile( async function getZipEntryAsElectronFile(
@ -90,48 +89,36 @@ export const getElectronFilesFromGoogleZip = async (filePath: string) => {
return files; return files;
}; };
export const showUploadDirsDialog = async () => { export const showUploadFilesDialog = async () => {
try { const filePaths: string[] = await ipcRenderer.invoke(
const filePaths: string[] = await ipcRenderer.invoke( 'show-upload-files-dialog'
'show-upload-dirs-dialog' );
); const files = await Promise.all(filePaths.map(getElectronFile));
const files = await Promise.all(filePaths.map(getElectronFile)); return files;
return files;
} catch (e) {
logError(e, 'error while selecting folders');
}
}; };
export const showUploadFilesDialog = async () => { export const showUploadDirsDialog = async () => {
try { const filePaths: string[] = await ipcRenderer.invoke(
const filePaths: string[] = await ipcRenderer.invoke( 'show-upload-dirs-dialog'
'show-upload-files-dialog' );
); const files = await Promise.all(filePaths.map(getElectronFile));
const files = await Promise.all(filePaths.map(getElectronFile)); return files;
return files;
} catch (e) {
logError(e, 'error while selecting files');
}
}; };
export const showUploadZipDialog = async () => { export const showUploadZipDialog = async () => {
try { const filePaths: string[] = await ipcRenderer.invoke(
const filePaths: string[] = await ipcRenderer.invoke( 'show-upload-zip-dialog'
'show-upload-zip-dialog' );
);
const files: ElectronFile[] = [];
for (const filePath of filePaths) { const files: ElectronFile[] = [];
files.push(...(await getElectronFilesFromGoogleZip(filePath))); for (const filePath of filePaths) {
} files.push(...(await getElectronFilesFromGoogleZip(filePath)));
return {
zipPaths: filePaths,
files,
};
} catch (e) {
logError(e, 'error while selecting zips');
} }
return {
zipPaths: filePaths,
files,
};
}; };
const getSavedPaths = (type: FILE_PATH_TYPE) => { const getSavedPaths = (type: FILE_PATH_TYPE) => {

View file

@ -30,7 +30,6 @@ export const getFileStream = async (filePath: string) => {
async pull(controller) { async pull(controller) {
try { try {
const buff = new Uint8Array(FILE_STREAM_CHUNK_SIZE); const buff = new Uint8Array(FILE_STREAM_CHUNK_SIZE);
// original types were not working correctly // original types were not working correctly
const bytesRead = (await fs.read( const bytesRead = (await fs.read(
file, file,
@ -161,15 +160,6 @@ export async function doesFolderExists(dirPath: string) {
.catch(() => false); .catch(() => false);
} }
export async function doesPathExists(dirPath: string) {
return await fs
.stat(dirPath)
.then((stats: fs.Stats) => {
return stats.isFile() || stats.isDirectory();
})
.catch(() => false);
}
export const convertBrowserStreamToNode = (fileStream: any) => { export const convertBrowserStreamToNode = (fileStream: any) => {
const reader = fileStream.getReader(); const reader = fileStream.getReader();
const rs = new Readable(); const rs = new Readable();
@ -188,19 +178,7 @@ export const convertBrowserStreamToNode = (fileStream: any) => {
return rs; return rs;
}; };
export async function createDirectory(dirPath: string) { export function writeStream(filePath: string, fileStream: ReadableStream<any>) {
await fs.mkdir(dirPath);
}
export async function renameDirectory(oldDirPath: string, newDirPath: string) {
await fs.rename(oldDirPath, newDirPath);
}
export async function writeFile(filePath: string, fileData: any) {
await fs.writeFile(filePath, fileData);
}
export function writeStream(filePath: string, fileStream: any) {
const writeable = fs.createWriteStream(filePath); const writeable = fs.createWriteStream(filePath);
const readable = convertBrowserStreamToNode(fileStream); const readable = convertBrowserStreamToNode(fileStream);
readable.pipe(writeable); readable.pipe(writeable);