55 lines
1.4 KiB
Dart
55 lines
1.4 KiB
Dart
|
|
/// 다운로드 서비스
|
||
|
|
library;
|
||
|
|
|
||
|
|
import 'dart:io';
|
||
|
|
import 'package:flutter_downloader/flutter_downloader.dart';
|
||
|
|
import 'package:path_provider/path_provider.dart';
|
||
|
|
import 'package:permission_handler/permission_handler.dart';
|
||
|
|
|
||
|
|
/// 다운로드 서비스 초기화
|
||
|
|
Future<void> initDownloadService() async {
|
||
|
|
await FlutterDownloader.initialize(
|
||
|
|
debug: false,
|
||
|
|
ignoreSsl: true,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// 이미지 다운로드
|
||
|
|
Future<String?> downloadImage(String url, {String? fileName}) async {
|
||
|
|
// 권한 요청
|
||
|
|
if (Platform.isAndroid) {
|
||
|
|
final status = await Permission.notification.request();
|
||
|
|
if (status.isDenied) {
|
||
|
|
// 알림 권한 거부해도 다운로드는 진행
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 다운로드 경로 설정
|
||
|
|
Directory? directory;
|
||
|
|
if (Platform.isAndroid) {
|
||
|
|
directory = Directory('/storage/emulated/0/Download');
|
||
|
|
if (!await directory.exists()) {
|
||
|
|
directory = await getExternalStorageDirectory();
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
directory = await getApplicationDocumentsDirectory();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (directory == null) return null;
|
||
|
|
|
||
|
|
// 파일명 생성
|
||
|
|
final name = fileName ?? 'fromis9_${DateTime.now().millisecondsSinceEpoch}.jpg';
|
||
|
|
|
||
|
|
// 다운로드 시작
|
||
|
|
final taskId = await FlutterDownloader.enqueue(
|
||
|
|
url: url,
|
||
|
|
savedDir: directory.path,
|
||
|
|
fileName: name,
|
||
|
|
showNotification: true,
|
||
|
|
openFileFromNotification: true,
|
||
|
|
);
|
||
|
|
|
||
|
|
return taskId;
|
||
|
|
}
|
||
|
|
|