Skip to content

Commit

Permalink
fix(file-service): provide required size to pick-upload functions (#94)
Browse files Browse the repository at this point in the history
* fix(file-service): provide required size to pick upload functions

* fix(file-service): throw error when file size exceeds

* fix(file-service): test commit

* fix(file-service): document things

* fix(file-service): document more things
  • Loading branch information
Khushboo1702 authored Jul 4, 2024
1 parent 9d9386c commit 267cb09
Showing 1 changed file with 43 additions and 17 deletions.
60 changes: 43 additions & 17 deletions lib/src/modules/common/file_upload/file_uploader.dart
Original file line number Diff line number Diff line change
@@ -1,44 +1,70 @@
import 'package:fa_flutter_core/fa_flutter_core.dart';
import 'package:image_picker/image_picker.dart';
import 'package:file_picker/file_picker.dart';
import 'package:open_file/src/platform/open_file.dart';
import 'package:open_file/open_file.dart';

class FileService {
FileService._();

/// Return the Clicked Image
/// Picks an image and returns a `PlatformFile`.
/// The file size should be in MB.
/// No limit for file size if [requiredFileSize] is null
/// this will throw error if file size exceeds [requiredFileSize].
static Future<PlatformFile?> pickImage(
ImageSource source, String? name) async {
ImageSource source, String? name, double? requiredFileSize) async {
final pickedFile = await ImagePicker().pickImage(source: source);

if (pickedFile != null) {
if (requiredFileSize != null) {
final fileSizeInBytes = requiredFileSize * 1024 * 1024;
final pickedFileSize = await pickedFile.length();

if (pickedFileSize > fileSizeInBytes) {
throw Exception("File size exceeds the $requiredFileSize MB limit");
}
}

return PlatformFile(
name: name ?? pickedFile.name,
size: await pickedFile.length(),
path: pickedFile.path,
);
}

return null;
}

/// Return the Picked File from the System
/// handled extensions are [pdf, jpeg, jpg, png]
/// We can add more via [extension] parameter
static Future<PlatformFile?> pickDocument(List<String>? extensions) async {
/// Picks a document from the system and returns a `PlatformFile`.
/// Handled extensions are [pdf, jpeg, jpg, png]. More can be added via the `extensions` parameter.
/// The file size should be in MB.
/// No limit for file size if [requiredFileSize] is null
/// this will throw error if file size exceeds [requiredFileSize].
static Future<PlatformFile?> pickDocument(
List<String>? extensions, double? requiredFileSize) async {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: extensions ??
[
'pdf',
'jpg',
'jpeg',
'png',
],
allowedExtensions: extensions ?? ['pdf', 'jpg', 'jpeg', 'png'],
);
if (result != null) return result.files.first;

if (result != null && result.files.isNotEmpty) {
final pickedFile = result.files.first;

if (requiredFileSize != null) {
final fileSizeInBytes = requiredFileSize * 1024 * 1024;

if (pickedFile.size > fileSizeInBytes) {
throw Exception("File size exceeds the $requiredFileSize MB limit");
}
}

return pickedFile;
}

return null;
}

/// Open the DOC
/// Opens the document at the specified path.
static void openFile(String path) {
OpenFile.open(path);
}
Expand Down

0 comments on commit 267cb09

Please sign in to comment.