Skip to content

Commit

Permalink
S3 cloud;Auto backup
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-Stackflow committed Aug 18, 2024
1 parent 9483687 commit fdb644d
Show file tree
Hide file tree
Showing 71 changed files with 9,963 additions and 1,289 deletions.
18 changes: 18 additions & 0 deletions lib/Database/cloud_service_config_dao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class CloudServiceConfigDao {

static Future<int> insertConfig(CloudServiceConfig config) async {
final db = await DatabaseManager.getDataBase();
if (await getSpecifyConfig(config.type) != null) {
return -1;
}
config.id = await getMaxId() + 1;
config.createTimestamp = DateTime.now().millisecondsSinceEpoch;
config.editTimestamp = DateTime.now().millisecondsSinceEpoch;
Expand Down Expand Up @@ -38,6 +41,17 @@ class CloudServiceConfigDao {
});
}

static Future<List<CloudServiceConfig>> getValidConfigs() async {
List<CloudServiceConfig> configs = await getConfigs();
List<CloudServiceConfig> validConfigs = [];
for (CloudServiceConfig config in configs) {
if (config.enabled && (await config.isValid())) {
validConfigs.add(config);
}
}
return validConfigs;
}

static Future<int> updateLastBackupTime(CloudServiceConfig config) async {
final db = await DatabaseManager.getDataBase();
config.lastBackupTimestamp = DateTime.now().millisecondsSinceEpoch;
Expand Down Expand Up @@ -131,4 +145,8 @@ class CloudServiceConfigDao {
static Future<CloudServiceConfig?> getDropboxConfig() async {
return getSpecifyConfig(CloudServiceType.Dropbox);
}

static Future<CloudServiceConfig?> getS3CloudConfig() async {
return getSpecifyConfig(CloudServiceType.S3Cloud);
}
}
1 change: 1 addition & 0 deletions lib/Database/create_table_sql.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ enum Sql {
total_size INTEGER NOT NULL DEFAULT -1,
remaining_size INTEGER NOT NULL DEFAULT -1,
used_size INTEGER NOT NULL DEFAULT -1,
configured INTEGER NOT NULL DEFAULT 0,
email TEXT NOT NULL DEFAULT ''
);
'''),
Expand Down
28 changes: 20 additions & 8 deletions lib/Database/database_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import '../Utils/hive_util.dart';

class DatabaseManager {
static const _dbName = "cloudotp.db";
static const _dbVersion = 4;
static const _dbVersion = 5;
static Database? _database;
static final dbFactory = createDatabaseFactoryFfi(ffiInit: ffiInit);

Expand All @@ -33,6 +33,7 @@ class DatabaseManager {
String path = join(await FileUtil.getDatabaseDir(), _dbName);
if (!await dbFactory.databaseExists(path)) {
password = await HiveUtil.regeneratePassword();
print("Database not exist and new password is $password");
await HiveUtil.setEncryptDatabaseStatus(
EncryptDatabaseStatus.defaultPassword);
}
Expand Down Expand Up @@ -75,18 +76,29 @@ class DatabaseManager {
await db.execute(Sql.createAutoBackupLogTable.sql);
}

static Future<void> _onUpgrade(Database db, int oldVersion, int newVersion) async {
static Future<void> _onUpgrade(
Database db, int oldVersion, int newVersion) async {
if (oldVersion < 2) {
await db.execute("alter table otp_token add column description TEXT NOT NULL DEFAULT ''");
await db.execute("alter table cloud_service_config add column enabled INTEGER NOT NULL DEFAULT 1");
await db.execute(
"alter table otp_token add column description TEXT NOT NULL DEFAULT ''");
await db.execute(
"alter table cloud_service_config add column enabled INTEGER NOT NULL DEFAULT 1");
}
if (oldVersion < 3) {
await db.execute("alter table cloud_service_config add column total_size INTEGER NOT NULL DEFAULT -1");
await db.execute("alter table cloud_service_config add column remaining_size INTEGER NOT NULL DEFAULT -1");
await db.execute("alter table cloud_service_config add column used_size INTEGER NOT NULL DEFAULT -1");
await db.execute(
"alter table cloud_service_config add column total_size INTEGER NOT NULL DEFAULT -1");
await db.execute(
"alter table cloud_service_config add column remaining_size INTEGER NOT NULL DEFAULT -1");
await db.execute(
"alter table cloud_service_config add column used_size INTEGER NOT NULL DEFAULT -1");
}
if (oldVersion < 4) {
await db.execute("alter table cloud_service_config add column email TEXT NOT NULL DEFAULT ''");
await db.execute(
"alter table cloud_service_config add column email TEXT NOT NULL DEFAULT ''");
}
if (oldVersion < 5) {
await db.execute(
"alter table cloud_service_config add column configured INTEGER NOT NULL DEFAULT 0");
}
}

Expand Down
154 changes: 79 additions & 75 deletions lib/Models/auto_backup_log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,72 +36,6 @@ enum AutoBackupStatus {
this == AutoBackupStatus.encryptFailed;
}

String get labelShort {
switch (this) {
case AutoBackupStatus.pending:
return S.current.pendingBackupShort;
case AutoBackupStatus.encrypting:
return S.current.encryptingBackupFileShort;
case AutoBackupStatus.encryptFailed:
return S.current.encryptBackupFileFailedShort;
case AutoBackupStatus.encrpytSuccess:
return S.current.encryptBackupFileSuccessShort;
case AutoBackupStatus.saving:
return S.current.savingBackupFileShort;
case AutoBackupStatus.saveFailed:
return S.current.saveBackupFileFailedShort;
case AutoBackupStatus.saveSuccess:
return S.current.saveBackupFileSuccessShort;
case AutoBackupStatus.uploading:
return S.current.uploadingBackupFileShort;
case AutoBackupStatus.uploadFailed:
return S.current.uploadBackupFileFailedShort;
case AutoBackupStatus.uploadSuccess:
return S.current.uploadBackupFileSuccessShort;
case AutoBackupStatus.complete:
return S.current.autoBackupCompleteShort;
case AutoBackupStatus.failed:
return S.current.autoBackupFailedShort;
default:
return S.current.pendingBackupShort;
}
}

String label(AutoBackupLog log) {
switch (this) {
case AutoBackupStatus.pending:
return S.current.pendingBackup(log.type.label);
case AutoBackupStatus.encrypting:
return S.current.encryptingBackupFile;
case AutoBackupStatus.encryptFailed:
return S.current.encryptBackupFileFailed;
case AutoBackupStatus.encrpytSuccess:
return S.current.encryptBackupFileSuccess;
case AutoBackupStatus.saving:
return S.current.savingBackupFile;
case AutoBackupStatus.saveFailed:
return S.current.saveBackupFileFailed;
case AutoBackupStatus.saveSuccess:
return S.current.saveBackupFileSuccess(log.backupPath);
case AutoBackupStatus.uploading:
return S.current.uploadingBackupFile;
case AutoBackupStatus.uploadFailed:
return S.current.uploadBackupFileFailed;
case AutoBackupStatus.uploadSuccess:
if (log.cloudServiceType == null) {
return S.current.uploadBackupFileFailed;
} else {
return S.current.uploadBackupFileSuccess(log.cloudServiceType!.label);
}
case AutoBackupStatus.complete:
return S.current.autoBackupComplete;
case AutoBackupStatus.failed:
return S.current.autoBackupFailed;
default:
return S.current.pendingBackup(log.type.label);
}
}

Color get color {
switch (this) {
case AutoBackupStatus.pending:
Expand Down Expand Up @@ -217,7 +151,6 @@ class AutoBackupLog {
List<AutoBackupLogStatusItem> status;
AutoBackupTriggerType triggerType;
String backupPath;
CloudServiceType? cloudServiceType;

AutoBackupLogStatusItem get lastStatusItem {
return status.last;
Expand All @@ -234,14 +167,12 @@ class AutoBackupLog {
required this.status,
required this.type,
required this.backupPath,
required this.cloudServiceType,
this.triggerType = AutoBackupTriggerType.manual,
});

AutoBackupLog.init({
required this.type,
required this.triggerType,
this.cloudServiceType,
}) : id = 0,
startTimestamp = DateTime.now().millisecondsSinceEpoch,
endTimestamp = 0,
Expand All @@ -260,12 +191,13 @@ class AutoBackupLog {

addStatus(
AutoBackupStatus status, {
String? remark,
CloudServiceType? type,
}) {
this.status.add(AutoBackupLogStatusItem(
status: status,
timestamp: DateTime.now().millisecondsSinceEpoch,
remark: remark ?? "",
cloudServiceType: type,
remark: '',
));
switch (status) {
case AutoBackupStatus.encrypting:
Expand Down Expand Up @@ -302,9 +234,6 @@ class AutoBackupLog {
type: AutoBackupType.values[map['type']],
triggerType: AutoBackupTriggerType.values[map['trigger_type']],
backupPath: map['backup_path'],
cloudServiceType: map['cloud_service_type'] == null
? null
: CloudServiceType.values[map['cloud_service_type']],
);
}

Expand All @@ -319,7 +248,6 @@ class AutoBackupLog {
'type': type.index,
'trigger_type': triggerType.index,
'backup_path': backupPath,
'cloud_service_type': cloudServiceType?.index,
};
}
}
Expand All @@ -328,18 +256,23 @@ class AutoBackupLogStatusItem {
final AutoBackupStatus status;
final int timestamp;
final String remark;
final CloudServiceType? cloudServiceType;

AutoBackupLogStatusItem({
required this.status,
required this.timestamp,
required this.remark,
this.cloudServiceType,
});

factory AutoBackupLogStatusItem.fromMap(Map<String, dynamic> map) {
return AutoBackupLogStatusItem(
status: AutoBackupStatus.values[map['status']],
timestamp: map['timestamp'],
remark: map['remark'],
cloudServiceType: map['cloud_service_type'] == null
? null
: CloudServiceType.values[map['cloud_service_type']],
);
}

Expand All @@ -348,6 +281,77 @@ class AutoBackupLogStatusItem {
'status': status.index,
'timestamp': timestamp,
'remark': remark,
'cloud_service_type': cloudServiceType?.index,
};
}

String get labelShort {
switch (status) {
case AutoBackupStatus.pending:
return S.current.pendingBackupShort;
case AutoBackupStatus.encrypting:
return S.current.encryptingBackupFileShort;
case AutoBackupStatus.encryptFailed:
return S.current.encryptBackupFileFailedShort;
case AutoBackupStatus.encrpytSuccess:
return S.current.encryptBackupFileSuccessShort;
case AutoBackupStatus.saving:
return S.current.savingBackupFileShort;
case AutoBackupStatus.saveFailed:
return S.current.saveBackupFileFailedShort;
case AutoBackupStatus.saveSuccess:
return S.current.saveBackupFileSuccessShort;
case AutoBackupStatus.uploading:
return S.current.uploadingBackupFileShort;
case AutoBackupStatus.uploadFailed:
return S.current.uploadBackupFileFailedShort;
case AutoBackupStatus.uploadSuccess:
return S.current.uploadBackupFileSuccessShort;
case AutoBackupStatus.complete:
return S.current.autoBackupCompleteShort;
case AutoBackupStatus.failed:
return S.current.autoBackupFailedShort;
default:
return S.current.pendingBackupShort;
}
}

String label(AutoBackupLog log) {
switch (status) {
case AutoBackupStatus.pending:
return S.current.pendingBackup(log.type.label);
case AutoBackupStatus.encrypting:
return S.current.encryptingBackupFile;
case AutoBackupStatus.encryptFailed:
return S.current.encryptBackupFileFailed;
case AutoBackupStatus.encrpytSuccess:
return S.current.encryptBackupFileSuccess;
case AutoBackupStatus.saving:
return S.current.savingBackupFile;
case AutoBackupStatus.saveFailed:
return S.current.saveBackupFileFailed;
case AutoBackupStatus.saveSuccess:
return S.current.saveBackupFileSuccess(log.backupPath);
case AutoBackupStatus.uploading:
if (cloudServiceType == null) {
return S.current.uploadBackupFileFailed;
} else {
return S.current.uploadingBackupFileTo(cloudServiceType!.label);
}
case AutoBackupStatus.uploadFailed:
return S.current.uploadBackupFileFailed;
case AutoBackupStatus.uploadSuccess:
if (cloudServiceType == null) {
return S.current.uploadBackupFileFailed;
} else {
return S.current.uploadBackupFileSuccess(cloudServiceType!.label);
}
case AutoBackupStatus.complete:
return S.current.autoBackupComplete;
case AutoBackupStatus.failed:
return S.current.autoBackupFailed;
default:
return S.current.pendingBackup(log.type.label);
}
}
}
Loading

0 comments on commit fdb644d

Please sign in to comment.