TF-725 Download inline image as base64 data
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
|
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:core/core.dart';
|
import 'package:core/core.dart';
|
||||||
@@ -26,4 +27,15 @@ class DownloadClient {
|
|||||||
|
|
||||||
return (responseBody as ResponseBody);
|
return (responseBody as ResponseBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<String?> downloadImageAsBase64(String url) async {
|
||||||
|
try {
|
||||||
|
final response = await _dioClient.get(
|
||||||
|
url,
|
||||||
|
options: Options(responseType: ResponseType.bytes));
|
||||||
|
return base64Encode(response);
|
||||||
|
} catch (e) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
abstract class ComposerDataSource {
|
||||||
|
Future<String?> downloadImageAsBase64(String url);
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
import 'package:core/core.dart';
|
||||||
|
import 'package:tmail_ui_user/features/composer/data/datasource/composer_datasource.dart';
|
||||||
|
|
||||||
|
class ComposerDataSourceImpl extends ComposerDataSource {
|
||||||
|
|
||||||
|
final DownloadClient downloadClient;
|
||||||
|
|
||||||
|
ComposerDataSourceImpl(this.downloadClient);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<String?> downloadImageAsBase64(String url) {
|
||||||
|
return downloadClient.downloadImageAsBase64(url);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:model/upload/file_info.dart';
|
import 'package:model/upload/file_info.dart';
|
||||||
|
import 'package:tmail_ui_user/features/composer/data/datasource/composer_datasource.dart';
|
||||||
import 'package:tmail_ui_user/features/composer/domain/repository/composer_repository.dart';
|
import 'package:tmail_ui_user/features/composer/domain/repository/composer_repository.dart';
|
||||||
import 'package:tmail_ui_user/features/upload/data/datasource/attachment_upload_datasource.dart';
|
import 'package:tmail_ui_user/features/upload/data/datasource/attachment_upload_datasource.dart';
|
||||||
import 'package:tmail_ui_user/features/upload/domain/model/upload_attachment.dart';
|
import 'package:tmail_ui_user/features/upload/domain/model/upload_attachment.dart';
|
||||||
@@ -8,11 +9,19 @@ import 'package:tmail_ui_user/features/upload/domain/model/upload_attachment.dar
|
|||||||
class ComposerRepositoryImpl extends ComposerRepository {
|
class ComposerRepositoryImpl extends ComposerRepository {
|
||||||
|
|
||||||
final AttachmentUploadDataSource _attachmentUploadDataSource;
|
final AttachmentUploadDataSource _attachmentUploadDataSource;
|
||||||
|
final ComposerDataSource _composerDataSource;
|
||||||
|
|
||||||
ComposerRepositoryImpl(this._attachmentUploadDataSource);
|
ComposerRepositoryImpl(
|
||||||
|
this._attachmentUploadDataSource,
|
||||||
|
this._composerDataSource);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
UploadAttachment uploadAttachment(FileInfo fileInfo, Uri uploadUri, {CancelToken? cancelToken}) {
|
UploadAttachment uploadAttachment(FileInfo fileInfo, Uri uploadUri, {CancelToken? cancelToken}) {
|
||||||
return _attachmentUploadDataSource.uploadAttachment(fileInfo, uploadUri, cancelToken: cancelToken);
|
return _attachmentUploadDataSource.uploadAttachment(fileInfo, uploadUri, cancelToken: cancelToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<String?> downloadImageAsBase64(String url) {
|
||||||
|
return _composerDataSource.downloadImageAsBase64(url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -4,4 +4,6 @@ import 'package:tmail_ui_user/features/upload/domain/model/upload_attachment.dar
|
|||||||
|
|
||||||
abstract class ComposerRepository {
|
abstract class ComposerRepository {
|
||||||
UploadAttachment uploadAttachment(FileInfo fileInfo, Uri uploadUri, {CancelToken? cancelToken});
|
UploadAttachment uploadAttachment(FileInfo fileInfo, Uri uploadUri, {CancelToken? cancelToken});
|
||||||
|
|
||||||
|
Future<String?> downloadImageAsBase64(String url);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
|
||||||
|
import 'package:core/presentation/state/failure.dart';
|
||||||
|
import 'package:core/presentation/state/success.dart';
|
||||||
|
import 'package:model/upload/file_info.dart';
|
||||||
|
|
||||||
|
class DownloadingImageAsBase64 extends UIState {
|
||||||
|
|
||||||
|
DownloadingImageAsBase64();
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [];
|
||||||
|
}
|
||||||
|
|
||||||
|
class DownloadImageAsBase64Success extends UIState {
|
||||||
|
|
||||||
|
final String imageAsBase64;
|
||||||
|
final String cid;
|
||||||
|
final FileInfo fileInfo;
|
||||||
|
|
||||||
|
DownloadImageAsBase64Success(this.imageAsBase64, this.cid, this.fileInfo);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [imageAsBase64, cid, fileInfo];
|
||||||
|
}
|
||||||
|
|
||||||
|
class DownloadImageAsBase64Failure extends FeatureFailure {
|
||||||
|
final dynamic exception;
|
||||||
|
|
||||||
|
DownloadImageAsBase64Failure(this.exception);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [exception];
|
||||||
|
}
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
import 'package:core/core.dart';
|
|
||||||
import 'package:dartz/dartz.dart';
|
|
||||||
import 'package:model/model.dart';
|
|
||||||
|
|
||||||
class UploadingAttachmentState extends UIState {
|
|
||||||
UploadingAttachmentState();
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object?> get props => [];
|
|
||||||
}
|
|
||||||
|
|
||||||
class UploadAttachmentSuccess extends UIState {
|
|
||||||
final Attachment attachment;
|
|
||||||
|
|
||||||
UploadAttachmentSuccess(this.attachment);
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object?> get props => [attachment];
|
|
||||||
}
|
|
||||||
|
|
||||||
class UploadAttachmentFailure extends FeatureFailure {
|
|
||||||
final dynamic exception;
|
|
||||||
|
|
||||||
UploadAttachmentFailure(this.exception);
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object> get props => [exception];
|
|
||||||
}
|
|
||||||
|
|
||||||
class UploadMultipleAttachmentAllSuccess extends UIState {
|
|
||||||
final List<Either<Failure, Success>> listResults;
|
|
||||||
|
|
||||||
UploadMultipleAttachmentAllSuccess(this.listResults);
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object?> get props => [listResults];
|
|
||||||
}
|
|
||||||
|
|
||||||
class UploadMultipleAttachmentHasSomeFailure extends UIState {
|
|
||||||
final List<Either<Failure, Success>> listResults;
|
|
||||||
|
|
||||||
UploadMultipleAttachmentHasSomeFailure(this.listResults);
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object?> get props => [listResults];
|
|
||||||
}
|
|
||||||
|
|
||||||
class UploadMultipleAttachmentAllFailure extends FeatureFailure {
|
|
||||||
final List<Either<Failure, Success>> listResults;
|
|
||||||
|
|
||||||
UploadMultipleAttachmentAllFailure(this.listResults);
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object> get props => [listResults];
|
|
||||||
}
|
|
||||||
|
|
||||||
class UploadMultipleAttachmentFailure extends FeatureFailure {
|
|
||||||
final exception;
|
|
||||||
|
|
||||||
UploadMultipleAttachmentFailure(this.exception);
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object> get props => [exception];
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import 'package:core/core.dart';
|
||||||
|
import 'package:dartz/dartz.dart';
|
||||||
|
import 'package:model/model.dart';
|
||||||
|
import 'package:tmail_ui_user/features/composer/domain/repository/composer_repository.dart';
|
||||||
|
import 'package:tmail_ui_user/features/composer/domain/state/download_image_as_base64_state.dart';
|
||||||
|
|
||||||
|
class DownloadImageAsBase64Interactor {
|
||||||
|
final ComposerRepository _composerRepository;
|
||||||
|
|
||||||
|
DownloadImageAsBase64Interactor(this._composerRepository);
|
||||||
|
|
||||||
|
Stream<Either<Failure, Success>> execute(String url, String cid, FileInfo fileInfo) async* {
|
||||||
|
try {
|
||||||
|
yield Right<Failure, Success>(DownloadingImageAsBase64());
|
||||||
|
final result = await _composerRepository.downloadImageAsBase64(url);
|
||||||
|
if (result?.isNotEmpty == true) {
|
||||||
|
yield Right<Failure, Success>(DownloadImageAsBase64Success(result!, cid, fileInfo));
|
||||||
|
} else {
|
||||||
|
yield Left<Failure, Success>(DownloadImageAsBase64Failure(null));
|
||||||
|
}
|
||||||
|
} catch (exception) {
|
||||||
|
yield Left<Failure, Success>(DownloadImageAsBase64Failure(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user