133 lines
4.4 KiB
Dart
133 lines
4.4 KiB
Dart
// ignore_for_file: depend_on_referenced_packages
|
|
|
|
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:didvan/models/ai/files_model.dart';
|
|
import 'package:didvan/services/storage/storage.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:get/get_connect/http/src/multipart/multipart_file.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:mime/mime.dart';
|
|
import 'package:path/path.dart' as p;
|
|
import 'package:http_parser/http_parser.dart' as parser;
|
|
|
|
class AiApiService {
|
|
static const String baseUrl = 'https://api.didvan.app/ai';
|
|
|
|
static Future<http.MultipartRequest> initial(
|
|
{required final String url,
|
|
required final String message,
|
|
final int? chatId,
|
|
final FilesModel? file,
|
|
final bool? edite}) async {
|
|
final headers = {
|
|
"Authorization": "Bearer ${await StorageService.getValue(key: 'token')}",
|
|
'Content-Type': 'multipart/form-data'
|
|
};
|
|
|
|
var request = http.MultipartRequest('POST', Uri.parse(baseUrl + url))
|
|
..headers.addAll(headers);
|
|
|
|
request.fields['prompt'] = message;
|
|
if (chatId != null) {
|
|
request.fields['chatId'] = chatId.toString();
|
|
}
|
|
if (edite != null) {
|
|
request.fields['edit'] = edite.toString().toLowerCase();
|
|
}
|
|
if (file != null) {
|
|
// final file = file;
|
|
// final filePath = file.path;
|
|
// final mimeType = filePath != null ? lookupMimeType(filePath) : null;
|
|
// final contentType = mimeType != null ? MediaType.parse(mimeType) : null;
|
|
|
|
// final fileReadStream = file.readStream;
|
|
// if (fileReadStream == null) {
|
|
// throw Exception('Cannot read file from null stream');
|
|
// }
|
|
// final stream = http.ByteStream(fileReadStream);
|
|
// final multipartFile = http.MultipartFile(
|
|
// 'file',
|
|
// stream,
|
|
// file.size,
|
|
// filename: file.name,
|
|
// contentType: contentType,
|
|
// );
|
|
// request.files.add(multipartFile);
|
|
Uint8List bytes;
|
|
if (kIsWeb) {
|
|
if (file.bytes != null) {
|
|
bytes = file.bytes!;
|
|
} else {
|
|
final Uri audioUri = Uri.parse(file.path);
|
|
final http.Response audioResponse = await http.get(audioUri);
|
|
bytes = audioResponse.bodyBytes;
|
|
}
|
|
request.files.add(http.MultipartFile.fromBytes(
|
|
'file',
|
|
bytes,
|
|
filename: file.isAudio()
|
|
? 'wav'
|
|
: file.isImage()
|
|
? 'png'
|
|
: 'pdf', // You can set a filename here
|
|
contentType: parser.MediaType.parse(file.isAudio()
|
|
? 'audio/mpeg'
|
|
: file.isImage()
|
|
? 'image/png'
|
|
: 'application/pdf'), // Set the MIME type
|
|
) // Use MediaType.parse to parse the MIME type
|
|
);
|
|
} else {
|
|
int length = 0;
|
|
try {
|
|
length = await file.main.length();
|
|
// ...
|
|
} catch (e) {
|
|
// Handle the error or return an error response
|
|
}
|
|
String? mimeType = lookupMimeType(
|
|
file.path); // Use MIME type instead of file extension
|
|
mimeType ??= 'application/octet-stream';
|
|
if (mimeType.startsWith('audio')) {
|
|
mimeType = 'audio/${p.extension(file.path).replaceAll('.', '')}';
|
|
}
|
|
request.files.add(
|
|
http.MultipartFile('file', file.main.readAsBytes().asStream(), length,
|
|
filename: file.basename,
|
|
contentType: parser.MediaType.parse(
|
|
mimeType)), // Use MediaType.parse to parse the MIME type
|
|
);
|
|
}
|
|
}
|
|
|
|
// print("req: ${request.files}");
|
|
// print("req: ${request.fields}");
|
|
|
|
return request;
|
|
}
|
|
|
|
static Future<Stream<List<int>>> getResponse(
|
|
http.MultipartRequest req) async {
|
|
try {
|
|
final response = await http.Client().send(req);
|
|
if (response.statusCode == 400) {
|
|
// Handle 400 response
|
|
final errorResponse = await response.stream.bytesToString();
|
|
final errorJson = jsonDecode(errorResponse);
|
|
throw Exception(errorJson['error'] ?? 'Bad Request');
|
|
} else if (response.statusCode != 200) {
|
|
// Handle other non-200 responses
|
|
throw Exception('Failed to load data');
|
|
} else {
|
|
return response.stream.asBroadcastStream();
|
|
}
|
|
} catch (e) {
|
|
// Handle any other errors
|
|
throw Exception('Failed to load data');
|
|
}
|
|
}
|
|
}
|