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.0KB

  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 (int 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. Media 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 (int i = 0; i < mediaPaths.length; i++) {
  28. var filePath = ConstCommon.baseImageUrl + mediaPaths[i];
  29. var isVideo = lookupMimeType(filePath) == "video/mp4";
  30. Media media = Media()
  31. ..pathFile = filePath
  32. ..isVideo = isVideo
  33. ..isServerFile = true;
  34. medias.add(media);
  35. }
  36. return medias;
  37. }
  38. static num convertUnit({num? inputValue, String? oldUnit, String? newUnit}) {
  39. num result = inputValue ?? 0;
  40. if ((newUnit ?? '').isEmpty || newUnit == oldUnit) {
  41. return result;
  42. }
  43. if (oldUnit == "kg" || oldUnit == "l") {
  44. result = result * 1000;
  45. } else if (oldUnit == "g" || oldUnit == "ml") {
  46. result = result / 1000;
  47. } else if (oldUnit == "m") {
  48. if (newUnit == "cm") {
  49. result = result * 100;
  50. } else if (newUnit == "mm") {
  51. result = result * 1000;
  52. }
  53. } else if (oldUnit == "cm") {
  54. if (newUnit == "m") {
  55. result = result / 100;
  56. } else if (newUnit == "mm") {
  57. result = result * 10;
  58. }
  59. } else if (oldUnit == "mm") {
  60. if (newUnit == "m") {
  61. result = result / 1000;
  62. } else if (newUnit == "cm") {
  63. result = result / 10;
  64. }
  65. }
  66. return result;
  67. }
  68. static Future<File> compressImage(File file) async {
  69. final dir = await path_provider.getTemporaryDirectory();
  70. final fileName = path.basenameWithoutExtension(file.path);
  71. final targetPath = dir.absolute.path + "/$fileName.jpg";
  72. final imgFile = await compressAndGetFile(file, targetPath);
  73. return File(imgFile?.path ?? '');
  74. }
  75. static Future<XFile?> compressAndGetFile(File file, String targetPath) async {
  76. try {
  77. final result = await FlutterImageCompress.compressAndGetFile(
  78. file.absolute.path,
  79. targetPath,
  80. quality: 50,
  81. minWidth: 1280,
  82. minHeight: 720,
  83. );
  84. return result;
  85. } on UnsupportedError catch (e) {
  86. //
  87. }
  88. }
  89. }