-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #221 from takenet/release/0.0.84
Created new version
- Loading branch information
Showing
20 changed files
with
398 additions
and
240 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 55 additions & 20 deletions
75
lib/src/controllers/chat/ds_image_message_bubble.controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,69 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:blip_ds/src/utils/ds_directory_formatter.util.dart'; | ||
import 'package:crypto/crypto.dart'; | ||
import 'package:get/get.dart'; | ||
|
||
import '../../services/ds_auth.service.dart'; | ||
import '../../services/ds_file.service.dart'; | ||
|
||
class DSImageMessageBubbleController extends GetxController { | ||
Future<ImageInfo> getImageInfo({ | ||
required final String url, | ||
final bool shouldAuthenticate = false, | ||
}) async { | ||
final Image img = Image.network( | ||
url, | ||
headers: shouldAuthenticate ? DSAuthService.httpHeaders : null, | ||
); | ||
final maximumProgress = RxInt(0); | ||
final downloadProgress = RxInt(0); | ||
final localPath = RxnString(); | ||
|
||
final String url; | ||
final String? mediaType; | ||
final bool shouldAuthenticate; | ||
|
||
final completer = Completer<ImageInfo>(); | ||
DSImageMessageBubbleController( | ||
this.url, { | ||
this.mediaType, | ||
this.shouldAuthenticate = false, | ||
}) { | ||
_downloadImage(); | ||
} | ||
|
||
void _onReceiveProgress(final int currentProgress, final int maxProgres) { | ||
downloadProgress.value = currentProgress; | ||
maximumProgress.value = maxProgres; | ||
} | ||
|
||
final ImageStream imageStream = | ||
img.image.resolve(const ImageConfiguration()); | ||
Future<void> _downloadImage() async { | ||
if (mediaType == null || !url.startsWith('http')) { | ||
localPath.value = url; | ||
return; | ||
} | ||
|
||
imageStream.addListener( | ||
ImageStreamListener( | ||
(ImageInfo i, bool _) { | ||
completer.complete(i); | ||
}, | ||
onError: (exception, stackTrace) => completer.completeError(exception), | ||
), | ||
final uri = Uri.parse(url); | ||
|
||
final fullPath = await DSDirectoryFormatter.getPath( | ||
type: mediaType!, | ||
fileName: md5.convert(utf8.encode(uri.path)).toString(), | ||
); | ||
|
||
return completer.future; | ||
if (await File(fullPath).exists()) { | ||
localPath.value = fullPath; | ||
return; | ||
} | ||
|
||
final fileName = fullPath.split('/').last; | ||
final path = fullPath.substring(0, fullPath.lastIndexOf('/')); | ||
|
||
try { | ||
final savedFilePath = await DSFileService.download( | ||
url, | ||
fileName, | ||
path: path, | ||
onReceiveProgress: _onReceiveProgress, | ||
httpHeaders: shouldAuthenticate ? DSAuthService.httpHeaders : null, | ||
); | ||
|
||
localPath.value = savedFilePath; | ||
} catch (_) { | ||
localPath.value = url; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:get/get.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
|
||
abstract class DSDirectoryFormatter { | ||
static Future<String> getPath({ | ||
required final String type, | ||
required final String fileName, | ||
}) async { | ||
final cachePath = (await getApplicationCacheDirectory()).path; | ||
|
||
final typeFolder = '${type.split('/').first.capitalizeFirst}'; | ||
final extension = type.split('/').last; | ||
|
||
final typePrefix = '${typeFolder.substring(0, 3).toUpperCase()}-'; | ||
|
||
final newFileName = | ||
'${!fileName.startsWith(typePrefix) ? typePrefix : ''}$fileName'; | ||
|
||
final path = await _formatDirectory( | ||
type: typeFolder, | ||
directory: cachePath, | ||
); | ||
|
||
return '$path/$newFileName.$extension'; | ||
} | ||
|
||
static Future<String> _formatDirectory({ | ||
required final String type, | ||
required final String directory, | ||
}) async { | ||
final formattedDirectory = '$directory/$type'; | ||
final directoryExists = await Directory(formattedDirectory).exists(); | ||
|
||
if (!directoryExists) { | ||
await Directory(formattedDirectory).create(recursive: true); | ||
} | ||
|
||
return formattedDirectory; | ||
} | ||
} |
Oops, something went wrong.