1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-23 05:19:22 +00:00
comunicmobile/lib/utils/files_utils.dart

48 lines
1.5 KiB
Dart
Raw Normal View History

2021-03-13 16:50:59 +00:00
import 'dart:io';
2019-04-25 09:13:02 +00:00
import 'package:flutter/material.dart';
2021-03-13 17:11:28 +00:00
import 'package:image_cropper/image_cropper.dart';
2021-03-13 16:50:59 +00:00
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:random_string/random_string.dart';
2019-04-25 09:13:02 +00:00
2021-03-13 17:03:20 +00:00
import '../models/api_request.dart';
import '../ui/dialogs/pick_file_dialog.dart';
2019-04-25 09:13:02 +00:00
/// Files utilities
///
/// @author Pierre HUBERT
/// Ask the user to choose an image, either from the gallery or using the camera
///
2021-03-13 17:03:20 +00:00
/// Throws an exception null in case of failure
2021-03-13 17:11:28 +00:00
Future<BytesFile> pickImage(BuildContext context,
{CropAspectRatio aspectRatio}) async {
2021-03-13 17:03:20 +00:00
return await showPickFileDialog(
2019-04-25 09:13:02 +00:00
context: context,
2021-03-13 17:03:20 +00:00
allowedMimeTypes: ["image/png", "image/jpeg", "image/gif"],
imageMaxHeight: 10000,
imageMaxWidth: 10000,
2021-03-13 17:11:28 +00:00
aspectRatio: aspectRatio,
2019-04-25 09:13:02 +00:00
);
}
2021-03-13 16:50:59 +00:00
/// Generate a new temporary file
///
/// Throws in case of failure
Future<File> generateTemporaryFile() async {
final tempDir = await getTemporaryDirectory();
if (tempDir == null)
throw Exception("Could not generate temporary directory!");
return File(path.join(tempDir.path, randomString(15, from: 65, to: 90)));
}
/// Check if a mime type maps to an image or not
bool isImage(String mimeType) => mimeType.startsWith("image/");
2021-03-12 18:10:10 +00:00
2021-03-12 19:52:26 +00:00
/// Check if a mime type maps to a video or not
2021-03-12 18:10:10 +00:00
bool isVideo(String mimeType) => mimeType.startsWith("video/mp4");
2021-03-12 19:52:26 +00:00
/// Check if a mime type maps to an audio file or not
bool isAudio(String mimeType) => mimeType.startsWith("audio/mpeg");