mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 00:05:16 +00:00
Show message files
This commit is contained in:
46
lib/helpers/conversation_files_helper.dart
Normal file
46
lib/helpers/conversation_files_helper.dart
Normal file
@ -0,0 +1,46 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:comunic/models/conversation_message.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
/// Conversation files helper
|
||||
///
|
||||
/// @author Pierre Hubert
|
||||
|
||||
class ConversationFilesHelper {
|
||||
/// Get the path for chat file
|
||||
static Future<File> getPathForChatFile(
|
||||
int msgID, ConversationMessageFile fileInfo) async {
|
||||
Directory basePath = await getTemporaryDirectory();
|
||||
|
||||
final storageDir = path.join(basePath.absolute.path, "conversation-files");
|
||||
final fileName = "$msgID";
|
||||
|
||||
return File(path.join(storageDir, fileName));
|
||||
}
|
||||
|
||||
/// Download chat file
|
||||
static Future<void> download({
|
||||
@required int msgID,
|
||||
@required ConversationMessageFile fileInfo,
|
||||
@required Function(double) onProgress,
|
||||
@required CancelToken cancelToken,
|
||||
}) async {
|
||||
final target = await getPathForChatFile(msgID, fileInfo);
|
||||
|
||||
// Create parent directory if required
|
||||
if (!await target.parent.exists()) {
|
||||
await target.parent.create(recursive: true);
|
||||
}
|
||||
|
||||
await Dio().download(
|
||||
fileInfo.url,
|
||||
target.path,
|
||||
cancelToken: cancelToken,
|
||||
onReceiveProgress: (p, t) => onProgress(p / fileInfo.size.toDouble()),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user