|
- import 'dart:io';
-
- import 'package:farm_tpf/custom_model/Media.dart';
- import 'package:farm_tpf/utils/const_common.dart';
- import 'package:flutter_cache_manager/flutter_cache_manager.dart';
- import 'package:flutter_image_compress/flutter_image_compress.dart';
- import 'package:mime/mime.dart';
- import 'package:path_provider/path_provider.dart' as path_provider;
- import 'package:path/path.dart' as path;
-
- class UtilAction {
- static Future<List<Media>> cacheFiles(String existedMedias) async {
- var medias = List<Media>();
- var mediaPaths = existedMedias.split(";");
- for (int i = 0; i < mediaPaths.length; i++) {
- var tempFile = await DefaultCacheManager()
- .getSingleFile(ConstCommon.baseImageUrl + mediaPaths[i]);
- print(tempFile.path);
- var isVideo = lookupMimeType(tempFile.path) == "video/mp4";
- print("file type: " + lookupMimeType(tempFile.path));
- Media media = Media()
- ..pathFile = tempFile.path
- ..isVideo = isVideo;
- medias.add(media);
- }
- return medias;
- }
-
- static List<Media> convertFilePathToMedia(String existedMedias) {
- var medias = List<Media>();
- var mediaPaths = existedMedias.split(";");
- for (int i = 0; i < mediaPaths.length; i++) {
- var filePath = ConstCommon.baseImageUrl + mediaPaths[i];
- var isVideo = lookupMimeType(filePath) == "video/mp4";
- Media media = Media()
- ..pathFile = filePath
- ..isVideo = isVideo
- ..isServerFile = true;
- medias.add(media);
- }
- return medias;
- }
-
- static num convertUnit({num inputValue, String oldUnit, String newUnit}) {
- num result = inputValue;
- if (newUnit.isEmpty || newUnit == oldUnit) {
- return result;
- }
- if (oldUnit == "kg" || oldUnit == "l") {
- result = result * 1000;
- } else if (oldUnit == "g" || oldUnit == "ml") {
- result = result / 1000;
- } else if (oldUnit == "m") {
- if (newUnit == "cm") {
- result = result * 100;
- } else if (newUnit == "mm") {
- result = result * 1000;
- }
- } else if (oldUnit == "cm") {
- if (newUnit == "m") {
- result = result / 100;
- } else if (newUnit == "mm") {
- result = result * 10;
- }
- } else if (oldUnit == "mm") {
- if (newUnit == "m") {
- result = result / 1000;
- } else if (newUnit == "cm") {
- result = result / 10;
- }
- }
- return result;
- }
-
- static Future<File> compressImage(File file) async {
- final dir = await path_provider.getTemporaryDirectory();
- final fileName = path.basenameWithoutExtension(file.path);
- final targetPath = dir.absolute.path + "/$fileName.jpg";
- print('before: ' + file.lengthSync().toString());
- final imgFile = await compressAndGetFile(file, targetPath);
- print('after: ' + imgFile.lengthSync().toString());
- return imgFile;
- }
-
- static Future<File> compressAndGetFile(File file, String targetPath) async {
- final result = await FlutterImageCompress.compressAndGetFile(
- file.absolute.path, targetPath,
- quality: 5, minWidth: 1280, minHeight: 720);
- return result;
- }
- }
|