Skip to content

Commit

Permalink
Update Readme & Change log
Browse files Browse the repository at this point in the history
  • Loading branch information
incrediblezayed committed Feb 18, 2024
1 parent ba4ff43 commit b3fbe8b
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 29 deletions.
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
});
```

Expand All @@ -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
Expand All @@ -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**

Expand All @@ -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,
});
```

Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class _MyAppState extends State<MyApp> {
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<int>? sheets = excel.encode();
return sheets;
Expand Down
5 changes: 2 additions & 3 deletions example/lib/save_with_byte_proxy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class _SaveWithByteProxyState extends State<SaveWithByteProxy> {
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<int>? sheets = excel.encode();
return sheets;
Expand Down Expand Up @@ -114,7 +114,7 @@ class _SaveWithByteProxyState extends State<SaveWithByteProxy> {
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(
Expand Down Expand Up @@ -168,7 +168,6 @@ class _SaveWithByteProxyState extends State<SaveWithByteProxy> {

///extController.text,
mimeType: MimeType.microsoftExcel,

);
log(path.toString());
},
Expand Down
47 changes: 33 additions & 14 deletions lib/file_saver.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -47,23 +48,33 @@ class FileSaver {
/// mimeType (Mainly required for web): MimeType from enum MimeType..
///
/// More Mimetypes will be added in future
Future<String> 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<String> 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) {
Expand Down Expand Up @@ -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(
Expand Down
23 changes: 16 additions & 7 deletions lib/src/utils/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8List> _getBytesFromLink(
LinkDetails link, {
Dio? dioClient,
Uint8List Function(dynamic data)? transformData,
Uint8List Function(dynamic data)? transformDioResponse,
}) async {
final dio = dioClient ??
Dio(
Expand All @@ -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;
Expand Down Expand Up @@ -89,15 +89,24 @@ class Helpers {
}

///This method is used to get [Uint8List] from either [filePath], [link] or [file]
static Future<Uint8List> getBytes(
{String? filePath, LinkDetails? link, File? file}) async {
static Future<Uint8List> 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 {
Expand Down

0 comments on commit b3fbe8b

Please sign in to comment.