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