Skip to content

Commit

Permalink
feat(neon_files): Implement file syncing
Browse files Browse the repository at this point in the history
Signed-off-by: jld3103 <[email protected]>
  • Loading branch information
provokateurin committed Aug 29, 2023
1 parent 7aae612 commit d07e049
Show file tree
Hide file tree
Showing 12 changed files with 465 additions and 74 deletions.
8 changes: 8 additions & 0 deletions packages/app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
watcher:
dependency: transitive
description:
name: watcher
sha256: "3d2ad6751b3c16cf07c7fca317a1413b3f26530319181b37e3b9039b84fc01d8"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
web:
dependency: transitive
description:
Expand Down
65 changes: 21 additions & 44 deletions packages/neon/neon_files/lib/blocs/files.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ part of '../neon_files.dart';
abstract class FilesBlocEvents {
void uploadFile(final List<String> path, final String localPath);

void syncFile(final List<String> path);

void openFile(final List<String> path, final String etag, final String? mimeType);

void delete(final List<String> path);
Expand All @@ -29,8 +27,8 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
this.options,
this.account,
) {
options.uploadQueueParallelism.addListener(_uploadParalelismListener);
options.downloadQueueParallelism.addListener(_downloadParalelismListener);
options.uploadQueueParallelism.addListener(_uploadParallelismListener);
options.downloadQueueParallelism.addListener(_downloadParallelismListener);
}

final FilesAppSpecificOptions options;
Expand All @@ -46,13 +44,28 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
_downloadQueue.dispose();
unawaited(tasks.close());

options.uploadQueueParallelism.removeListener(_uploadParalelismListener);
options.downloadQueueParallelism.removeListener(_downloadParalelismListener);
options.uploadQueueParallelism.removeListener(_uploadParallelismListener);
options.downloadQueueParallelism.removeListener(_downloadParallelismListener);
}

@override
BehaviorSubject<List<FilesTask>> tasks = BehaviorSubject<List<FilesTask>>.seeded([]);

@override
Future refresh() async {
await browser.refresh();
}

@override
void removeFavorite(final List<String> path) {
wrapAction(
() async => account.client.webdav.proppatch(
path.join('/'),
set: WebDavProp(ocfavorite: 0),
),
);
}

@override
void addFavorite(final List<String> path) {
wrapAction(
Expand Down Expand Up @@ -100,21 +113,6 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
);
}

@override
Future refresh() async {
await browser.refresh();
}

@override
void removeFavorite(final List<String> path) {
wrapAction(
() async => account.client.webdav.proppatch(
path.join('/'),
set: WebDavProp(ocfavorite: 0),
),
);
}

@override
void rename(final List<String> path, final String name) {
wrapAction(
Expand All @@ -125,27 +123,6 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
);
}

@override
void syncFile(final List<String> path) {
wrapAction(
() async {
final file = File(
p.join(
await NeonPlatform.instance.userAccessibleAppDataPath,
account.humanReadableID,
'files',
path.join(Platform.pathSeparator),
),
);
if (!file.parent.existsSync()) {
file.parent.createSync(recursive: true);
}
await _downloadFile(path, file);
},
disableTimeout: true,
);
}

@override
void uploadFile(final List<String> path, final String localPath) {
wrapAction(
Expand Down Expand Up @@ -177,11 +154,11 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta

FilesBrowserBloc getNewFilesBrowserBloc() => FilesBrowserBloc(options, account);

void _downloadParalelismListener() {
void _downloadParallelismListener() {
_downloadQueue.parallel = options.downloadQueueParallelism.value;
}

void _uploadParalelismListener() {
void _uploadParallelismListener() {
_uploadQueue.parallel = options.uploadQueueParallelism.value;
}
}
3 changes: 1 addition & 2 deletions packages/neon/neon_files/lib/models/file_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class FileDetails {

FileDetails.fromWebDav({
required final WebDavFile file,
required final List<String> path,
}) : path = List.from(path)..add(file.name),
}) : path = file.path.split('/').where((final element) => element.isNotEmpty).toList(),
isDirectory = file.isDirectory,
size = file.size,
etag = file.etag,
Expand Down
9 changes: 9 additions & 0 deletions packages/neon/neon_files/lib/neon_files.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import 'package:neon/nextcloud.dart';
import 'package:neon/platform.dart';
import 'package:neon/settings.dart';
import 'package:neon/sort_box.dart';
import 'package:neon/sync.dart';
import 'package:neon/theme.dart';
import 'package:neon/utils.dart';
import 'package:neon/widgets.dart';
import 'package:neon_files/l10n/localizations.dart';
import 'package:neon_files/routes.dart';
import 'package:neon_files/sync/mapping.dart';
import 'package:neon_files/widgets/file_list_tile.dart';
import 'package:open_file/open_file.dart';
import 'package:path/path.dart' as p;
Expand All @@ -40,9 +43,12 @@ part 'options.dart';
part 'pages/details.dart';
part 'pages/main.dart';
part 'sort/files.dart';
part 'sync/implementation.dart';
part 'sync/sources.dart';
part 'utils/task.dart';
part 'widgets/browser_view.dart';
part 'widgets/file_preview.dart';
part 'widgets/file_tile.dart';

class FilesApp extends AppImplementation<FilesBloc, FilesAppSpecificOptions> {
FilesApp();
Expand All @@ -68,6 +74,9 @@ class FilesApp extends AppImplementation<FilesBloc, FilesAppSpecificOptions> {
@override
final Widget page = const FilesMainPage();

@override
FilesSync getSync() => FilesSync();

@override
final RouteBase route = $filesAppRoute;
}
82 changes: 82 additions & 0 deletions packages/neon/neon_files/lib/sync/implementation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
part of '../neon_files.dart';

class FilesSync implements SyncImplementation<FilesSyncMapping, WebDavFile, FileSystemEntity> {
@override
String appId = AppIDs.files;

@override
FilesSyncSources getSources(final Account account, final FilesSyncMapping mapping) => FilesSyncSources(
account.client,
mapping.remotePath,
mapping.localPath,
);

@override
Map<String, dynamic> serializeMapping(final FilesSyncMapping mapping) => mapping.toJson();

@override
FilesSyncMapping deserializeMapping(final Map<String, dynamic> json) => FilesSyncMapping.fromJson(json);

@override
Future<FilesSyncMapping?> addMapping(final BuildContext context, final Account account) async {
final accountsBloc = Provider.of<AccountsBloc>(context, listen: false);
final appsBloc = accountsBloc.getAppsBlocFor(account);
final filesBloc = appsBloc.getAppBlocByID(AppIDs.files)! as FilesBloc;
final filesBrowserBloc = filesBloc.getNewFilesBrowserBloc();

final remotePath = await showDialog<List<String>?>(
context: context,
builder: (final context) => FilesChooseFolderDialog(
bloc: filesBrowserBloc,
filesBloc: filesBloc,
originalPath: const [],
),
);
filesBrowserBloc.dispose();
if (remotePath == null) {
return null;
}

final localPath = await FileUtils.pickDirectory();
// ignore: use_build_context_synchronously
if (localPath == null || !context.mounted) {
return null;
}

return FilesSyncMapping(
appId: AppIDs.files,
accountId: account.id,
remotePath: remotePath,
localPath: localPath,
journal: SyncJournal({}),
);
}

@override
Widget getConflictDetailsLocal(final BuildContext context, final FileSystemEntity object) {
final stat = object.statSync();
return FilesFileTile(
showFullPath: true,
filesBloc: Provider.of<FilesBloc>(context, listen: false),
details: FileDetails(
path: object.path.split('/'),
isDirectory: object is Directory,
size: stat.size,
etag: '',
mimeType: '',
lastModified: stat.modified,
hasPreview: false,
isFavorite: false,
),
);
}

@override
Widget getConflictDetailsRemote(final BuildContext context, final WebDavFile object) => FilesFileTile(
showFullPath: true,
filesBloc: Provider.of<FilesBloc>(context, listen: false),
details: FileDetails.fromWebDav(
file: object,
),
);
}
64 changes: 64 additions & 0 deletions packages/neon/neon_files/lib/sync/mapping.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:neon/nextcloud.dart';
import 'package:neon/sync.dart';
import 'package:watcher/watcher.dart';

part 'mapping.g.dart';

@JsonSerializable()
class FilesSyncMapping implements SyncMapping<WebDavFile, FileSystemEntity> {
FilesSyncMapping({
required this.accountId,
required this.appId,
required this.journal,
required this.remotePath,
required this.localPath,
});

factory FilesSyncMapping.fromJson(final Map<String, dynamic> json) => _$FilesSyncMappingFromJson(json);
Map<String, dynamic> toJson() => _$FilesSyncMappingToJson(this);

@override
String accountId;

@override
String appId;

@override
SyncJournal journal;

final List<String> remotePath;

final String localPath;

@override
late String title = remotePath.join('/');

@override
late String subtitle = localPath;

@override
late String id = '${Uri.encodeComponent(remotePath.join('/'))}-${Uri.encodeComponent(localPath)}';

StreamSubscription<WatchEvent>? _subscription;

@override
void watch(final Function() onUpdated) {
debugPrint('Watching file changes: $localPath');
_subscription ??= DirectoryWatcher(localPath).events.listen(
(final event) {
debugPrint('Registered file change: ${event.path} ${event.type}');
onUpdated();
},
);
}

@override
void dispose() {
unawaited(_subscription?.cancel());
}
}
27 changes: 27 additions & 0 deletions packages/neon/neon_files/lib/sync/mapping.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d07e049

Please sign in to comment.