Link between front-end and back-end
This commit is contained in:
@ -8,7 +8,6 @@ import 'package:path/path.dart' as path;
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:image/image.dart' as img;
|
||||
import 'package:image_cropper/image_cropper.dart' as img_cropper;
|
||||
import 'package:uuid/v5.dart';
|
||||
|
||||
class ImagesManager {
|
||||
static final ImagesManager instance = ImagesManager._internal();
|
||||
@ -32,10 +31,10 @@ class ImagesManager {
|
||||
final directory = Directory.fromUri(directoryUri);
|
||||
|
||||
if (!await directory.exists()) {
|
||||
log.info("Image directory does not yet extists. Creating it.");
|
||||
log.info("Image directory does not yet exist. Creating it.");
|
||||
}
|
||||
|
||||
directory.create(recursive: false);
|
||||
await directory.create(recursive: true);
|
||||
|
||||
log.info("Image directory set up at '$directory'");
|
||||
|
||||
@ -43,11 +42,9 @@ class ImagesManager {
|
||||
}
|
||||
|
||||
Future<String?> createImage(ImageSource source) async {
|
||||
// Get image from camera or not
|
||||
final XFile? ximage = await ImagePicker().pickImage(source: source);
|
||||
if (ximage == null) return null;
|
||||
|
||||
// Crop image
|
||||
final uuid = Uuid().v6();
|
||||
final imageDir = await imageDirectory;
|
||||
final finalPath = path.join(
|
||||
@ -60,10 +57,9 @@ class ImagesManager {
|
||||
);
|
||||
await ximage.saveTo(tempPath);
|
||||
|
||||
img_cropper.CroppedFile? cropped = await img_cropper.ImageCropper()
|
||||
.cropImage(
|
||||
img_cropper.CroppedFile? cropped = await img_cropper.ImageCropper().cropImage(
|
||||
sourcePath: tempPath,
|
||||
aspectRatio: img_cropper.CropAspectRatio(ratioX: 1.0, ratioY: 1.0),
|
||||
aspectRatio: const img_cropper.CropAspectRatio(ratioX: 1.0, ratioY: 1.0),
|
||||
uiSettings: [
|
||||
img_cropper.AndroidUiSettings(
|
||||
toolbarTitle: "Crop image",
|
||||
@ -74,7 +70,10 @@ class ImagesManager {
|
||||
],
|
||||
);
|
||||
|
||||
if (cropped == null) return null;
|
||||
if (cropped == null) {
|
||||
await File(tempPath).delete();
|
||||
return null;
|
||||
}
|
||||
|
||||
await File(finalPath).writeAsBytes(await cropped.readAsBytes());
|
||||
await File(tempPath).delete();
|
||||
@ -94,10 +93,37 @@ class ImagesManager {
|
||||
final imagePath = path.join(imageDir.path, "$imageUuid.jpg");
|
||||
final file = File(imagePath);
|
||||
if (!await file.exists()) {
|
||||
log.warning("Tried to load an image that does not extist: '$imagePath'.");
|
||||
log.warning("Tried to load an image that does not exist: '$imagePath'.");
|
||||
return null;
|
||||
}
|
||||
|
||||
return Image.file(file);
|
||||
}
|
||||
|
||||
Future<bool> deleteImage(String imageUuid) async {
|
||||
final imageDir = await imageDirectory;
|
||||
if (!Uuid.isValidUUID(fromString: imageUuid)) {
|
||||
log.warning(
|
||||
"Tried to delete an image with an invalid UUID : '$imageUuid'.",
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
final imagePath = path.join(imageDir.path, "$imageUuid.jpg");
|
||||
final file = File(imagePath);
|
||||
|
||||
if (await file.exists()) {
|
||||
try {
|
||||
await file.delete();
|
||||
log.info("Image with UUID '$imageUuid' deleted successfully.");
|
||||
return true;
|
||||
} catch (e) {
|
||||
log.severe("Error deleting image with UUID '$imageUuid': $e");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
log.warning("Tried to delete an image that does not exist: '$imagePath'.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user