123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- <template>
- <div></div>
- </template>
- <script>
- //-----------------------------------------------------------------------------
- import Vue from 'vue';
- import Component from 'vue-class-component';
- import _ from 'lodash';
- import bookManager from '../share/bookManager';
- import readerApi from '../../../api/reader';
- import * as utils from '../../../share/utils';
- import * as cryptoUtils from '../../../share/cryptoUtils';
- import localForage from 'localforage';
- const ssCacheStore = localForage.createInstance({
- name: 'ssCacheStore'
- });
- export default @Component({
- watch: {
- serverSyncEnabled: function() {
- this.serverSyncEnabledChanged();
- },
- serverStorageKey: function() {
- this.serverStorageKeyChanged(true);
- },
- settings: function() {
- this.debouncedSaveSettings();
- },
- profiles: function() {
- this.saveProfiles();
- },
- currentProfile: function() {
- this.currentProfileChanged(true);
- },
- },
- })
- class ServerStorage extends Vue {
- created() {
- this.inited = false;
- this.keyInited = false;
- this.commit = this.$store.commit;
- this.prevServerStorageKey = null;
- this.$root.$on('generateNewServerStorageKey', () => {this.generateNewServerStorageKey()});
- this.debouncedSaveSettings = _.debounce(() => {
- this.saveSettings();
- }, 500);
- this.debouncedNotifySuccess = _.debounce(() => {
- this.success('Данные синхронизированы с сервером');
- }, 1000);
- this.oldProfiles = {};
- this.oldSettings = {};
- }
- async init() {
- try {
- this.cachedRecent = await ssCacheStore.getItem('recent');
- if (!this.cachedRecent)
- await this.setCachedRecent({rev: 0, data: {}});
- this.cachedRecentPatch = await ssCacheStore.getItem('recent-patch');
- if (!this.cachedRecentPatch)
- await this.setCachedRecentPatch({rev: 0, data: {}});
- this.cachedRecentMod = await ssCacheStore.getItem('recent-mod');
- if (!this.cachedRecentMod)
- await this.setCachedRecentMod({rev: 0, data: {}});
- if (!this.serverStorageKey) {
- //генерируем новый ключ
- await this.generateNewServerStorageKey();
- } else {
- await this.serverStorageKeyChanged();
- }
- } finally {
- this.inited = true;
- }
- }
- async setCachedRecent(value) {
- await ssCacheStore.setItem('recent', value);
- this.cachedRecent = value;
- }
- async setCachedRecentPatch(value) {
- await ssCacheStore.setItem('recent-patch', value);
- this.cachedRecentPatch = value;
- }
- async setCachedRecentMod(value) {
- await ssCacheStore.setItem('recent-mod', value);
- this.cachedRecentMod = value;
- }
- async generateNewServerStorageKey() {
- const key = utils.toBase58(utils.randomArray(32));
- this.commit('reader/setServerStorageKey', key);
- await this.serverStorageKeyChanged(true);
- }
- async serverSyncEnabledChanged() {
- if (this.serverSyncEnabled) {
- this.prevServerStorageKey = null;
- if (!this.serverStorageKey) {
- //генерируем новый ключ
- await this.generateNewServerStorageKey();
- } else {
- await this.serverStorageKeyChanged(true);
- }
- }
- }
- async serverStorageKeyChanged(force) {
- if (this.prevServerStorageKey != this.serverStorageKey) {
- this.prevServerStorageKey = this.serverStorageKey;
- this.hashedStorageKey = utils.toBase58(cryptoUtils.sha256(this.serverStorageKey));
- this.keyInited = true;
- await this.loadProfiles(force);
- this.checkCurrentProfile();
- await this.currentProfileChanged(force);
- const loadSuccess = await this.loadRecent();
- if (loadSuccess && force)
- await this.saveRecent();
- }
- }
- async currentProfileChanged(force) {
- if (!this.currentProfile)
- return;
- await this.loadSettings(force);
- }
- get serverSyncEnabled() {
- return this.$store.state.reader.serverSyncEnabled;
- }
- get settings() {
- return this.$store.state.reader.settings;
- }
- get settingsRev() {
- return this.$store.state.reader.settingsRev;
- }
- get serverStorageKey() {
- return this.$store.state.reader.serverStorageKey;
- }
- get profiles() {
- return this.$store.state.reader.profiles;
- }
- get profilesRev() {
- return this.$store.state.reader.profilesRev;
- }
- get currentProfile() {
- return this.$store.state.reader.currentProfile;
- }
- get showServerStorageMessages() {
- return this.settings.showServerStorageMessages;
- }
- checkCurrentProfile() {
- if (!this.profiles[this.currentProfile]) {
- this.commit('reader/setCurrentProfile', '');
- }
- }
- success(message) {
- if (this.showServerStorageMessages)
- this.$notify.success({message});
- }
- warning(message) {
- if (this.showServerStorageMessages && !this.offlineModeActive)
- this.$notify.warning({message});
- }
- error(message) {
- if (this.showServerStorageMessages && !this.offlineModeActive)
- this.$notify.error({message});
- }
- async loadSettings(force = false, doNotifySuccess = true) {
- if (!this.keyInited || !this.serverSyncEnabled || !this.currentProfile)
- return;
- const setsId = `settings-${this.currentProfile}`;
- const oldRev = this.settingsRev[setsId] || 0;
- //проверим ревизию на сервере
- if (!force) {
- try {
- const revs = await this.storageCheck({[setsId]: {}});
- if (revs.state == 'success' && revs.items[setsId].rev == oldRev) {
- return;
- }
- } catch(e) {
- this.error(`Ошибка соединения с сервером: ${e.message}`);
- return;
- }
- }
- let sets = null;
- try {
- sets = await this.storageGet({[setsId]: {}});
- } catch(e) {
- this.error(`Ошибка соединения с сервером: ${e.message}`);
- return;
- }
- if (sets.state == 'success') {
- sets = sets.items[setsId];
- if (sets.rev == 0)
- sets.data = {};
- this.oldSettings = _.cloneDeep(sets.data);
- this.commit('reader/setSettings', sets.data);
- this.commit('reader/setSettingsRev', {[setsId]: sets.rev});
- if (doNotifySuccess)
- this.debouncedNotifySuccess();
- } else {
- this.warning(`Неверный ответ сервера: ${sets.state}`);
- }
- }
- async saveSettings() {
- if (!this.keyInited || !this.serverSyncEnabled || !this.currentProfile || this.savingSettings)
- return;
- const diff = utils.getObjDiff(this.oldSettings, this.settings);
- if (utils.isEmptyObjDiff(diff))
- return;
- this.savingSettings = true;
- try {
- const setsId = `settings-${this.currentProfile}`;
- let result = {state: ''};
- const oldRev = this.settingsRev[setsId] || 0;
- try {
- result = await this.storageSet({[setsId]: {rev: oldRev + 1, data: this.settings}});
- } catch(e) {
- this.error(`Ошибка соединения с сервером (${e.message}). Данные не сохранены и могут быть перезаписаны.`);
- }
- if (result.state == 'reject') {
- await this.loadSettings(true, false);
- this.warning(`Последние изменения отменены. Данные синхронизированы с сервером.`);
- } else if (result.state == 'success') {
- this.oldSettings = _.cloneDeep(this.settings);
- this.commit('reader/setSettingsRev', {[setsId]: this.settingsRev[setsId] + 1});
- }
- } finally {
- this.savingSettings = false;
- }
- }
- async loadProfiles(force = false, doNotifySuccess = true) {
- if (!this.keyInited || !this.serverSyncEnabled)
- return;
- const oldRev = this.profilesRev;
- //проверим ревизию на сервере
- if (!force) {
- try {
- const revs = await this.storageCheck({profiles: {}});
- if (revs.state == 'success' && revs.items.profiles.rev == oldRev) {
- return;
- }
- } catch(e) {
- this.error(`Ошибка соединения с сервером: ${e.message}`);
- return;
- }
- }
- let prof = null;
- try {
- prof = await this.storageGet({profiles: {}});
- } catch(e) {
- this.error(`Ошибка соединения с сервером: ${e.message}`);
- return;
- }
- if (prof.state == 'success') {
- prof = prof.items.profiles;
- if (prof.rev == 0)
- prof.data = {};
- this.oldProfiles = _.cloneDeep(prof.data);
- this.commit('reader/setProfiles', prof.data);
- this.commit('reader/setProfilesRev', prof.rev);
- this.checkCurrentProfile();
- if (doNotifySuccess)
- this.debouncedNotifySuccess();
- } else {
- this.warning(`Неверный ответ сервера: ${prof.state}`);
- }
- }
- async saveProfiles() {
- if (!this.keyInited || !this.serverSyncEnabled || this.savingProfiles)
- return;
- const diff = utils.getObjDiff(this.oldProfiles, this.profiles);
- if (utils.isEmptyObjDiff(diff))
- return;
- //обнуляются профили во время разработки при hotReload, подстраховка
- if (!this.$store.state.reader.allowProfilesSave) {
- console.error('Сохранение профилей не санкционировано');
- return;
- }
- this.savingProfiles = true;
- try {
- let result = {state: ''};
- try {
- result = await this.storageSet({profiles: {rev: this.profilesRev + 1, data: this.profiles}});
- } catch(e) {
- this.error(`Ошибка соединения с сервером (${e.message}). Данные не сохранены и могут быть перезаписаны.`);
- }
- if (result.state == 'reject') {
- await this.loadProfiles(true, false);
- this.warning(`Последние изменения отменены. Данные синхронизированы с сервером.`);
- } else if (result.state == 'success') {
- this.oldProfiles = _.cloneDeep(this.profiles);
- this.commit('reader/setProfilesRev', this.profilesRev + 1);
- }
- } finally {
- this.savingProfiles = false;
- }
- }
- async loadRecent(skipRevCheck = false, doNotifySuccess = true) {
- if (!this.keyInited || !this.serverSyncEnabled || this.loadingRecent)
- return;
- this.loadingRecent = true;
- try {
- //проверим ревизию на сервере
- let query = {recent: {}, recentPatch: {}, recentMod: {}};
- let revs = null;
- if (!skipRevCheck) {
- try {
- revs = await this.storageCheck(query);
- if (revs.state == 'success') {
- if (revs.items.recent.rev != this.cachedRecent.rev) {
- //no changes
- } else if (revs.items.recentPatch.rev != this.cachedRecentPatch.rev) {
- query = {recentPatch: {}, recentMod: {}};
- } else if (revs.items.recentMod.rev != this.cachedRecentMod.rev) {
- query = {recentMod: {}};
- } else
- return true;
- }
- } catch(e) {
- this.error(`Ошибка соединения с сервером: ${e.message}`);
- return;
- }
- }
- let recent = null;
- try {
- recent = await this.storageGet(query);
- } catch(e) {
- this.error(`Ошибка соединения с сервером: ${e.message}`);
- return;
- }
- if (recent.state == 'success') {
- let newRecent = recent.items.recent;
- let newRecentPatch = recent.items.recentPatch;
- let newRecentMod = recent.items.recentMod;
- if (!newRecent) {
- newRecent = _.cloneDeep(this.cachedRecent);
- }
- if (!newRecentPatch) {
- newRecentPatch = _.cloneDeep(this.cachedRecentPatch);
- }
- if (!newRecentMod) {
- newRecentMod = _.cloneDeep(this.cachedRecentMod);
- }
- if (newRecent.rev == 0) newRecent.data = {};
- if (newRecentPatch.rev == 0) newRecentPatch.data = {};
- if (newRecentMod.rev == 0) newRecentMod.data = {};
- let result = Object.assign({}, newRecent.data, newRecentPatch.data);
- const md = newRecentMod.data;
- if (md.key && result[md.key])
- result[md.key] = utils.applyObjDiff(result[md.key], md.mod);
- if (newRecent.rev != this.cachedRecent.rev)
- await this.setCachedRecent(newRecent);
- if (newRecentPatch.rev != this.cachedRecentPatch.rev)
- await this.setCachedRecentPatch(newRecentPatch);
- if (newRecentMod.rev != this.cachedRecentMod.rev)
- await this.setCachedRecentMod(newRecentMod);
- if (!bookManager.loaded) {
- this.warning('Ожидание загрузки списка книг перед синхронизацией');
- while (!bookManager.loaded) await utils.sleep(100);
- }
- await bookManager.setRecent(result);
- } else {
- this.warning(`Неверный ответ сервера: ${recent.state}`);
- return;
- }
- if (doNotifySuccess)
- this.debouncedNotifySuccess();
- } finally {
- this.loadingRecent = false;
- }
- return true;
- }
- async saveRecent(itemKey, recurse) {
- if (!this.keyInited || !this.serverSyncEnabled || this.savingRecent)
- return;
- this.savingRecent = true;
- try {
- const bm = bookManager;
- let needSaveRecent = false;
- let needSaveRecentPatch = false;
- let needSaveRecentMod = false;
- //newRecentMod
- let newRecentMod = {};
- if (itemKey && this.cachedRecentPatch.data[itemKey] && this.prevItemKey == itemKey) {
- newRecentMod = _.cloneDeep(this.cachedRecentMod);
- newRecentMod.rev++;
- newRecentMod.data.key = itemKey;
- newRecentMod.data.mod = utils.getObjDiff(this.cachedRecentPatch.data[itemKey], bm.recent[itemKey]);
- needSaveRecentMod = true;
- }
- this.prevItemKey = itemKey;
- //newRecentPatch
- let newRecentPatch = {};
- if (itemKey && !needSaveRecentMod) {
- newRecentPatch = _.cloneDeep(this.cachedRecentPatch);
- newRecentPatch.rev++;
- newRecentPatch.data[itemKey] = bm.recent[itemKey];
- let applyMod = this.cachedRecentMod.data;
- if (applyMod && applyMod.key && newRecentPatch.data[applyMod.key])
- newRecentPatch.data[applyMod.key] = utils.applyObjDiff(newRecentPatch.data[applyMod.key], applyMod.mod);
- newRecentMod = {rev: this.cachedRecentMod.rev + 1, data: {}};
- needSaveRecentPatch = true;
- needSaveRecentMod = true;
- }
- //newRecent
- let newRecent = {};
- if (!itemKey || (needSaveRecentPatch && Object.keys(newRecentPatch.data).length > 10)) {
- newRecent = {rev: this.cachedRecent.rev + 1, data: bm.recent};
- newRecentPatch = {rev: this.cachedRecentPatch.rev + 1, data: {}};
- newRecentMod = {rev: this.cachedRecentMod.rev + 1, data: {}};
- needSaveRecent = true;
- needSaveRecentPatch = true;
- needSaveRecentMod = true;
- }
- //query
- let query = {};
- if (needSaveRecent) {
- query = {recent: newRecent, recentPatch: newRecentPatch, recentMod: newRecentMod};
- } else if (needSaveRecentPatch) {
- query = {recentPatch: newRecentPatch, recentMod: newRecentMod};
- } else {
- query = {recentMod: newRecentMod};
- }
- //сохранение
- let result = {state: ''};
- try {
- result = await this.storageSet(query);
- } catch(e) {
- this.error(`Ошибка соединения с сервером (${e.message}). Данные не сохранены и могут быть перезаписаны.`);
- }
- if (result.state == 'reject') {
- await this.loadRecent(false, false);
- this.warning(`Последние изменения отменены. Данные синхронизированы с сервером.`);
- if (!recurse && itemKey) {
- this.savingRecent = false;
- this.saveRecent(itemKey, true);
- return;
- }
- } else if (result.state == 'success') {
- if (needSaveRecent && newRecent.rev)
- await this.setCachedRecent(newRecent);
- if (needSaveRecentPatch && newRecentPatch.rev)
- await this.setCachedRecentPatch(newRecentPatch);
- if (needSaveRecentMod && newRecentMod.rev)
- await this.setCachedRecentMod(newRecentMod);
- }
- } finally {
- this.savingRecent = false;
- }
- }
- async storageCheck(items) {
- return await this.storageApi('check', items);
- }
- async storageGet(items) {
- return await this.storageApi('get', items);
- }
- async storageSet(items, force) {
- return await this.storageApi('set', items, force);
- }
- async storageApi(action, items, force) {
- const request = {action, items};
- if (force)
- request.force = true;
- const encodedRequest = await this.encodeStorageItems(request);
- return await this.decodeStorageItems(await readerApi.storage(encodedRequest));
- }
- async encodeStorageItems(request) {
- if (!this.hashedStorageKey)
- throw new Error('hashedStorageKey is empty');
- if (!_.isObject(request.items))
- throw new Error('items is not an object');
- let result = Object.assign({}, request);
- let items = {};
- for (const id of Object.keys(request.items)) {
- const item = request.items[id];
- if (request.action == 'set' && !_.isObject(item.data))
- throw new Error('encodeStorageItems: data is not an object');
- let encoded = Object.assign({}, item);
- if (item.data) {
- const comp = utils.pako.deflate(JSON.stringify(item.data), {level: 1});
- let encrypted = null;
- try {
- encrypted = cryptoUtils.aesEncrypt(comp, this.serverStorageKey);
- } catch (e) {
- throw new Error('encrypt failed');
- }
- encoded.data = '1' + utils.toBase64(encrypted);
- }
- items[`${this.hashedStorageKey}.${utils.toBase58(id)}`] = encoded;
- }
- result.items = items;
- return result;
- }
- async decodeStorageItems(response) {
- if (!this.hashedStorageKey)
- throw new Error('hashedStorageKey is empty');
- let result = Object.assign({}, response);
- let items = {};
- if (response.items) {
- if (!_.isObject(response.items))
- throw new Error('items is not an object');
- for (const id of Object.keys(response.items)) {
- const item = response.items[id];
- let decoded = Object.assign({}, item);
- if (item.data) {
- if (!_.isString(item.data) || !item.data.length)
- throw new Error('decodeStorageItems: data is not a string');
- if (item.data[0] !== '1')
- throw new Error('decodeStorageItems: unknown data format');
- const a = utils.fromBase64(item.data.substr(1));
- let decrypted = null;
- try {
- decrypted = cryptoUtils.aesDecrypt(a, this.serverStorageKey);
- } catch (e) {
- throw new Error('decrypt failed');
- }
- decoded.data = JSON.parse(utils.pako.inflate(decrypted, {to: 'string'}));
- }
- const ids = id.split('.');
- if (!(ids.length == 2) || !(ids[0] == this.hashedStorageKey))
- throw new Error(`decodeStorageItems: bad id - ${id}`);
- items[utils.fromBase58(ids[1])] = decoded;
- }
- }
- result.items = items;
- return result;
- }
- }
- //-----------------------------------------------------------------------------
- </script>
|