Line data Source code
1 : import 'dart:io';
2 :
3 : import 'package:archive/archive_io.dart';
4 : import 'package:at_client/src/stream/file_transfer_object.dart';
5 : import 'package:at_client/src/util/constants.dart';
6 : import 'package:http/http.dart' as http;
7 :
8 : class FileTransferService {
9 0 : Future<dynamic> uploadToFileBin(
10 : List<int> file, String container, String fileName) async {
11 : try {
12 0 : var response = await http.post(
13 0 : Uri.parse(TextConstants.fileBinURL + '$container/' + fileName),
14 : body: file,
15 : );
16 : return response;
17 : } catch (e) {
18 : rethrow;
19 : }
20 : }
21 :
22 0 : Future<FileDownloadResponse> downloadFromFileBin(
23 : FileTransferObject fileTransferObject, String downloadPath) async {
24 : try {
25 0 : var response = await http.get(Uri.parse(fileTransferObject.fileUrl));
26 0 : if (response.statusCode != 200) {
27 0 : return FileDownloadResponse(
28 : isError: true, errorMsg: 'error in fetching data');
29 : }
30 0 : var archive = ZipDecoder().decodeBytes(response.bodyBytes);
31 :
32 : var tempDirectory =
33 0 : await Directory(downloadPath).createTemp('encrypted-files');
34 0 : for (var file in archive) {
35 0 : var unzippedFile = file.content as List<int>;
36 0 : var encryptedFile = File(tempDirectory.path + '/' + file.name);
37 0 : encryptedFile.writeAsBytesSync(unzippedFile);
38 : }
39 :
40 0 : return FileDownloadResponse(filePath: tempDirectory.path);
41 : } catch (e) {
42 0 : print('error in downloading file: $e');
43 0 : return FileDownloadResponse(isError: true, errorMsg: e.toString());
44 : }
45 : }
46 : }
|