dartlangeditorflutterflutter-appsflutter-examplesflutter-packageflutter-widgetquillquill-deltaquilljsreactquillrich-textrich-text-editorwysiwygwysiwyg-editor
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.
93 lines
2.4 KiB
93 lines
2.4 KiB
import 'dart:io' show File; |
|
|
|
import 'package:flutter/foundation.dart' show immutable; |
|
import 'package:flutter/widgets.dart' show BuildContext; |
|
import '../../logic/models/config/configurations.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 isHttpBasedUrl(String url) { |
|
try { |
|
final uri = Uri.parse(url.trim()); |
|
return uri.isScheme('HTTP') || uri.isScheme('HTTPS'); |
|
} catch (_) { |
|
return false; |
|
} |
|
} |
|
|
|
bool isYouTubeUrl(String videoUrl) { |
|
try { |
|
final uri = Uri.parse(videoUrl); |
|
return uri.host == 'www.youtube.com' || |
|
uri.host == 'youtube.com' || |
|
uri.host == 'youtu.be'; |
|
} catch (_) { |
|
return false; |
|
} |
|
} |
|
|
|
bool isImageBase64(String imageUrl) { |
|
return !isHttpBasedUrl(imageUrl) && isBase64(imageUrl); |
|
} |
|
|
|
enum SaveImageResultMethod { network, localStorage } |
|
|
|
@immutable |
|
class SaveImageResult { |
|
const SaveImageResult({required this.isSuccess, required this.method}); |
|
|
|
final bool isSuccess; |
|
final SaveImageResultMethod method; |
|
} |
|
|
|
Future<SaveImageResult> saveImage({ |
|
required String imageUrl, |
|
required BuildContext context, |
|
}) async { |
|
final imageSaverService = |
|
QuillSharedExtensionsConfigurations.get(context: context) |
|
.imageSaverService; |
|
final imageFile = File(imageUrl); |
|
final hasPermission = await imageSaverService.hasAccess(); |
|
if (!hasPermission) { |
|
await imageSaverService.requestAccess(); |
|
} |
|
final imageExistsLocally = await imageFile.exists(); |
|
if (!imageExistsLocally) { |
|
try { |
|
await imageSaverService.saveImageFromNetwork( |
|
Uri.parse(imageUrl), |
|
); |
|
return const SaveImageResult( |
|
isSuccess: true, |
|
method: SaveImageResultMethod.network, |
|
); |
|
} catch (e) { |
|
return const SaveImageResult( |
|
isSuccess: false, |
|
method: SaveImageResultMethod.network, |
|
); |
|
} |
|
} |
|
try { |
|
await imageSaverService.saveLocalImage(imageUrl); |
|
return const SaveImageResult( |
|
isSuccess: true, |
|
method: SaveImageResultMethod.localStorage, |
|
); |
|
} catch (e) { |
|
return const SaveImageResult( |
|
isSuccess: false, |
|
method: SaveImageResultMethod.localStorage, |
|
); |
|
} |
|
}
|
|
|