Skip to content

Commit

Permalink
support icloud sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Gandum2077 committed Apr 29, 2020
1 parent bbb4377 commit acf1582
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 7 deletions.
23 changes: 23 additions & 0 deletions prefs.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@
}
]
},
{
"title": "ICLOUD_AND_TIPS",
"items": [
{
"title": "Sync Database",
"type": "boolean",
"key": "sync_database",
"value": false
},
{
"title": "Overwrite Database in iCloud with Local Database",
"type": "script",
"key": "overwrite_icloud_database",
"value": "require('scripts/utils/settings').overwriteIcloudDatabase();"
},
{
"title": "Overwrite Local Database with Database in iCloud",
"type": "script",
"key": "overwrite_local_database",
"value": "require('scripts/utils/settings').overwriteLocalDatabase();"
}
]
},
{
"title": "STORAGE",
"items": [
Expand Down
16 changes: 11 additions & 5 deletions scripts/control/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const constants = require("../utils/constants");
const database = require("../utils/database");
const icloudManager = require("../utils/icloud");
const inputAlert = require("../dialogs/inputAlert");
const editListDialogs = require("../dialogs/editListDialogs");
const { ContentView } = require("../views/views");
Expand Down Expand Up @@ -554,15 +555,20 @@ class Controller {
}

async openPrefs() {
await $prefs.open();
this.checkPrefs();
await $prefs.open(() => this.checkPrefs());
}

checkPrefs() {
const intervals = $prefs.get("slideshow_intervals");
if (intervals < 1 || intervals > 30) {
$prefs.set("slideshow_intervals", 5);
}
if (intervals < 1 || intervals > 30) $prefs.set("slideshow_intervals", 5);
const databaseSyncEnabled = $prefs.get("sync_database");
if (!$file.exists(constants.databaseFileOnIcloud))
icloudManager.copyDatabaseToIcloud();
database.closeDB();
database.databaseFile = databaseSyncEnabled
? constants.databaseFileOnIcloud
: constants.databaseFile;
database.openDB();
}
}

Expand Down
5 changes: 5 additions & 0 deletions scripts/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const savedTagsFile = "assets/savedTags.json";
const favoritedTagsFile = "assets/favoritedTags.json";
const tagCombinationsFile = "assets/tagCombinations.json";

const icloudSyncPath = "drive://._JSBooruSync";
const databaseFileOnIcloud = "drive://._JSBooruSync/database.db";

const sitesConfig = JSON.parse($file.read(sitesConfigFile).string);

class UserConfig {
Expand Down Expand Up @@ -104,6 +107,8 @@ module.exports = {
savedTagsFile,
favoritedTagsFile,
tagCombinationsFile,
icloudSyncPath,
databaseFileOnIcloud,
sitesConfig,
userConfig
};
6 changes: 4 additions & 2 deletions scripts/utils/database.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { databaseFile } = require("./constants");
const { databaseFile, databaseFileOnIcloud } = require("./constants");

class Database {
constructor(databaseFile) {
Expand Down Expand Up @@ -368,6 +368,8 @@ class Database {
}
}

const database = new Database(databaseFile);
const database = new Database(
$prefs.get("sync_database") ? databaseFileOnIcloud : databaseFile
);

module.exports = database;
37 changes: 37 additions & 0 deletions scripts/utils/icloud.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const constants = require("./constants");

class IcloudManager {
constructor(
path = constants.icloudSyncPath,
databaseFile = constants.databaseFileOnIcloud
) {
this.path = path;
this.databaseFile = databaseFile;
this.initial();
}

initial() {
if (!$file.exists(this.path)) $file.mkdir(this.path);
}

copyDatabaseToIcloud() {
this.initial()
if (!$file.exists(this.databaseFile)) return false;
return $file.copy({
src: constants.databaseFile,
dst: this.databaseFile
});
}

copyDatabaseFromIcloud() {
if (!$file.exists(this.databaseFile)) return false;
return $file.copy({
src: this.databaseFile,
dst: constants.databaseFile
});
}
}

const icloudManager = new IcloudManager();

module.exports = icloudManager;
15 changes: 15 additions & 0 deletions scripts/utils/settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
const constants = require("./constants");
const database = require("./database");
const icloudManager = require("./icloud");

function overwriteIcloudDatabase() {
database.closeDB();
icloudManager.copyDatabaseToIcloud();
database.openDB();
}

function overwriteLocalDatabase() {
database.closeDB();
icloudManager.copyDatabaseFromIcloud();
database.openDB();
}

function rebuildDatabase() {
$ui.alert({
Expand Down Expand Up @@ -44,6 +57,8 @@ function reset() {
}

module.exports = {
overwriteIcloudDatabase,
overwriteLocalDatabase,
rebuildDatabase,
reset
};
4 changes: 4 additions & 0 deletions strings/en.strings
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
"Next 3 Pages" = "Next 3 Pages";
"Next 4 Pages" = "Next 4 Pages";
"Slideshow Intervals" = "Slideshow Intervals";
"ICLOUD_AND_TIPS" = "iCloud\nWarning: Since iCloud can not sync instantly, data changes cannot be synchronized on other devices immediately, and operations on another device before synchronization is complete may cause file conflicts and lose some data";
"Sync Database" = "Sync Database";
"Overwrite Database in iCloud with Local Database" = "Overwrite Database in iCloud with Local Database";
"Overwrite Local Database with Database in iCloud" = "Overwrite Local Database with Database in iCloud";
"STORAGE" = "STORAGE";
"Clear Download Cache" = "Clear Download Cache";
"Rebuild Database" = "Rebuild Database";
Expand Down
4 changes: 4 additions & 0 deletions strings/zh-Hans.strings
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
"Next 3 Pages" = "提前三页";
"Next 4 Pages" = "提前四页";
"Slideshow Intervals" = "幻灯片浏览速度";
"ICLOUD_AND_TIPS" = "iCloud\n警告:由于iCloud并非即时同步,因此在其他设备上并不能立即同步数据更改,并且同步完成前其他设备上的操作可能会导致文件版本冲突并丢失一部分数据";
"Sync Database" = "同步数据库";
"Overwrite Database in iCloud with Local Database" = "用本地数据库覆盖iCloud中的数据库";
"Overwrite Local Database with Database in iCloud" = "用iCloud数据库覆盖本地数据库";
"STORAGE" = "存储";
"Clear Download Cache" = "清空下载缓存";
"Rebuild Database" = "重建数据库";
Expand Down

0 comments on commit acf1582

Please sign in to comment.