Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d3fc58
commit 4691008
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:dio/dio.dart'; | ||
import 'package:odin/services/locator.dart'; | ||
import 'package:odin/services/logger.dart'; | ||
import 'package:odin/services/random_service.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
|
||
class DownloadService { | ||
final RandomService _randomService = locator<RandomService>(); | ||
String progress = '0%'; | ||
bool downloading = false; | ||
|
||
Dio dio = Dio(); | ||
Future<String> _getFilePath() async { | ||
final fileName = _randomService.getRandomString(10); | ||
Directory? dir; | ||
if (Platform.isAndroid) { | ||
dir = await getExternalStorageDirectory(); | ||
} else if (Platform.isIOS) { | ||
dir = await getTemporaryDirectory(); | ||
} else { | ||
dir = await getDownloadsDirectory(); | ||
} | ||
String path = '${dir?.path}/$fileName.odin'; | ||
logger.d("File path : $path"); | ||
return path; | ||
} | ||
|
||
Future<File> downloadFile(String url) async { | ||
final filePath = await _getFilePath(); | ||
progress = '0%'; | ||
downloading = true; | ||
await dio.download( | ||
url, | ||
filePath, | ||
onReceiveProgress: (rcv, total) { | ||
progress = ((rcv / total) * 100).toStringAsFixed(0) + "%"; | ||
}, | ||
deleteOnError: true, | ||
); | ||
downloading = false; | ||
return File(filePath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters