mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
@ -12,13 +12,15 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
|||||||
import 'package:dartz/dartz.dart';
|
import 'package:dartz/dartz.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:path/path.dart' as path;
|
import 'package:path/path.dart' as p;
|
||||||
import 'change_cover_popover.dart';
|
import 'change_cover_popover.dart';
|
||||||
|
|
||||||
part 'cover_image_picker_bloc.freezed.dart';
|
part 'cover_image_picker_bloc.freezed.dart';
|
||||||
|
|
||||||
class CoverImagePickerBloc
|
class CoverImagePickerBloc
|
||||||
extends Bloc<CoverImagePickerEvent, CoverImagePickerState> {
|
extends Bloc<CoverImagePickerEvent, CoverImagePickerState> {
|
||||||
|
static const allowedExtensions = ['jpg', 'png', 'jpeg'];
|
||||||
|
|
||||||
CoverImagePickerBloc() : super(const CoverImagePickerState.initial()) {
|
CoverImagePickerBloc() : super(const CoverImagePickerState.initial()) {
|
||||||
on<CoverImagePickerEvent>(
|
on<CoverImagePickerEvent>(
|
||||||
(event, emit) async {
|
(event, emit) async {
|
||||||
@ -28,7 +30,7 @@ class CoverImagePickerBloc
|
|||||||
},
|
},
|
||||||
urlSubmit: (UrlSubmit urlSubmit) async {
|
urlSubmit: (UrlSubmit urlSubmit) async {
|
||||||
emit(const CoverImagePickerState.loading());
|
emit(const CoverImagePickerState.loading());
|
||||||
final validateImage = await _validateUrl(urlSubmit.path);
|
final validateImage = await _validateURL(urlSubmit.path);
|
||||||
if (validateImage) {
|
if (validateImage) {
|
||||||
emit(CoverImagePickerState.networkImage(left(urlSubmit.path)));
|
emit(CoverImagePickerState.networkImage(left(urlSubmit.path)));
|
||||||
} else {
|
} else {
|
||||||
@ -86,28 +88,22 @@ class CoverImagePickerBloc
|
|||||||
if (state is FileImagePicked) {
|
if (state is FileImagePicked) {
|
||||||
try {
|
try {
|
||||||
final path = state.path;
|
final path = state.path;
|
||||||
final newPath = '$directory/${path.split("\\").last}';
|
final newPath = p.join(directory, p.split(path).last);
|
||||||
final newFile = await File(path).copy(newPath);
|
final newFile = await File(path).copy(newPath);
|
||||||
imagePaths.add(newFile.path);
|
imagePaths.add(newFile.path);
|
||||||
await prefs.setStringList(kLocalImagesKey, imagePaths);
|
|
||||||
return imagePaths;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
} else if (state is NetworkImagePicked) {
|
} else if (state is NetworkImagePicked) {
|
||||||
try {
|
try {
|
||||||
String? url = state.successOrFail.fold((path) => path, (r) => null);
|
final url = state.successOrFail.fold((path) => path, (r) => null);
|
||||||
if (url != null) {
|
if (url != null) {
|
||||||
final response = await http.get(Uri.parse(url));
|
final response = await http.get(Uri.parse(url));
|
||||||
final newPath =
|
final newPath = p.join(directory, _networkImageName(url));
|
||||||
"$directory/IMG_$_timeStampString.${_getExtention(url)}";
|
|
||||||
|
|
||||||
final imageFile = File(newPath);
|
final imageFile = File(newPath);
|
||||||
await imageFile.create();
|
await imageFile.create();
|
||||||
await imageFile.writeAsBytes(response.bodyBytes);
|
await imageFile.writeAsBytes(response.bodyBytes);
|
||||||
imagePaths.add(imageFile.absolute.path);
|
imagePaths.add(imageFile.absolute.path);
|
||||||
await prefs.setStringList(kLocalImagesKey, imagePaths);
|
|
||||||
return imagePaths;
|
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -115,61 +111,73 @@ class CoverImagePickerBloc
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
await prefs.setStringList(kLocalImagesKey, imagePaths);
|
||||||
|
return imagePaths;
|
||||||
}
|
}
|
||||||
|
|
||||||
_pickImages() async {
|
Future<String?> _pickImages() async {
|
||||||
FilePickerResult? result = await getIt<FilePickerService>().pickFiles(
|
final result = await getIt<FilePickerService>().pickFiles(
|
||||||
dialogTitle: LocaleKeys.document_plugins_cover_addLocalImage.tr(),
|
dialogTitle: LocaleKeys.document_plugins_cover_addLocalImage.tr(),
|
||||||
allowMultiple: false,
|
allowMultiple: false,
|
||||||
type: fp.FileType.image,
|
type: fp.FileType.image,
|
||||||
allowedExtensions: ['jpg', 'png', 'jpeg'],
|
allowedExtensions: allowedExtensions,
|
||||||
);
|
);
|
||||||
if (result != null && result.files.isNotEmpty) {
|
if (result != null && result.files.isNotEmpty) {
|
||||||
final path = result.files.first.path;
|
return result.files.first.path;
|
||||||
if (path != null) {
|
|
||||||
return path;
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String> _coverPath() async {
|
Future<String> _coverPath() async {
|
||||||
final directory = await getIt<SettingsLocationCubit>().fetchLocation();
|
final directory = await getIt<SettingsLocationCubit>().fetchLocation();
|
||||||
return Directory(path.join(directory, 'covers'))
|
return Directory(p.join(directory, 'covers'))
|
||||||
.create(recursive: true)
|
.create(recursive: true)
|
||||||
.then((value) => value.path);
|
.then((value) => value.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
String get _timeStampString =>
|
String _networkImageName(String url) {
|
||||||
DateTime.now().millisecondsSinceEpoch.toString();
|
return 'IMG_${DateTime.now().millisecondsSinceEpoch.toString()}.${_getExtention(
|
||||||
|
url,
|
||||||
|
fromNetwork: true,
|
||||||
|
)}';
|
||||||
|
}
|
||||||
|
|
||||||
String? _getExtention(String path) => path.contains(".jpg")
|
String? _getExtention(
|
||||||
? "jpg"
|
String path, {
|
||||||
: path.contains(".png")
|
bool fromNetwork = false,
|
||||||
? "png"
|
}) {
|
||||||
: path.contains(".jpeg")
|
String? ext;
|
||||||
? "jpeg"
|
if (!fromNetwork) {
|
||||||
: (path.contains("auto=format") && path.contains("unsplash"))
|
final extension = p.extension(path);
|
||||||
? "jpeg"
|
if (extension.isEmpty) {
|
||||||
: null;
|
return null;
|
||||||
|
}
|
||||||
|
ext = extension.substring(1);
|
||||||
|
} else {
|
||||||
|
final uri = Uri.parse(path);
|
||||||
|
final paramters = uri.queryParameters;
|
||||||
|
final dl = paramters['dl'];
|
||||||
|
if (dl != null) {
|
||||||
|
ext = p.extension(dl).substring(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (allowedExtensions.contains(ext)) {
|
||||||
|
return ext;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
_validateUrl(String path) async {
|
Future<bool> _validateURL(String path) async {
|
||||||
if (_getExtention(path) != null) {
|
final extension = _getExtention(path, fromNetwork: true);
|
||||||
|
if (extension == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
final response = await http.get(Uri.parse(path));
|
final response = await http.head(Uri.parse(path));
|
||||||
if (response.statusCode == 200) {
|
return response.statusCode == 200;
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user