Files
pipelines/app/lib/features/chat/files/files_controller.dart
T
alan b8fb4d350c Upload files into the shown folder from the chat Files panel
The header gains an Upload button that uses the host file picker (disabled
with its reason where no picker exists), and the panel body becomes a drop
target: on web, document drag events are observed through a dart:js_interop
seam and a file drag over the panel shows a highlight naming the folder.
Each file is posted to POST /files?path=<folder> through an injectable
transport; refusals (exists, sign in, too large) stay visible until dismissed
and the folder reloads afterwards. The list/grid builders move to
files_entries.dart to keep the panel small.
2026-11-09 00:09:01 +00:00

170 lines
4.6 KiB
Dart

import 'package:flutter/foundation.dart';
import '../../../core/api_gateway.dart';
import '../../../core/transport/pipelines_client.dart';
import '../data/directory_listing.dart';
import '../domain/chat_attachment_picker.dart';
import 'files_models.dart';
import 'files_upload.dart';
class FilesController extends ChangeNotifier {
FilesController({required this.gateway, required this.initialPath});
final ApiGateway gateway;
String initialPath;
bool _started = false;
DirectoryListing? listing;
String? error;
bool loading = false;
bool showHidden = false;
String filter = '';
FilesSort sort = const FilesSort(FilesSortKey.name);
FilesViewMode view = FilesViewMode.list;
int _request = 0;
bool _disposed = false;
/// Files still being stored and the size of the batch they belong to.
int uploading = 0;
int uploadTotal = 0;
/// Why the last upload batch failed, until dismissed or a new batch starts.
String? uploadError;
bool get missingDirectory => initialPath.trim().isEmpty;
String get uploadLabel => uploadTotal <= 1
? 'Uploading…'
: 'Uploading ${uploadTotal - uploading + 1} of $uploadTotal…';
List<DirectoryEntry> get entries {
final current = listing;
if (current == null) return const [];
final needle = filter.trim().toLowerCase();
return sortFileEntries(
current.entries.where(
(entry) =>
(showHidden || !entry.hidden) &&
(needle.isEmpty || entry.name.toLowerCase().contains(needle)),
),
sort,
);
}
Future<void> start() {
_started = true;
return open(initialPath);
}
/// Session metadata can arrive after the retained conversation is mounted.
/// Keep a folder the operator navigated to; otherwise load the new root.
Future<void> updateInitialPath(String path) async {
if (_disposed || path.trim().isEmpty || path == initialPath) return;
final previous = initialPath;
initialPath = path;
if (_started && (listing == null || listing!.path == previous)) {
listing = null;
await open(path);
} else {
_notify();
}
}
Future<void> open(String? path) async {
if (path == null || path.trim().isEmpty) return;
final request = ++_request;
loading = true;
error = null;
_notify();
try {
final next = await listDirectory(gateway, path);
if (_disposed || request != _request) return;
listing = next;
filter = '';
} catch (e) {
if (_disposed || request != _request) return;
error = "Couldn't load this folder.";
} finally {
if (!_disposed && request == _request) {
loading = false;
_notify();
}
}
}
Future<void> retry() => open(listing?.path ?? initialPath);
/// Stores [files] in the shown folder, one request each, then reloads that
/// folder so the new entries appear. Each refusal is kept in [uploadError]
/// in the server's words; the remaining files are still attempted.
Future<void> upload(List<ChatPickedFile> files) async {
final directory = listing?.path;
if (files.isEmpty) return;
if (directory == null) {
reportUploadError('Open a folder before uploading files.');
return;
}
final endpoint = configuredGatewayEndpoint(gateway);
uploadTotal += files.length;
uploading += files.length;
uploadError = null;
_notify();
final failures = <String>[];
for (final file in files) {
try {
await uploadFileInto(endpoint, directory, file);
} on FilesUploadException catch (error) {
failures.add(error.message);
} finally {
uploading -= 1;
}
if (_disposed) return;
_notify();
}
if (uploading == 0) uploadTotal = 0;
if (failures.isNotEmpty) uploadError = failures.join('\n');
_notify();
// The operator may have moved on while the batch was in flight; only the
// folder that received the files is reloaded.
if (listing?.path == directory) await retry();
}
/// Shows a host-side upload failure (no picker, unreadable drop) in the
/// same place as a server refusal.
void reportUploadError(String message) {
uploadError = message;
_notify();
}
void dismissUploadError() {
uploadError = null;
_notify();
}
void toggleHidden(bool value) {
showHidden = value;
_notify();
}
void setFilter(String value) {
filter = value;
_notify();
}
void setSort(FilesSort value) {
sort = value;
_notify();
}
void setView(FilesViewMode value) {
view = value;
_notify();
}
void _notify() {
if (!_disposed) notifyListeners();
}
@override
void dispose() {
_disposed = true;
_request++;
super.dispose();
}
}