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.

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