Skip to content

Commit

Permalink
fix: sync files checking by hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Sivan22 committed Nov 10, 2024
1 parent 031d5ea commit 422001d
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 43 deletions.
29 changes: 14 additions & 15 deletions lib/screens/reading/pdf/pdf_book_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import '../../../widgets/password_dialog.dart';
import 'pdf_thumbnails_screen.dart';
import 'package:otzaria/models/tabs.dart';
import 'package:printing/printing.dart';
import 'package:otzaria/utils/page_converter.dart';

class PdfBookViewr extends StatefulWidget {
final PdfBookTab tab;
Expand Down Expand Up @@ -70,22 +71,20 @@ class _PdfBookViewrState extends State<PdfBookViewr>
}
return IconButton(
onPressed: () async {
//find the latest outline
final outlines = await widget
.tab.pdfViewerController.document
.loadOutline();
final outline = outlines[0].children.firstWhere(
(element) =>
element.dest?.pageNumber ==
(widget.tab.pdfViewerController.pageNumber ??
0),
);
final appModel = context.read<AppModel>();
final textBook = await appModel.library.then((library) =>
library.findBookByTitle(widget.tab.title, TextBook));

openTextBookFromRef(
widget.tab.title,
outline.title,
context,
);
if (textBook != null) {
final currentPage =
widget.tab.pdfViewerController.pageNumber ?? 0;
final textIndex = await pdfToTextPage(
widget.tab.title, currentPage, context);

if (textIndex != null) {
appModel.openBook(textBook, textIndex);
}
}
},
icon: const Icon(
Icons.text_snippet,
Expand Down
22 changes: 13 additions & 9 deletions lib/screens/reading/text/text_book_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:otzaria/screens/reading/text/combined_book_screen.dart';
import 'package:otzaria/screens/printing_screen.dart';
import 'package:otzaria/screens/reading/text/splited_view_screen.dart';
import 'package:otzaria/utils/daf_yomi_helper.dart';
import 'package:otzaria/utils/page_converter.dart';
import 'package:provider/provider.dart';
import 'package:otzaria/screens/reading/text/text_book_search_screen.dart';
import 'dart:io';
Expand Down Expand Up @@ -104,16 +105,19 @@ class _TextBookViewerState extends State<TextBookViewer>
icon: const Icon(Icons.picture_as_pdf),
tooltip: 'פתח ספר במהדורה מודפסת ',
onPressed: () async {
openPdfBookFromRef(
widget.tab.book.title,
await utils.refFromIndex(
widget.tab.positionsListener.itemPositions
.value.isNotEmpty
? widget.tab.positionsListener
.itemPositions.value.first.index
: 0,
widget.tab.book.tableOfContents),
final appModel = context.read<AppModel>();
final book = await appModel.library.then((library) =>
library.findBookByTitle(
widget.tab.title, PdfBook));
final index = await textToPdfPage(
widget.tab.title,
widget.tab.positionsListener.itemPositions.value
.isNotEmpty
? widget.tab.positionsListener.itemPositions
.value.first.index
: 0,
context);
appModel.openBook(book!, index ?? 0);
})
: SizedBox.shrink()),

Expand Down
72 changes: 55 additions & 17 deletions lib/utils/file_sync_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ class FileSyncService {
final String repositoryName;
final String branch;
bool isSyncing = false;
int _currentProgress = 0;
int _totalFiles = 0;

FileSyncService({
required this.githubOwner,
required this.repositoryName,
this.branch = 'main',
});

int get currentProgress => _currentProgress;
int get totalFiles => _totalFiles;

Future<String> get _localManifestPath async {
final directory = _localDirectory;
return '${await directory}${Platform.pathSeparator}files_manifest.json';
Expand All @@ -30,7 +35,7 @@ class FileSyncService {
if (!await file.exists()) {
return {};
}
final content = await file.readAsString();
final content = await file.readAsString(encoding: utf8);
return json.decode(content);
} catch (e) {
print('Error reading local manifest: $e');
Expand All @@ -42,9 +47,16 @@ class FileSyncService {
final url =
'https://raw.githubusercontent.com/$githubOwner/$repositoryName/$branch/files_manifest.json';
try {
final response = await http.get(Uri.parse(url));
final response = await http.get(
Uri.parse(url),
headers: {
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
},
);
if (response.statusCode == 200) {
return json.decode(response.body);
// Explicitly decode as UTF-8
return json.decode(utf8.decode(response.bodyBytes));
}
throw Exception('Failed to fetch remote manifest');
} catch (e) {
Expand All @@ -57,14 +69,29 @@ class FileSyncService {
final url =
'https://raw.githubusercontent.com/$githubOwner/$repositoryName/$branch/$filePath';
try {
final response = await http.get(Uri.parse(url));
final response = await http.get(
Uri.parse(url),
headers: {
'Accept-Charset': 'utf-8',
},
);
if (response.statusCode == 200) {
final directory = await _localDirectory;
final file = File('$directory/$filePath');

// Create directories if they don't exist
await file.parent.create(recursive: true);
await file.writeAsBytes(response.bodyBytes);

// For text files, handle UTF-8 encoding explicitly
if (filePath.endsWith('.txt') ||
filePath.endsWith('.json') ||
filePath.endsWith('.csv')) {
await file.writeAsString(utf8.decode(response.bodyBytes),
encoding: utf8);
} else {
// For binary files, write bytes directly
await file.writeAsBytes(response.bodyBytes);
}
}
} catch (e) {
print('Error downloading file $filePath: $e');
Expand All @@ -80,30 +107,36 @@ class FileSyncService {
// Update the manifest for this specific file
localManifest[filePath] = fileInfo;

// Write the updated manifest back to disk
await manifestFile.writeAsString(json.encode(localManifest));
// Write the updated manifest back to disk with UTF-8 encoding
await manifestFile.writeAsString(
json.encode(localManifest),
encoding: utf8,
);
} catch (e) {
print('Error updating local manifest for file $filePath: $e');
}
}

Future<void> _removeFromLocalManifest(String filePath) async {
try {
// Try to remove the actual file if it exists
final directory = await _localDirectory;
final file = File('$directory/$filePath');
if (await file.exists()) {
await file.delete();
}
//if successful, remove from manifest
final manifestFile = File(await _localManifestPath);
Map<String, dynamic> localManifest = await _getLocalManifest();

// Remove the file from the manifest
localManifest.remove(filePath);

// Write the updated manifest back to disk
await manifestFile.writeAsString(json.encode(localManifest));

// Also remove the actual file if it exists
final directory = await _localDirectory;
final file = File('$directory/$filePath');
if (await file.exists()) {
await file.delete();
}
// Write the updated manifest back to disk with UTF-8 encoding
await manifestFile.writeAsString(
json.encode(localManifest),
encoding: utf8,
);
} catch (e) {
print('Error removing file $filePath from local manifest: $e');
}
Expand All @@ -117,7 +150,7 @@ class FileSyncService {

remoteManifest.forEach((filePath, remoteInfo) {
if (!localManifest.containsKey(filePath) ||
localManifest[filePath]['modified'] != remoteInfo['modified']) {
localManifest[filePath]['hash'] != remoteInfo['hash']) {
filesToUpdate.add(filePath);
}
});
Expand All @@ -131,12 +164,15 @@ class FileSyncService {
}
isSyncing = true;
int count = 0;
_currentProgress = 0;

try {
final remoteManifest = await _getRemoteManifest();
final localManifest = await _getLocalManifest();

// Find files to update or add
final filesToUpdate = await checkForUpdates();
_totalFiles = filesToUpdate.length;

// Download and update manifest for each file individually
for (final filePath in filesToUpdate) {
Expand All @@ -146,6 +182,7 @@ class FileSyncService {
await downloadFile(filePath);
await _updateLocalManifestForFile(filePath, remoteManifest[filePath]);
count++;
_currentProgress = count;
}

// Remove files that exist locally but not in remote
Expand All @@ -156,6 +193,7 @@ class FileSyncService {
if (!remoteManifest.containsKey(localFilePath)) {
await _removeFromLocalManifest(localFilePath);
count++;
_currentProgress = count;
}
}
} catch (e) {
Expand Down
Loading

0 comments on commit 422001d

Please sign in to comment.