Skip to content

Commit

Permalink
fix(restore): fix handling of missing backup files during restoration
Browse files Browse the repository at this point in the history
  • Loading branch information
gokadzev committed Nov 20, 2024
1 parent e9e4e11 commit 00842be
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/localization/app_ar.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "تم مسح ذاكرة التخزين المؤقت",
"cancel": "إلغاء",
"chooseBackupDir": "اختر دليل النسخ الاحتياطي",
"chooseRestoreDir": "اختر مسار الاستعادة",
"chooseBackupFiles": "Choose backup files",
"clear": "مسح",
"clearCache": "مسح ذاكرة التخزين المؤقت",
"clearRecentlyPlayed": "Clear recently played history",
Expand Down
2 changes: 1 addition & 1 deletion lib/localization/app_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "Cache gelöscht",
"cancel": "Abbrechen",
"chooseBackupDir": "Backup-Verzeichnis auswählen",
"chooseRestoreDir": "Verzeichnis zur Wiederherstellung auswählen",
"chooseBackupFiles": "Choose backup files",
"clear": "Löschen",
"clearCache": "Cache leeren",
"clearRecentlyPlayed": "Wiedergabeverlauf löschen",
Expand Down
2 changes: 1 addition & 1 deletion lib/localization/app_el.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "Έγινε εκκαθάριση της cache",
"cancel": "Ακύρωση",
"chooseBackupDir": "Επιλογή καταλόγου αντιγράφων ασφαλείας",
"chooseRestoreDir": "Επιλογή καταλόγου επαναφοράς αρχείων",
"chooseBackupFiles": "Choose backup files",
"clear": "Απαλοιφή",
"clearCache": "Απαλοιφή cache",
"clearRecentlyPlayed": "Καθαρισμός πρόσφατου ιστορικού αναπαραγωγής",
Expand Down
2 changes: 1 addition & 1 deletion lib/localization/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "Cache cleared",
"cancel": "Cancel",
"chooseBackupDir": "Choose backup folder",
"chooseRestoreDir": "Choose restore folder",
"chooseBackupFiles": "Choose backup files",
"clear": "Clear",
"clearCache": "Clear cache",
"clearRecentlyPlayed": "Clear recently played",
Expand Down
2 changes: 1 addition & 1 deletion lib/localization/app_es.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "Caché eliminada",
"cancel": "Cancelar",
"chooseBackupDir": "Seleccionar carpeta de respaldo",
"chooseRestoreDir": "Seleccionar carpeta de restauración",
"chooseBackupFiles": "Choose backup files",
"clear": "Borrar",
"clearCache": "Borrar caché",
"clearRecentlyPlayed": "Borrar reproducido recientemente",
Expand Down
2 changes: 1 addition & 1 deletion lib/localization/app_fr.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "Cache vidé",
"cancel": "Annuler",
"chooseBackupDir": "Choisir un dossier de sauvegarde",
"chooseRestoreDir": "Choisir un dossier de restauration",
"chooseBackupFiles": "Choose backup files",
"clear": "Effacer",
"clearCache": "Vider le cache",
"clearRecentlyPlayed": "Clear recently played history",
Expand Down
2 changes: 1 addition & 1 deletion lib/localization/app_id.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "Cache berhasil dihapus",
"cancel": "Batal",
"chooseBackupDir": "Pilih direktori backup",
"chooseRestoreDir": "Pilih direktori restore",
"chooseBackupFiles": "Choose backup files",
"clear": "Hapus",
"clearCache": "Hapus cache",
"clearRecentlyPlayed": "Hapus riwayat terputar",
Expand Down
2 changes: 1 addition & 1 deletion lib/localization/app_it.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "Cache pulita con successo",
"cancel": "Annulla",
"chooseBackupDir": "Scegli directory di backup",
"chooseRestoreDir": "Scegli directory di ripristino",
"chooseBackupFiles": "Choose backup files",
"clear": "Pulisci",
"clearCache": "Pulisci cache",
"clearRecentlyPlayed": "Cancella cronologia recenti",
Expand Down
2 changes: 1 addition & 1 deletion lib/localization/app_ja.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "キャッシュを消去しました",
"cancel": "キャンセル",
"chooseBackupDir": "バックアップ先のフォルダを選択",
"chooseRestoreDir": "復元元のフォルダを選択",
"chooseBackupFiles": "Choose backup files",
"clear": "消去",
"clearCache": "キャッシュを削除",
"clearRecentlyPlayed": "再生履歴を削除",
Expand Down
2 changes: 1 addition & 1 deletion lib/localization/app_ka.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "ნარჩენები გასუფთავდა",
"cancel": "გაუქმება",
"chooseBackupDir": "აირჩიეთ რეზერვის საქაღალდე",
"chooseRestoreDir": "აირჩიეთ რეზერვის დაბრუნების საქაღალდე",
"chooseBackupFiles": "Choose backup files",
"clear": "გასუფთავება",
"clearCache": "ნარჩენების გასუფთავება",
"clearRecentlyPlayed": "ბოლოს ჩართული სიმღერების გასუფთავება",
Expand Down
4 changes: 2 additions & 2 deletions lib/localization/app_ko.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "캐시가 성공적으로 지워졌음",
"cancel": "취소",
"chooseBackupDir": "백업 디렉토리 선택",
"chooseRestoreDir": "복원 디렉토리 선택",
"chooseBackupFiles": "Choose backup files",
"clear": "지우기",
"clearCache": "캐시 지우기",
"clearRecentlyPlayed": "최근 재생 기록 지우기",
Expand Down Expand Up @@ -90,4 +90,4 @@
"useSquigglySlider": "굴곡 슬라이더",
"userPlaylists": "사용자 재생목록",
"youtubePlaylistLinkOrId": "유튜브 재생목록 링크 또는 ID "
}
}
2 changes: 1 addition & 1 deletion lib/localization/app_pl.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "Pamięć wyczyszczona",
"cancel": "Anuluj",
"chooseBackupDir": "Wybierz ścieżkę do pliku kopii",
"chooseRestoreDir": "Wybierz ścieżkę do pliku kopii",
"chooseBackupFiles": "Choose backup files",
"clear": "Wyczyść",
"clearCache": "Wyczyść pamięć podręczną",
"clearRecentlyPlayed": "Wyczyść historię ostatnio odtworzonych utworów",
Expand Down
2 changes: 1 addition & 1 deletion lib/localization/app_pt.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "Cache limpo com sucesso",
"cancel": "Cancelar",
"chooseBackupDir": "Escolha o diretório para salvar o backup",
"chooseRestoreDir": "Escolha o diretório para restaurar o backup",
"chooseBackupFiles": "Choose backup files",
"clear": "Limpar",
"clearCache": "Limpar cache",
"clearRecentlyPlayed": "Limpar histórico de reproduções recentes",
Expand Down
4 changes: 2 additions & 2 deletions lib/localization/app_ru.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "Кэш очищен",
"cancel": "Отмена",
"chooseBackupDir": "Выберите каталог для сохранения",
"chooseRestoreDir": "Выберите каталог для восстановления данных",
"chooseBackupFiles": "Choose backup files",
"clear": "Очистить",
"clearCache": "Очистить кэш",
"clearRecentlyPlayed": "Очистить недавние треки",
Expand Down Expand Up @@ -90,4 +90,4 @@
"useSquigglySlider": "Волнистая полоса прокрутки",
"userPlaylists": "Ваши плейлисты",
"youtubePlaylistLinkOrId": "ID плейлиста YouTube или ссылка на него "
}
}
2 changes: 1 addition & 1 deletion lib/localization/app_tr.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "Önbellek temizlendi",
"cancel": "İptal",
"chooseBackupDir": "Yedekleme Dizini Seç",
"chooseRestoreDir": "Geri Yükleme Dizini Seç",
"chooseBackupFiles": "Choose backup files",
"clear": "Temizle",
"clearCache": "Önbelleği Temizle",
"clearRecentlyPlayed": "Yakında Oynatılanları Temizle",
Expand Down
2 changes: 1 addition & 1 deletion lib/localization/app_uk.arb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cacheMsg": "Кеш очищений",
"cancel": "Скасувати",
"chooseBackupDir": "Виберіть каталог для резервного копіювання",
"chooseRestoreDir": "Виберіть каталог для відновлення файлів",
"chooseBackupFiles": "Choose backup files",
"clear": "Очистити",
"clearCache": "Очистити кеш",
"clearRecentlyPlayed": "Clear recently played history",
Expand Down
42 changes: 34 additions & 8 deletions lib/services/data_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:musify/extensions/l10n.dart';
import 'package:musify/main.dart';

void addOrUpdateData(String category, String key, dynamic value) async {
final _box = await _openBox(category);
Expand Down Expand Up @@ -108,22 +109,47 @@ Future<String> backupData(BuildContext context) async {

Future<String> restoreData(BuildContext context) async {
final boxNames = ['user', 'settings'];
final uplPath = await FilePicker.platform.getDirectoryPath();
final backupFiles = await FilePicker.platform.pickFiles(
allowMultiple: true,
);

if (uplPath == null) {
return '${context.l10n!.chooseRestoreDir}!';
if (backupFiles == null || backupFiles.files.isEmpty) {
return '${context.l10n!.chooseBackupFiles}!';
}

try {
for (final boxName in boxNames) {
final sourceFile = File('$uplPath/$boxName.hive');
final box = await _openBox(boxName);

final boxPath = box.path;
await sourceFile.copy(boxPath!);
final _file = backupFiles.files.firstWhere(
(file) => file.name == '$boxName.hive',
orElse: () => PlatformFile(
name: '',
size: 0,
), // Create a PlatformFile with null path if not found
);

if (_file.path != null && _file.path!.isNotEmpty && _file.size != 0) {
final sourceFilePath = _file.path!;
final sourceFile = File(sourceFilePath);

final box = await _openBox(boxName);
final boxPath = box.path;
await box.close();

if (boxPath != null) {
await sourceFile.copy(boxPath);
}
} else {
logger.log(
'Source file for $boxName not found while restoring data.',
null,
null,
);
}
}

return '${context.l10n!.restoredSuccess}!';
} catch (e, stackTrace) {
logger.log('${context.l10n!.restoreError}:', e, stackTrace);
return '${context.l10n!.restoreError}: $e\n$stackTrace';
}
}

0 comments on commit 00842be

Please sign in to comment.