passo_mobile_app/lib/utils/attachment_services.dart

155 lines
4.7 KiB
Dart
Raw Permalink Normal View History

import 'dart:convert';
2023-08-27 08:38:05 +00:00
import 'package:dio/dio.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:share_plus/share_plus.dart';
import 'package:unit2/utils/request.dart';
2023-08-27 08:38:05 +00:00
import 'package:unit2/utils/request_permission.dart';
import 'package:unit2/utils/urls.dart';
import '../model/profile/attachment.dart';
import 'package:http/http.dart' as http;
class AttachmentServices {
static final AttachmentServices _instance = AttachmentServices();
static AttachmentServices get instance => _instance;
Future<List<AttachmentCategory>> getCategories() async {
List<AttachmentCategory> attachmentCategories = [];
2023-08-27 08:38:05 +00:00
String path = Url.instance.attachmentCategories();
Map<String, String> headers = {
'Content-Type': 'application/json; charset=UTF-8',
};
try {
http.Response response = await Request.instance
.getRequest(param: {}, path: path, headers: headers);
if (response.statusCode == 200) {
Map data = jsonDecode(response.body);
for (var cat in data['data']) {
AttachmentCategory newCat = AttachmentCategory.fromJson(cat);
attachmentCategories.add(newCat);
}
}
} catch (e) {
throw e.toString();
}
return attachmentCategories;
}
2023-08-07 06:33:38 +00:00
Future<Map<dynamic, dynamic>> attachment(
{required String categoryId,
required String module,
required List<String> paths,
required String token,
required String profileId}) async {
String authtoken = "Token $token";
Map<String, String> headers = {'Authorization': authtoken};
String path = Url.instance.attachments();
Map<dynamic, dynamic>? response = {};
Map<String, String> body = {
"attachment_category_id": categoryId.toString(),
"attachment_module": module.toString()
};
try {
var request = http.MultipartRequest(
2023-08-07 06:36:58 +00:00
'POST', Uri.parse('https://${Url.instance.host()}$path$profileId/'));
request.fields.addAll(body);
request.headers.addAll(headers);
paths.forEach((element) async {
request.files
.add(await http.MultipartFile.fromPath('attachments', element));
});
http.StreamedResponse res = await request.send();
final steamResponse = await res.stream.bytesToString();
Map data = jsonDecode(steamResponse);
if (res.statusCode == 201) {
response = data;
} else {
String message = data['response']['details'];
response.addAll({'message': message});
response.addAll(
{'success': false},
);
}
} catch (e) {
throw e.toString();
}
return response;
}
2023-08-07 06:33:38 +00:00
Future<bool> deleteAttachment(
{required Attachment attachment,
required int moduleId,
required String profileId,
required String token}) async {
bool? success;
String authtoken = "Token $token";
String path = "${Url.instance.attachments()}$profileId/";
Map? body;
body = {
"attachment_module": moduleId,
"attachments": [
{
"id": attachment.id,
"created_at": attachment.createdAt?.toString(),
"source": attachment.source,
"filename": attachment.filename,
"category": {
"id": attachment.category?.id,
"subclass": {
"id": attachment.category?.subclass?.id,
"name": attachment.category?.subclass?.name,
"attachment_class": {
"id": attachment.category?.subclass?.attachmentClass?.id,
"name": attachment.category?.subclass?.attachmentClass?.name
}
},
"description": attachment.category?.description
}
}
]
};
Map<String, dynamic> params = {"force_mode": "true"};
Map<String, String> headers = {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': authtoken
};
2023-08-27 08:38:05 +00:00
try {
2023-08-07 06:33:38 +00:00
http.Response response = await Request.instance.deleteRequest(
path: path, headers: headers, body: body, param: params);
if (response.statusCode == 200) {
Map data = jsonDecode(response.body);
success = data['success'];
} else {
success = false;
}
2023-08-27 08:38:05 +00:00
} catch (e) {
throw (e.toString());
}
2023-08-07 06:33:38 +00:00
return success!;
}
2023-08-27 08:38:05 +00:00
Future<bool> downloadAttachment(
{required String filename,
required String source,
required String downLoadDir}) async {
bool success = false;
var dio = Dio();
Response response;
try {
if (await requestPermission(Permission.storage)) {
response = await dio.download(source, "$downLoadDir/$filename");
if (response.statusCode == 200) {
success = true;
}
}
return success;
} catch (e) {
throw e.toString();
}
}
}