Add a event that triggers after removing the image from the editor && delete unused dependencies and upgrade all packages and plugins and remove gallery_saver which has not been updated for more than 23 months, it was a great plugin but it old now, and I also add some simple documentation and other minor improvements (#1413)
parent
09113cbc90
commit
3e0ed1212a
6 changed files with 259 additions and 120 deletions
@ -1,5 +1,84 @@ |
||||
import 'package:string_validator/string_validator.dart'; |
||||
import 'dart:io'; |
||||
|
||||
import 'package:flutter/foundation.dart' show Uint8List; |
||||
import 'package:http/http.dart' as http; |
||||
import 'package:image_gallery_saver/image_gallery_saver.dart'; |
||||
|
||||
// I would like to orgnize the project structure and the code more |
||||
// but here I don't want to change too much since that is a community project |
||||
|
||||
RegExp _base64 = RegExp( |
||||
r'^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$', |
||||
); |
||||
|
||||
bool isBase64(String str) { |
||||
return _base64.hasMatch(str); |
||||
} |
||||
|
||||
bool isImageBase64(String imageUrl) { |
||||
return !imageUrl.startsWith('http') && isBase64(imageUrl); |
||||
} |
||||
|
||||
enum SaveImageResultMethod { network, localStorage } |
||||
|
||||
class _SaveImageResult { |
||||
const _SaveImageResult({required this.isSuccess, required this.method}); |
||||
|
||||
final bool isSuccess; |
||||
final SaveImageResultMethod method; |
||||
} |
||||
|
||||
Future<_SaveImageResult> saveImage(String imageUrl) async { |
||||
final imageFile = File(imageUrl); |
||||
final imageExistsLocally = await imageFile.exists(); |
||||
if (!imageExistsLocally) { |
||||
final success = await _saveNetworkImageToLocal(imageUrl); |
||||
return _SaveImageResult( |
||||
isSuccess: success, |
||||
method: SaveImageResultMethod.network, |
||||
); |
||||
} |
||||
final success = await _saveImageLocally(imageFile); |
||||
return _SaveImageResult( |
||||
isSuccess: success, |
||||
method: SaveImageResultMethod.localStorage, |
||||
); |
||||
} |
||||
|
||||
Future<bool> _saveNetworkImageToLocal(String imageUrl) async { |
||||
try { |
||||
final response = await http.get( |
||||
Uri.parse(imageUrl), |
||||
); |
||||
if (response.statusCode != 200) { |
||||
return false; |
||||
} |
||||
final imageBytes = response.bodyBytes; |
||||
final result = await ImageGallerySaver.saveImage( |
||||
Uint8List.fromList(imageBytes), |
||||
); |
||||
return result['isSuccess']; |
||||
} catch (e) { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
Future<Uint8List> _convertFileToUint8List(File file) async { |
||||
try { |
||||
final uint8list = await file.readAsBytes(); |
||||
return uint8list; |
||||
} catch (e) { |
||||
return Uint8List(0); |
||||
} |
||||
} |
||||
|
||||
Future<bool> _saveImageLocally(File imageFile) async { |
||||
try { |
||||
final imageBytes = await _convertFileToUint8List(imageFile); |
||||
final result = await ImageGallerySaver.saveImage(imageBytes); |
||||
|
||||
return result['isSuccess']; |
||||
} catch (e) { |
||||
return false; |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue