uploadPhoto method

  1. @override
Future<UploadPhoto> uploadPhoto({
  1. required String distinctId,
  2. File? photo,
  3. String? imageUrl,
  4. String? imageName,
  5. String? title,
  6. String? category,
})
override

Implementation

@override
Future<UploadPhoto> uploadPhoto({
  required String distinctId,
  File? photo,
  String? imageUrl,
  String? imageName,
  String? title,
  String? category,
}) async {
  if (photo == null && (imageUrl == null || imageUrl.isEmpty)) {
    throw ArgumentError('uploadPhoto requiere `photo` o `imageUrl`.');
  }
  if (photo != null && !UploadPhoto.isAllowed(photo.path)) {
    throw ArgumentError.value(
      photo.path,
      'photo',
      'Formato no permitido. Usa: ${UploadPhoto.allowedExtensions.join(', ')}',
    );
  }
  final response = await dio.post(
    '/photos',
    data: FormData.fromMap({
      'campaign': Environment.campaign,
      'api_key': Environment.apiKey,
      'distinct_id': distinctId,
      if (photo == null) 'image_url': imageUrl,
      'image_name': ?imageName,
      'title': ?title,
      'category': ?category,
      if (photo != null)
        'upload_photo': await MultipartFile.fromFile(
          photo.path,
          filename: photo.path.split('/').last,
        ),
    }),
  );
  final raw = response.data;
  final Map<String, dynamic> responseData =
      raw is String ? json.decode(raw) : Map<String, dynamic>.from(raw as Map);
  return _jsonToUploadPhoto(responseData);
}