diff --git a/CHANGELOG.md b/CHANGELOG.md index b10c21a..eb9c837 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,32 @@ All notable changes to this project will be documented in this file. ## [0.2.10] + * Moved from `http` to `dio` for better control over the headers and other options + * LinkDetails has more options such as `method` & `body`\ + here is the example of how to use the LinkDetails + ```dart + LinkDetails( + link: "www.example.com/file.extention", + headers: /// Your headers here, + method: /// Your method here (GET, POST, PUT, DELETE, PATCH), + body: /// Request body here + ), + ``` + * Both `saveFile` and `saveAs` methods now have the `dioClient` & `transformDioResponse` as a parameter, so you can pass your own dio client to the method and it will use that client to download the file & you can also pass the `transformDioResponse` to transform the response as per your requirement.\ + ```dart + await FileSaver.instance.saveFile( + name: "FileName", + link: "www.example.com/file.extention", + filePath: "pathOfFile", + file: File(), + bytes: bytes, + ext: "extention", + mimeType: MimeType.pdf, + dioClient: Dio(), + transformDioResponse: (response) { + return response.data; + }); + ``` * Fixed ([GitHub issue #95](https://github.com/incrediblezayed/file_saver/issues/95)) * Fixed ([GitHub issue #92](https://github.com/incrediblezayed/file_saver/issues/92)) diff --git a/README.md b/README.md index 6b53814..7d49a28 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ [![Discord](https://www.hassanansari.dev/public/file_saver_discord.png)](https://discord.gg/4yRFt68kty) +## Huge Shoutout to all the contributors and the people who are using this package, I'm really grateful to all of you. Thank you for your support. + This plugin package primarily focuses on one task: saving files on Android, iOS, Web, Windows, MacOS, and Linux. It might not have a plethora of features, but it does this job well. This package depends on path_provider for Android and iOS and basic html anchor for Web. The main reason I built this plugin was to @@ -22,7 +24,9 @@ await FileSaver.instance.saveFile({ LinkDetails? link, String ext = "", MimeType mimeType = MimeType.other, - String? customMimeType + String? customMimeType, + Dio? dioClient, + Uint8List Function(Uint8List)? transformDioResponse, }); ``` @@ -35,9 +39,14 @@ _File file_ which will be your file in the File object (from dart:io)\ Or\ _Stirng filePath_ which will be your file path\ Or\ -_LinkDetails link_ which will provide the link & header to your file. LinkDetails can be used as +_LinkDetails link_ which will provide the link, header, request methid and body to your file. LinkDetails can be used as ```dart -LinkDetails(link: "https://www.example.com/file.extentions", headers: {"your-header-key": "you-header-value"}) +LinkDetails( + link: "https://www.example.com/file.extentions", + headers: {"your-header-key": "you-header-value"}, + method: "POST", + body: body +) ``` \ Out of these parameters, you will have to use atleast one @@ -46,6 +55,12 @@ _String ext_ this will be your file extension.\ Another parameter is _MimeType type_ Specifically for Web, which will be your file type +_String customMimeType_ this will be your custom mime type, if you want to use your own mime type, you can use this parameter + +_Dio dioClient_ this will be your dio client, if you want to use dio for downloading the file, you can use this parameter + +_Uint8List Function(Uint8List) transformDioResponse_ this will be your function to transform the response, if you want to transform the response as per your requirement, you can use this parameter + MimeType is also included in my Package, I've included types for **Sheets, Presentation, Word, Plain Text, PDF, MP3, MP4 and many other common formats** @@ -60,7 +75,9 @@ await FileSaver.instance.saveAs({ LinkDetails? link, required String ext, required MimeType mimeType, - String? customMimeType + String? customMimeType, + Dio? dioClient, + Uint8List Function(Uint8List)? transformDioResponse, }); ``` diff --git a/example/lib/main.dart b/example/lib/main.dart index cc1ec17..558483e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -31,7 +31,7 @@ class _MyAppState extends State { final Sheet sheetObject = excel['Sheet1']; sheetObject.insertColumn(0); for (int i = 1; i < 10; i++) { - sheetObject.appendRow([IntCellValue(i)]); + sheetObject.appendRow([TextCellValue(i.toString())]); } List? sheets = excel.encode(); return sheets; diff --git a/example/lib/save_with_byte_proxy.dart b/example/lib/save_with_byte_proxy.dart index 5ce5117..49dc650 100644 --- a/example/lib/save_with_byte_proxy.dart +++ b/example/lib/save_with_byte_proxy.dart @@ -31,7 +31,7 @@ class _SaveWithByteProxyState extends State { final Sheet sheetObject = excel['Sheet1']; sheetObject.insertColumn(0); for (int i = 1; i < 10; i++) { - sheetObject.appendRow([IntCellValue(i)]); + sheetObject.appendRow([TextCellValue(i.toString())]); } List? sheets = excel.encode(); return sheets; @@ -114,7 +114,7 @@ class _SaveWithByteProxyState extends State { Sheet sheetObject = excel['Sheet1']; sheetObject.insertColumn(0); for (int i = 1; i < 10; i++) { - sheetObject.appendRow([IntCellValue(i)]); + sheetObject.appendRow([TextCellValue(i.toString())]); } String path = await FileSaver.instance.saveFile( @@ -168,7 +168,6 @@ class _SaveWithByteProxyState extends State { ///extController.text, mimeType: MimeType.microsoftExcel, - ); log(path.toString()); }, diff --git a/lib/file_saver.dart b/lib/file_saver.dart index b80b59a..a147f90 100644 --- a/lib/file_saver.dart +++ b/lib/file_saver.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:io'; +import 'package:dio/dio.dart'; import 'package:file_saver/src/models/file.model.dart'; import 'package:file_saver/src/models/link_details.dart'; import 'package:file_saver/src/saver.dart'; @@ -47,23 +48,33 @@ class FileSaver { /// mimeType (Mainly required for web): MimeType from enum MimeType.. /// /// More Mimetypes will be added in future - Future saveFile( - {required String name, - Uint8List? bytes, - File? file, - String? filePath, - LinkDetails? link, - String ext = '', - MimeType mimeType = MimeType.other, - String? customMimeType}) async { - assert(mimeType != MimeType.custom || customMimeType != null, - 'customMimeType is required when mimeType is MimeType.custom'); - + Future saveFile({ + required String name, + Uint8List? bytes, + File? file, + String? filePath, + LinkDetails? link, + String ext = '', + MimeType mimeType = MimeType.other, + String? customMimeType, + Dio? dioClient, + Uint8List Function(dynamic data)? transformDioResponse, + }) async { + if (mimeType == MimeType.custom && customMimeType == null) { + throw Exception( + 'customMimeType is required when mimeType is MimeType.custom'); + } String extension = Helpers.getExtension(extension: ext); final isFile = file != null || filePath != null; if (!isFile) { bytes = bytes ?? - await Helpers.getBytes(file: file, filePath: filePath, link: link); + await Helpers.getBytes( + file: file, + filePath: filePath, + link: link, + dioClient: dioClient, + transformDioResponse: transformDioResponse, + ); } try { if (isFile) { @@ -139,13 +150,21 @@ class FileSaver { required String ext, required MimeType mimeType, String? customMimeType, + Dio? dioClient, + Uint8List Function(dynamic data)? transformDioResponse, }) async { if (mimeType == MimeType.custom && customMimeType == null) { throw Exception( 'customMimeType is required when mimeType is MimeType.custom'); } bytes = bytes ?? - await Helpers.getBytes(file: file, filePath: filePath, link: link); + await Helpers.getBytes( + file: file, + filePath: filePath, + link: link, + dioClient: dioClient, + transformDioResponse: transformDioResponse, + ); _saver = Saver( fileModel: FileModel( diff --git a/lib/src/utils/helpers.dart b/lib/src/utils/helpers.dart index 7026b57..febee2d 100644 --- a/lib/src/utils/helpers.dart +++ b/lib/src/utils/helpers.dart @@ -27,12 +27,12 @@ class Helpers { ///This method provides [Uint8List] from link ///[LinkDetails] is used to provide link and headers ///[Dio] is used to provide custom dio instance if needed - ///[transformData] is used to provide custom data transformation + ///[transformDioResponse] is used to provide custom data transformation ///Note: Always put the full link within the link field static Future _getBytesFromLink( LinkDetails link, { Dio? dioClient, - Uint8List Function(dynamic data)? transformData, + Uint8List Function(dynamic data)? transformDioResponse, }) async { final dio = dioClient ?? Dio( @@ -44,8 +44,8 @@ class Helpers { Response response = await dio.request( link.link, ); - if (transformData != null) { - return transformData(response.data); + if (transformDioResponse != null) { + return transformDioResponse(response.data); } Uint8List bytes = response.data; return bytes; @@ -89,15 +89,24 @@ class Helpers { } ///This method is used to get [Uint8List] from either [filePath], [link] or [file] - static Future getBytes( - {String? filePath, LinkDetails? link, File? file}) async { + static Future getBytes({ + String? filePath, + LinkDetails? link, + File? file, + Dio? dioClient, + Uint8List Function(dynamic data)? transformDioResponse, + }) async { assert(filePath != null || link != null || file != null, 'Either filePath or link or file must be provided'); if (filePath != null) { return _getBytesFromPath(filePath); } else { if (link != null) { - return _getBytesFromLink(link); + return _getBytesFromLink( + link, + dioClient: dioClient, + transformDioResponse: transformDioResponse, + ); } else if (file != null) { return _getBytesFromFile(file); } else {