Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(download): chunked download file #1531

Merged
merged 7 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions lib/screen/detail/artwork_detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class _ArtworkDetailPageState extends State<ArtworkDetailPage>
with AfterLayoutMixin<ArtworkDetailPage> {
late ScrollController _scrollController;
late bool withSharing;
ValueNotifier<double> downloadProgress = ValueNotifier(0);

HashSet<String> _accountNumberHash = HashSet.identity();
AssetToken? currentAsset;
Expand Down Expand Up @@ -484,22 +485,38 @@ class _ArtworkDetailPageState extends State<ArtworkDetailPage>
BlendMode.srcIn,
),
),
iconOnProcessing: const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation<Color>(AppColor.disabledColor),
strokeWidth: 1,
),
),
iconOnProcessing: ValueListenableBuilder(
valueListenable: downloadProgress,
builder: (context, double value, child) {
ppupha marked this conversation as resolved.
Show resolved Hide resolved
return SizedBox(
ppupha marked this conversation as resolved.
Show resolved Hide resolved
height: 20,
width: 20,
child: CircularProgressIndicator(
value: value <= 0 ? null : value,
valueColor: value <= 0
? null
: AlwaysStoppedAnimation<Color>(Colors.blue),
ppupha marked this conversation as resolved.
Show resolved Hide resolved
ppupha marked this conversation as resolved.
Show resolved Hide resolved
backgroundColor:
value <= 0 ? null : AppColor.disabledColor,
color: AppColor.disabledColor,
strokeWidth: 2,
),
);
}),
ppupha marked this conversation as resolved.
Show resolved Hide resolved
onTap: () async {
try {
final file =
await _feralfileService.downloadFeralfileArtwork(asset);
final file = await _feralfileService.downloadFeralfileArtwork(
asset, onReceiveProgress: (received, total) {
setState(() {
downloadProgress.value = received / total;
});
});
if (!mounted) {
return;
}
setState(() {
downloadProgress.value = 0;
});
Navigator.of(context).pop();
if (file != null) {
await FileHelper.shareFile(file, deleteAfterShare: true);
Expand All @@ -510,6 +527,9 @@ class _ArtworkDetailPageState extends State<ArtworkDetailPage>
if (!mounted) {
return;
}
setState(() {
downloadProgress.value = 0;
});
log.info('Download artwork failed: $e');
if (e is DioException) {
unawaited(UIHelper.showFeralfileArtworkSavedFailed(context));
Expand Down
11 changes: 7 additions & 4 deletions lib/service/feralfile_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import 'package:autonomy_flutter/screen/claim/claim_token_page.dart';
import 'package:autonomy_flutter/service/account_service.dart';
import 'package:autonomy_flutter/util/asset_token_ext.dart';
import 'package:autonomy_flutter/util/custom_exception.dart';
import 'package:autonomy_flutter/util/download_helper.dart';
import 'package:autonomy_flutter/util/feralfile_extension.dart';
import 'package:autonomy_flutter/util/file_helper.dart';
import 'package:autonomy_flutter/util/log.dart';
import 'package:autonomy_flutter/util/wallet_storage_ext.dart';
import 'package:collection/collection.dart';
Expand Down Expand Up @@ -78,7 +78,8 @@ abstract class FeralFileService {

Future<Artwork> getArtwork(String artworkId);

Future<File?> downloadFeralfileArtwork(AssetToken assetToken);
Future<File?> downloadFeralfileArtwork(AssetToken assetToken,
{Function(int received, int total)? onReceiveProgress});
}

class FeralFileServiceImpl extends FeralFileService {
Expand Down Expand Up @@ -322,7 +323,8 @@ class FeralFileServiceImpl extends FeralFileService {
}

@override
Future<File?> downloadFeralfileArtwork(AssetToken assetToken) async {
Future<File?> downloadFeralfileArtwork(AssetToken assetToken,
{Function(int received, int total)? onReceiveProgress}) async {
try {
final artwork = await injector<FeralFileService>()
.getArtwork(assetToken.tokenId ?? '');
Expand All @@ -347,7 +349,8 @@ class FeralFileServiceImpl extends FeralFileService {
signature: signatureHex,
owner: ownerAddress,
);
final file = await FileHelper.downloadFile(url);
final file = DownloadHelper.fileChunkedDownload(url,
onReceiveProgress: onReceiveProgress);
return file;
} catch (e) {
log.info('Error downloading artwork: $e');
Expand Down
133 changes: 133 additions & 0 deletions lib/util/download_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import 'dart:io';

import 'package:autonomy_flutter/util/file_helper.dart';
import 'package:autonomy_flutter/util/log.dart';
import 'package:collection/collection.dart';
import 'package:dio/dio.dart';
import 'package:http/http.dart' as http;

class DownloadHelper {
static final _dio = Dio();

static Future<int> _getFileSize(Map<String, String> headers) async =>
int.parse(headers['content-length'] ?? '0');

static Future<String> _getFileName(Map<String, String> headers) async {
final fileName = headers['content-disposition']
?.split(';')
.firstWhereOrNull((element) => element.contains('filename'))
?.split('=')[1]
.replaceAll('"', '') ??
'file';
return fileName;
}

static String _getPartFilePath(String filePath, int partNum) =>
'$filePath.part$partNum';

static Future<void> _clearPartFiles(String filePath, int numParts) async {
for (int i = 0; i < numParts; i++) {
final partFile = File(_getPartFilePath(filePath, i));
if (partFile.existsSync()) {
await partFile.delete();
}
}
}

static int _getChunkSize(int fileSize) {
const int maxChunkSize = 1024 * 1024;
return fileSize > maxChunkSize ? maxChunkSize : fileSize;
}

static Future<File?> fileChunkedDownload(String fullUrl,
{Function(int received, int total)? onReceiveProgress}) async {
log.info('Downloading file: $fullUrl');
final dir = await FileHelper.getDownloadDir();
final savePath = '${dir.path}/Downloads/';
final request = http.MultipartRequest('GET', Uri.parse(fullUrl));
final response = await request.send();

// Get the file size
final int fileSize = await _getFileSize(response.headers);
final String fileName = await _getFileName(response.headers);
final filePath = savePath + fileName;
int received = 0;

// Calculate the number of parts
final partSize = _getChunkSize(fileSize);

final int numParts = (fileSize / partSize).ceil();
try {
// Perform multipart download
final downloadFeatures = List.generate(numParts, (i) => i).map((i) async {
final int start = i * partSize;
final int end = (i + 1) * partSize - 1;

await _dio.download(
fullUrl,
_getPartFilePath(filePath, i),
options: Options(
headers: {
HttpHeaders.rangeHeader: 'bytes=$start-$end',
},
),
);
received += partSize;
if (onReceiveProgress != null) {
onReceiveProgress(received, fileSize);
}

log.info('Downloaded part $i/$numParts');
});

final batchSize = 10;
ppupha marked this conversation as resolved.
Show resolved Hide resolved
ppupha marked this conversation as resolved.
Show resolved Hide resolved
final batches = <List<Future<void>>>[];
List<Future<void>> batch = <Future<void>>[];
for (final future in downloadFeatures) {
batch.add(future);
if (batch.length == batchSize) {
batches.add(batch);
batch = <Future<void>>[];
}
}
if (batch.isNotEmpty) {
batches.add(batch);
}
for (final batch in batches) {
await Future.wait(batch);
}

// Concatenate parts to create the final file
final outputFile = File(filePath);
final IOSink sink = outputFile.openWrite(mode: FileMode.writeOnlyAppend);
for (int i = 0; i < numParts; i++) {
final File partFile = File(_getPartFilePath(filePath, i));
await sink.addStream(partFile.openRead());
}
await sink.close();
await _clearPartFiles(filePath, numParts);
return outputFile;
} catch (e) {
log.info('Error downloading file: $e');
await _clearPartFiles(filePath, numParts);
}
return null;
}

static Future<File> downloadFile(
String fullUrl,
) async {
final response = await http.get(Uri.parse(fullUrl));
final bytes = response.bodyBytes;
final header = response.headers;
final filename = header['x-amz-meta-filename'] ??
header['content-disposition']
?.split(';')
.firstWhereOrNull((element) => element.contains('filename'))
?.split('=')[1]
.replaceAll('"', '') ??
'file';
final file = await FileHelper.saveFileToDownloadDir(bytes, filename);
return file;
}
}
22 changes: 3 additions & 19 deletions lib/util/file_helper.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:collection/collection.dart';
import 'package:http/http.dart' as http;
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:share_plus/share_plus.dart';

class FileHelper {
static Future<Directory> getDownloadDir() async =>
await getApplicationDocumentsDirectory();

static Future<bool> saveImageToGallery(Uint8List data, String name) async {
final response = await ImageGallerySaver.saveImage(data,
name: name, isReturnImagePathOfIOS: true);
Expand All @@ -33,23 +34,6 @@ class FileHelper {
return file;
}

static Future<File> downloadFile(
String fullUrl,
) async {
final response = await http.get(Uri.parse(fullUrl));
final bytes = response.bodyBytes;
final header = response.headers;
final filename = header['x-amz-meta-filename'] ??
header['content-disposition']
?.split(';')
.firstWhereOrNull((element) => element.contains('filename'))
?.split('=')[1]
.replaceAll('"', '') ??
'file';
final file = await FileHelper.saveFileToDownloadDir(bytes, filename);
return file;
}

static Future<ShareResult> shareFile(File file,
{bool deleteAfterShare = false, Function? onShareSuccess}) async {
final result = await Share.shareXFiles([XFile(file.path)]);
Expand Down
Loading