aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/server/LocalApi.js
blob: 644f15b93e30a6158f6afabcc92eea923c43e577 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { ipcRenderer } from 'electron';
import { session } from '@electron/remote';
import du from 'du';

import { getServicePartitionsDirectory } from '../../helpers/service-helpers';

const debug = require('debug')('Ferdi:LocalApi');

export default class LocalApi {
  // Settings
  getAppSettings(type) {
    return new Promise(resolve => {
      ipcRenderer.once('appSettings', (event, resp) => {
        debug('LocalApi::getAppSettings resolves', resp.type, resp.data);
        resolve(resp);
      });

      ipcRenderer.send('getAppSettings', type);
    });
  }

  async updateAppSettings(type, data) {
    debug('LocalApi::updateAppSettings resolves', type, data);
    ipcRenderer.send('updateAppSettings', {
      type,
      data,
    });
  }

  // Services
  async getAppCacheSize() {
    const partitionsDir = getServicePartitionsDirectory();
    return new Promise((resolve, reject) => {
      du(partitionsDir, (err, size) => {
        if (err) reject(err);

        debug('LocalApi::getAppCacheSize resolves', size);
        resolve(size);
      });
    });
  }

  async clearCache(serviceId = null) {
    const s = serviceId
      ? session.fromPartition(`persist:service-${serviceId}`)
      : session.defaultSession;

    debug('LocalApi::clearCache resolves', serviceId || 'clearAppCache');
    await s.clearStorageData({
      storages: [
        'appcache',
        'filesystem',
        'indexdb',
        'shadercache',
        'websql',
        'serviceworkers',
        'cachestorage',
      ],
      quotas: ['temporary', 'persistent', 'syncable'],
    });
    return s.clearCache();
  }
}