🐛 Fix web file upload
This commit is contained in:
@@ -147,7 +147,17 @@ void main() async {
|
|||||||
|
|
||||||
runApp(
|
runApp(
|
||||||
ProviderScope(
|
ProviderScope(
|
||||||
observers: [TalkerRiverpodObserver(talker: talker)],
|
observers: [
|
||||||
|
TalkerRiverpodObserver(
|
||||||
|
talker: talker,
|
||||||
|
settings: TalkerRiverpodLoggerSettings(
|
||||||
|
printProviderAdded: false,
|
||||||
|
printProviderDisposed: false,
|
||||||
|
printProviderUpdated: false,
|
||||||
|
printStateFullData: false,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
overrides: [sharedPreferencesProvider.overrideWithValue(prefs)],
|
overrides: [sharedPreferencesProvider.overrideWithValue(prefs)],
|
||||||
child: Directionality(
|
child: Directionality(
|
||||||
textDirection: TextDirection.ltr,
|
textDirection: TextDirection.ltr,
|
||||||
|
@@ -106,6 +106,7 @@ final apiClientProvider = Provider<Dio>((ref) {
|
|||||||
printResponseHeaders: false,
|
printResponseHeaders: false,
|
||||||
printResponseMessage: false,
|
printResponseMessage: false,
|
||||||
printRequestData: false,
|
printRequestData: false,
|
||||||
|
printResponseData: false,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io';
|
|
||||||
import 'dart:ui';
|
import 'dart:ui';
|
||||||
import 'package:croppy/croppy.dart';
|
import 'package:croppy/croppy.dart';
|
||||||
import 'package:cross_file/cross_file.dart';
|
import 'package:cross_file/cross_file.dart';
|
||||||
@@ -9,7 +8,6 @@ import 'package:flutter/widgets.dart';
|
|||||||
import 'package:island/models/file.dart';
|
import 'package:island/models/file.dart';
|
||||||
import 'package:island/services/file_uploader.dart';
|
import 'package:island/services/file_uploader.dart';
|
||||||
import 'package:native_exif/native_exif.dart';
|
import 'package:native_exif/native_exif.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
|
||||||
|
|
||||||
enum FileUploadMode { generic, mediaSafe }
|
enum FileUploadMode { generic, mediaSafe }
|
||||||
|
|
||||||
@@ -21,8 +19,7 @@ Future<XFile?> cropImage(
|
|||||||
}) async {
|
}) async {
|
||||||
final result = await showMaterialImageCropper(
|
final result = await showMaterialImageCropper(
|
||||||
context,
|
context,
|
||||||
imageProvider:
|
imageProvider: MemoryImage(await image.readAsBytes()),
|
||||||
kIsWeb ? NetworkImage(image.path) : FileImage(File(image.path)),
|
|
||||||
showLoadingIndicatorOnSubmit: true,
|
showLoadingIndicatorOnSubmit: true,
|
||||||
allowedAspectRatios: allowedAspectRatios,
|
allowedAspectRatios: allowedAspectRatios,
|
||||||
);
|
);
|
||||||
@@ -64,7 +61,10 @@ Completer<SnCloudFile?> putFileToCloud({
|
|||||||
fileData.isOnDevice &&
|
fileData.isOnDevice &&
|
||||||
fileData.type == UniversalFileType.image) {
|
fileData.type == UniversalFileType.image) {
|
||||||
final data = fileData.data;
|
final data = fileData.data;
|
||||||
if (data is XFile && !kIsWeb && (Platform.isIOS || Platform.isAndroid)) {
|
if (data is XFile &&
|
||||||
|
!kIsWeb &&
|
||||||
|
(defaultTargetPlatform == TargetPlatform.iOS ||
|
||||||
|
defaultTargetPlatform == TargetPlatform.android)) {
|
||||||
Exif.fromPath(data.path)
|
Exif.fromPath(data.path)
|
||||||
.then((exif) async {
|
.then((exif) async {
|
||||||
final gpsAttributes = {
|
final gpsAttributes = {
|
||||||
@@ -184,15 +184,11 @@ Completer<SnCloudFile?> _processUpload(
|
|||||||
|
|
||||||
final uploader = FileUploader(dio);
|
final uploader = FileUploader(dio);
|
||||||
|
|
||||||
// Get File object
|
|
||||||
File fileObj;
|
|
||||||
if (file.path.isNotEmpty) {
|
|
||||||
fileObj = File(file.path);
|
|
||||||
// Call progress start
|
// Call progress start
|
||||||
onProgress?.call(0.0, Duration.zero);
|
onProgress?.call(0.0, Duration.zero);
|
||||||
uploader
|
uploader
|
||||||
.uploadFile(
|
.uploadFile(
|
||||||
file: fileObj,
|
file: file,
|
||||||
fileName: actualFilename,
|
fileName: actualFilename,
|
||||||
contentType: actualMimetype,
|
contentType: actualMimetype,
|
||||||
poolId: poolId,
|
poolId: poolId,
|
||||||
@@ -206,45 +202,6 @@ Completer<SnCloudFile?> _processUpload(
|
|||||||
completer.completeError(e);
|
completer.completeError(e);
|
||||||
throw e;
|
throw e;
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
// Write to temp file
|
|
||||||
getTemporaryDirectory()
|
|
||||||
.then((tempDir) {
|
|
||||||
final tempFile = File('${tempDir.path}/temp_upload_$actualFilename');
|
|
||||||
file
|
|
||||||
.readAsBytes()
|
|
||||||
.then((bytes) => tempFile.writeAsBytes(bytes))
|
|
||||||
.then((_) {
|
|
||||||
fileObj = tempFile;
|
|
||||||
// Call progress start
|
|
||||||
onProgress?.call(0.0, Duration.zero);
|
|
||||||
uploader
|
|
||||||
.uploadFile(
|
|
||||||
file: fileObj,
|
|
||||||
fileName: actualFilename,
|
|
||||||
contentType: actualMimetype,
|
|
||||||
poolId: poolId,
|
|
||||||
)
|
|
||||||
.then((result) {
|
|
||||||
// Call progress end
|
|
||||||
onProgress?.call(1.0, Duration.zero);
|
|
||||||
completer.complete(result);
|
|
||||||
})
|
|
||||||
.catchError((e) {
|
|
||||||
completer.completeError(e);
|
|
||||||
throw e;
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catchError((e) {
|
|
||||||
completer.completeError(e);
|
|
||||||
throw e;
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catchError((e) {
|
|
||||||
completer.completeError(e);
|
|
||||||
throw e;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return completer;
|
return completer;
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io';
|
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:cross_file/cross_file.dart';
|
||||||
import 'package:crypto/crypto.dart';
|
import 'package:crypto/crypto.dart';
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
@@ -14,7 +14,7 @@ class FileUploader {
|
|||||||
FileUploader(this._dio);
|
FileUploader(this._dio);
|
||||||
|
|
||||||
/// Calculates the MD5 hash of a file.
|
/// Calculates the MD5 hash of a file.
|
||||||
Future<String> _calculateFileHash(File file) async {
|
Future<String> _calculateFileHash(XFile file) async {
|
||||||
final bytes = await file.readAsBytes();
|
final bytes = await file.readAsBytes();
|
||||||
final digest = md5.convert(bytes);
|
final digest = md5.convert(bytes);
|
||||||
return digest.toString();
|
return digest.toString();
|
||||||
@@ -22,7 +22,7 @@ class FileUploader {
|
|||||||
|
|
||||||
/// Creates an upload task for the given file.
|
/// Creates an upload task for the given file.
|
||||||
Future<Map<String, dynamic>> createUploadTask({
|
Future<Map<String, dynamic>> createUploadTask({
|
||||||
required File file,
|
required XFile file,
|
||||||
required String fileName,
|
required String fileName,
|
||||||
required String contentType,
|
required String contentType,
|
||||||
String? poolId,
|
String? poolId,
|
||||||
@@ -80,7 +80,7 @@ class FileUploader {
|
|||||||
|
|
||||||
/// Uploads a file in chunks using the multi-part API.
|
/// Uploads a file in chunks using the multi-part API.
|
||||||
Future<SnCloudFile> uploadFile({
|
Future<SnCloudFile> uploadFile({
|
||||||
required File file,
|
required XFile file,
|
||||||
required String fileName,
|
required String fileName,
|
||||||
required String contentType,
|
required String contentType,
|
||||||
String? poolId,
|
String? poolId,
|
||||||
|
Reference in New Issue
Block a user