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.

75 lines
2.4KB

  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 num convertUnit({num inputValue, String oldUnit, String newUnit}) {
  27. num result = inputValue;
  28. if (newUnit.isEmpty || newUnit == oldUnit) {
  29. return result;
  30. }
  31. if (oldUnit == "kg" || oldUnit == "l") {
  32. result = result * 1000;
  33. } else if (oldUnit == "g" || oldUnit == "ml") {
  34. result = result / 1000;
  35. } else if (oldUnit == "m") {
  36. if (newUnit == "cm") {
  37. result = result * 100;
  38. } else if (newUnit == "mm") {
  39. result = result * 1000;
  40. }
  41. } else if (oldUnit == "cm") {
  42. if (newUnit == "m") {
  43. result = result / 100;
  44. } else if (newUnit == "mm") {
  45. result = result * 10;
  46. }
  47. } else if (oldUnit == "mm") {
  48. if (newUnit == "m") {
  49. result = result / 1000;
  50. } else if (newUnit == "cm") {
  51. result = result / 10;
  52. }
  53. }
  54. return result;
  55. }
  56. static Future<File> compressImage(File file) async {
  57. final dir = await path_provider.getTemporaryDirectory();
  58. final fileName = path.basenameWithoutExtension(file.path);
  59. final targetPath = dir.absolute.path + "/$fileName.jpg";
  60. final imgFile = await compressAndGetFile(file, targetPath);
  61. return imgFile;
  62. }
  63. static Future<File> compressAndGetFile(File file, String targetPath) async {
  64. final result = await FlutterImageCompress.compressAndGetFile(
  65. file.absolute.path, targetPath,
  66. minWidth: 1024, minHeight: 1024);
  67. return result;
  68. }
  69. }