Files
syncrow-app/lib/utils/helpers/file_helper.dart
2024-02-17 16:27:27 +03:00

126 lines
4.0 KiB
Dart

// import 'dart:io';
// import 'dart:math';
// import 'package:dio/dio.dart';
// import 'package:file_picker/file_picker.dart';
// import 'package:flutter/material.dart';
// import 'package:flutter/services.dart';
//
// import 'package:path_provider/path_provider.dart';
//
// class FileHelper {
// static Future<Map<String, Uint8List>> takePicture() async {
// try {
// XFile? pickedImage = await ImagePicker().pickImage(source: ImageSource.camera);
// if (pickedImage != null) {
// final fileTemp = File(pickedImage.path);
// final bytes = fileTemp.readAsBytesSync();
// return {pickedImage.path: bytes};
// } else {
// return {};
// }
// } on PlatformException catch (_) {
// return {};
// }
// }
//
// static Future<Map<String, Uint8List>> pickFile({bool gallery = false}) async {
// try {
// FilePickerResult? pickedFile;
// XFile? pickedFromGallery;
// if (gallery) {
// final picker = ImagePicker();
// pickedFromGallery =
// await picker.pickImage(source: ImageSource.gallery, requestFullMetadata: false);
// } else {
// pickedFile = await FilePicker.platform.pickFiles(
// allowedExtensions: ['png', 'jpg', 'jpeg', 'pdf'],
// type: FileType.custom,
// withReadStream: true,
// );
// }
// if (pickedFile != null) {
// String path = pickedFile.files.single.path ?? '';
// final fileTemp = File(path);
// final bytes = fileTemp.readAsBytesSync();
// if (path.isEmpty) {
// return {};
// }
// return {path: bytes};
// } else if (pickedFromGallery != null) {
// String path = pickedFromGallery.path;
// final fileTemp = File(path);
// final bytes = fileTemp.readAsBytesSync();
// if (path.isEmpty) {
// return {};
// }
// return {path: bytes};
// } else {
// return {};
// }
// } on PlatformException catch (_) {
// return {};
// }
// }
//
// Future<String> downloadFile(String url) async {
// String filePath;
// try {
// filePath = await _prepareSaveDir(url);
// await HTTPService()
// .downloadRequest(path: url, savePath: filePath, expectedResponseModel: (json) {});
// } catch (err) {
// CustomSnackBar.displaySnackBar('Something went wrong please try again!');
// return '';
// }
// return filePath;
// }
//
// String getRandomString(int length) {
// const chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
// Random rnd = Random();
//
// return String.fromCharCodes(
// Iterable.generate(length, (_) => chars.codeUnitAt(rnd.nextInt(chars.length))));
// }
//
// Future<String> _prepareSaveDir(String url) async {
// try {
// final localPath = (await _findLocalPath(url))!;
//
// final savedDir = Directory(localPath);
// bool hasExisted = await savedDir.exists();
// if (!hasExisted) {
// await File(savedDir.path).create();
// }
// return savedDir.path;
// } catch (err) {
// return '';
// }
// }
//
// Future<String?> _findLocalPath(String url) async {
// String fileName = getRandomString(10);
// String fileNameWithExtension = url.contains('pdf') ? '$fileName.pdf' : '$fileName.jpeg';
//
// if (Platform.isAndroid) {
// return "/sdcard/download/$fileNameWithExtension";
// } else {
// var directory = await getApplicationDocumentsDirectory();
// return '${directory.path}${Platform.pathSeparator}$fileNameWithExtension';
// }
// }
//
// static Future<Uint8List?> fetchPdfContent(final String url) async {
// try {
// final Response<List<int>> response = await Dio().get<List<int>>(
// url,
// options: Options(responseType: ResponseType.bytes),
// );
// return Uint8List.fromList(response.data ?? []);
// } catch (e) {
// debugPrint(e.toString());
// return null;
// }
// }
// }