Skip to content

Commit

Permalink
neon_files: Fix uploading files for web
Browse files Browse the repository at this point in the history
  • Loading branch information
provokateurin committed Jun 10, 2023
1 parent bcb7134 commit f5c75e6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
19 changes: 19 additions & 0 deletions packages/neon/neon_files/lib/blocs/files.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ part of '../neon_files.dart';
abstract class FilesBlocEvents {
void uploadFile(final List<String> path, final String localPath);

void uploadBytes(final List<String> path, final Uint8List bytes);

void syncFile(final List<String> path);

void openFile(final List<String> path, final String etag, final String? mimeType);
Expand Down Expand Up @@ -178,6 +180,23 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
);
}

@override
void uploadBytes(final List<String> path, final Uint8List bytes) {
wrapAction(
() async {
final task = UploadTask(
path: path,
size: bytes.lengthInBytes,
lastModified: null,
);
uploadTasks.add(uploadTasks.value..add(task));
await _uploadQueue.add(() => task.execute(client, Stream.value(bytes)));
uploadTasks.add(uploadTasks.value..removeWhere((final t) => t == task));
},
disableTimeout: true,
);
}

Future _downloadFile(
final List<String> path,
final File file,
Expand Down
25 changes: 20 additions & 5 deletions packages/neon/neon_files/lib/dialogs/choose_create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,24 @@ class _FilesChooseCreateDialogState extends State<FilesChooseCreateDialog> {
);
if (result != null) {
for (final file in result.files) {
await upload(File(file.path!));
await upload(
file.name,
path: kIsWeb ? null : file.path,
bytes: file.bytes,
);
}
}
}

Future upload(final File file) async {
Future upload(
final String name, {
final String? path,
final Uint8List? bytes,
}) async {
assert((path == null) != (bytes == null), 'Provide either path or bytes');
final sizeWarning = widget.bloc.options.uploadSizeWarning.value;
if (sizeWarning != null) {
if (path != null && sizeWarning != null) {
final file = File(path);
final stat = file.statSync();
if (stat.size > sizeWarning) {
// ignore: use_build_context_synchronously
Expand All @@ -45,7 +55,12 @@ class _FilesChooseCreateDialogState extends State<FilesChooseCreateDialog> {
}
}
}
widget.bloc.uploadFile([...widget.basePath, p.basename(file.path)], file.path);

if (path != null) {
widget.bloc.uploadFile([...widget.basePath, name], path);
} else {
widget.bloc.uploadBytes([...widget.basePath, name], bytes!);
}
}

@override
Expand Down Expand Up @@ -92,7 +107,7 @@ class _FilesChooseCreateDialogState extends State<FilesChooseCreateDialog> {
final picker = ImagePicker();
final result = await picker.pickImage(source: ImageSource.camera);
if (result != null) {
await upload(File(result.path));
await upload(result.name, path: result.path);
}
},
),
Expand Down
2 changes: 1 addition & 1 deletion packages/neon/neon_files/lib/neon_files.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
library neon_files;

import 'dart:async';
import 'dart:typed_data';

import 'package:collection/collection.dart';
import 'package:file_icons/file_icons.dart';
import 'package:file_picker/file_picker.dart';
import 'package:filesize/filesize.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:intersperse/intersperse.dart';
Expand Down
2 changes: 1 addition & 1 deletion packages/neon/neon_files/lib/utils/upload_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UploadTask {

final List<String> path;
final int size;
final DateTime lastModified;
final DateTime? lastModified;

final _streamController = StreamController<int>();
late final progress = _streamController.stream.asBroadcastStream();
Expand Down

0 comments on commit f5c75e6

Please sign in to comment.