You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.1KB

  1. import 'dart:io';
  2. import 'package:farm_tpf/custom_model/Media.dart';
  3. import 'package:farm_tpf/utils/const_common.dart';
  4. import 'package:flutter_cache_manager/flutter_cache_manager.dart';
  5. import 'package:flutter_image_compress/flutter_image_compress.dart';
  6. import 'package:mime/mime.dart';
  7. import 'package:path_provider/path_provider.dart' as path_provider;
  8. import 'package:path/path.dart' as path;
  9. class UtilAction {
  10. static Future<List<Media>> cacheFiles(String existedMedias) async {
  11. var medias = <Media>[];
  12. var mediaPaths = existedMedias.split(";");
  13. for (var i = 0; i < mediaPaths.length; i++) {
  14. var tempFile = await DefaultCacheManager().getSingleFile(ConstCommon.baseImageUrl + mediaPaths[i]);
  15. print(tempFile.path);
  16. var isVideo = lookupMimeType(tempFile.path) == "video/mp4";
  17. var media = Media()
  18. ..pathFile = tempFile.path
  19. ..isVideo = isVideo;
  20. medias.add(media);
  21. }
  22. return medias;
  23. }
  24. static List<Media> convertFilePathToMedia(String existedMedias) {
  25. var medias = <Media>[];
  26. var mediaPaths = existedMedias.split(";");
  27. for (var i = 0; i < mediaPaths.length; i++) {
  28. var filePath = ConstCommon.baseImageUrl + mediaPaths[i];
  29. var isVideo = lookupMimeType(filePath) == "video/mp4";
  30. var media = Media()
  31. ..pathFile = filePath
  32. ..isVideo = isVideo
  33. ..isServerFile = true;
  34. medias.add(media);
  35. }
  36. return medias;
  37. }
  38. static num convertUnit({
  39. required num inputValue,
  40. required String oldUnit,
  41. required String newUnit,
  42. }) {
  43. var result = inputValue;
  44. if (newUnit.isEmpty || newUnit == oldUnit) {
  45. return result;
  46. }
  47. if (oldUnit == "kg" || oldUnit == "l") {
  48. result = result * 1000;
  49. } else if (oldUnit == "g" || oldUnit == "ml") {
  50. result = result / 1000;
  51. } else if (oldUnit == "m") {
  52. if (newUnit == "cm") {
  53. result = result * 100;
  54. } else if (newUnit == "mm") {
  55. result = result * 1000;
  56. }
  57. } else if (oldUnit == "cm") {
  58. if (newUnit == "m") {
  59. result = result / 100;
  60. } else if (newUnit == "mm") {
  61. result = result * 10;
  62. }
  63. } else if (oldUnit == "mm") {
  64. if (newUnit == "m") {
  65. result = result / 1000;
  66. } else if (newUnit == "cm") {
  67. result = result / 10;
  68. }
  69. }
  70. return result;
  71. }
  72. static Future<File> compressImage(File file) async {
  73. final dir = await path_provider.getTemporaryDirectory();
  74. final fileName = path.basenameWithoutExtension(file.path);
  75. final targetPath = dir.absolute.path + "/$fileName.jpg";
  76. print('before: ' + file.lengthSync().toString());
  77. final imgFile = await compressAndGetFile(file, targetPath);
  78. print('after: ' + imgFile.lengthSync().toString());
  79. return imgFile;
  80. }
  81. static Future<File> compressAndGetFile(File file, String targetPath) async {
  82. try {
  83. final result = await FlutterImageCompress.compressAndGetFile(file.absolute.path, targetPath, quality: 50, minWidth: 1280, minHeight: 720);
  84. return result!;
  85. } on UnsupportedError catch (e) {
  86. return Future.value(file);
  87. }
  88. }
  89. }