Merge pull request #681 from ente-io/add-electron-cache

use electron cache for bundled app
This commit is contained in:
Abhinav Kumar 2022-08-10 16:29:32 +05:30 committed by GitHub
commit 54e52933de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 60 additions and 14 deletions

View file

@ -25,7 +25,7 @@ import FormPaperFooter from 'components/Form/FormPaper/Footer';
import LinkButton from 'components/pages/gallery/LinkButton';
import { CustomError } from 'utils/error';
import isElectron from 'is-electron';
import desktopService from 'services/desktopService';
import safeStorageService from 'services/electron/safeStorage';
import VerticallyCentered from 'components/Container';
import EnteSpinner from 'components/EnteSpinner';
import { Input } from '@mui/material';
@ -43,7 +43,7 @@ export default function Credentials() {
const keyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES);
let key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
if (!key && isElectron()) {
key = await desktopService.getEncryptionKey();
key = await safeStorageService.getEncryptionKey();
if (key) {
await saveKeyInSessionStore(
SESSION_KEYS.ENCRYPTION_KEY,

View file

@ -13,7 +13,7 @@ import { logError } from 'utils/sentry';
import { getAlbumSiteHost, PAGES } from 'constants/pages';
import { EnteLogo } from 'components/EnteLogo';
import isElectron from 'is-electron';
import desktopService from 'services/desktopService';
import safeStorageService from 'services/electron/safeStorage';
import { saveKeyInSessionStore } from 'utils/crypto';
import { getKey, SESSION_KEYS } from 'utils/storage/sessionStorage';
@ -128,7 +128,7 @@ export default function LandingPage() {
const user = getData(LS_KEYS.USER);
let key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
if (!key && isElectron()) {
key = await desktopService.getEncryptionKey();
key = await safeStorageService.getEncryptionKey();
if (key) {
await saveKeyInSessionStore(
SESSION_KEYS.ENCRYPTION_KEY,

View file

@ -12,6 +12,10 @@ import { EnteFile } from 'types/file';
import { logError } from 'utils/sentry';
import { FILE_TYPE } from 'constants/file';
import { CustomError } from 'utils/error';
import electronService from 'services/electron/common';
import electronCacheService from 'services/electron/cache';
const THUMB_CACHE = 'thumbs';
class DownloadManager {
private fileObjectURLPromise = new Map<string, Promise<string[]>>();
@ -27,10 +31,15 @@ class DownloadManager {
const downloadPromise = async () => {
const thumbnailCache = await (async () => {
try {
return await caches.open('thumbs');
if (electronService.checkIsBundledApp()) {
return await electronCacheService.open(
THUMB_CACHE
);
} else {
return await caches.open(THUMB_CACHE);
}
} catch (e) {
return null;
// ignore
logError(e, 'cache open failed');
}
})();
@ -48,6 +57,7 @@ class DownloadManager {
new Response(thumbBlob)
);
} catch (e) {
logError(e, 'cache put failed');
// TODO: handle storage full exception.
}
return URL.createObjectURL(thumbBlob);

View file

@ -0,0 +1,18 @@
import { runningInBrowser } from 'utils/common';
class ElectronCacheService {
private ElectronAPIs: any;
private allElectronAPIsExist: boolean = false;
constructor() {
this.ElectronAPIs = runningInBrowser() && window['ElectronAPIs'];
this.allElectronAPIsExist = !!this.ElectronAPIs?.openLocalCache;
}
async open(cacheName: string) {
if (this.allElectronAPIsExist) {
return await this.ElectronAPIs.openLocalCache(cacheName);
}
}
}
export default new ElectronCacheService();

View file

@ -0,0 +1,18 @@
import isElectron from 'is-electron';
import { runningInBrowser } from 'utils/common';
class ElectronService {
ElectronAPIs: any;
private isBundledApp: boolean = false;
constructor() {
this.ElectronAPIs = runningInBrowser() && window['ElectronAPIs'];
this.isBundledApp = !!this.ElectronAPIs?.openLocalCache;
}
checkIsBundledApp() {
return isElectron() && this.isBundledApp;
}
}
export default new ElectronService();

View file

@ -1,7 +1,7 @@
import { runningInBrowser } from 'utils/common';
import { logError } from 'utils/sentry';
class DesktopService {
class SafeStorageService {
private ElectronAPIs: any;
private allElectronAPIsExist: boolean = false;
constructor() {
@ -35,8 +35,8 @@ class DesktopService {
return await this.ElectronAPIs.clearElectronStore();
}
} catch (e) {
logError(e, 'getEncryptionKey failed');
logError(e, 'clearElectronStore failed');
}
}
}
export default new DesktopService();
export default new SafeStorageService();

View file

@ -20,7 +20,7 @@ import {
import { getLocalFamilyData, isPartOfFamily } from 'utils/billing';
import { ServerErrorCodes } from 'utils/error';
import isElectron from 'is-electron';
import desktopService from './desktopService';
import safeStorageService from './electron/safeStorage';
const ENDPOINT = getEndpoint();
@ -124,7 +124,7 @@ export const logoutUser = async () => {
}
await clearFiles();
if (isElectron()) {
desktopService.clearElectronStore();
safeStorageService.clearElectronStore();
}
router.push(PAGES.ROOT);
} catch (e) {

View file

@ -8,7 +8,7 @@ import { setRecoveryKey } from 'services/userService';
import { logError } from 'utils/sentry';
import { ComlinkWorker } from 'utils/comlink';
import isElectron from 'is-electron';
import desktopService from 'services/desktopService';
import safeStorageService from 'services/electron/safeStorage';
export interface B64EncryptionResult {
encryptedData: string;
@ -103,7 +103,7 @@ export const saveKeyInSessionStore = async (
const sessionKeyAttributes = await cryptoWorker.encryptToB64(key);
setKey(keyType, sessionKeyAttributes);
if (isElectron() && !fromDesktop) {
desktopService.setEncryptionKey(key);
safeStorageService.setEncryptionKey(key);
}
};