Last active
February 8, 2024 16:01
-
-
Save mbaka-bilal/a80892ac963c3d3fd0b2e83c5bb20b47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:dio/dio.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_dotenv/flutter_dotenv.dart'; | |
import 'package:path/path.dart'; | |
import '../model/cloudinary_upload_model.dart'; | |
import '../utils/app_constants.dart'; | |
import '../utils/utility.dart'; | |
Future<NetworkResponse> uploadAssets( | |
{required List<String> assets, required String folderPath}) async { | |
if (assets.isEmpty){ | |
return const NetworkResponse(status: RequestStatus.success,data: "Empty"); | |
} | |
final assetsBytes = []; | |
for (String e in assets){ | |
if (e != ""){ | |
try{ | |
assetsBytes.add(await MultipartFile.fromFile(e, filename: basename(e))); | |
}catch(e){ | |
debugPrint("Error $e"); | |
return NetworkResponse(status: RequestStatus.failed,data: e.toString()); | |
} | |
} | |
} | |
final response = await networkRequest(() async { | |
final formData = FormData.fromMap({ | |
"upload_preset": dotenv.env["UPLOAD_PRESET"], | |
"api_key": dotenv.env["CLOUDINARY_API_KEY"], | |
"folder": folderPath, | |
"file": assetsBytes, | |
}); | |
final response = await dio.post( | |
"https://api.cloudinary.com/v1_1/${dotenv.env["CLOUD_NAME"]}/image/upload", | |
data: formData, | |
); | |
print("status code is ${response.statusCode}"); | |
print("upload image response is:::: ${response.data}"); | |
if (response.statusCode == 200) { | |
final map = CloudinaryUploadModel.fromJson(response.data); | |
return NetworkResponse(status: RequestStatus.success,data: map); | |
} else { | |
return const NetworkResponse( | |
status: RequestStatus.failed, data: unknownErrorException); | |
} | |
}) as NetworkResponse; | |
return response; | |
} | |
class CloudinaryUploadModel { | |
String? assetId; | |
String? publicId; | |
int? version; | |
String? versionId; | |
String? signature; | |
int? width; | |
int? height; | |
String? format; | |
String? resourceType; | |
String? createdAt; | |
dynamic tags; | |
int? bytes; | |
String? type; | |
String? etag; | |
bool? placeholder; | |
String? url; | |
String? secureUrl; | |
String? folder; | |
String? originalFilename; | |
CloudinaryUploadModel( | |
{this.assetId, | |
this.publicId, | |
this.version, | |
this.versionId, | |
this.signature, | |
this.width, | |
this.height, | |
this.format, | |
this.resourceType, | |
this.createdAt, | |
this.tags, | |
this.bytes, | |
this.type, | |
this.etag, | |
this.placeholder, | |
this.url, | |
this.secureUrl, | |
this.folder, | |
this.originalFilename}); | |
CloudinaryUploadModel.fromJson(Map<String, dynamic> json) { | |
assetId = json['asset_id']; | |
publicId = json['public_id']; | |
version = json['version']; | |
versionId = json['version_id']; | |
signature = json['signature']; | |
width = json['width']; | |
height = json['height']; | |
format = json['format']; | |
resourceType = json['resource_type']; | |
createdAt = json['created_at']; | |
tags = json['tags']; | |
bytes = json['bytes']; | |
type = json['type']; | |
etag = json['etag']; | |
placeholder = json['placeholder']; | |
url = json['url']; | |
secureUrl = json['secure_url']; | |
folder = json['folder']; | |
originalFilename = json['original_filename']; | |
} | |
Map<String, dynamic> toJson() { | |
final Map<String, dynamic> data = <String, dynamic>{}; | |
data['asset_id'] = assetId; | |
data['public_id'] = publicId; | |
data['version'] = version; | |
data['version_id'] = versionId; | |
data['signature'] = signature; | |
data['width'] = width; | |
data['height'] = height; | |
data['format'] = format; | |
data['resource_type'] = resourceType; | |
data['created_at'] = createdAt; | |
if (tags != null) { | |
data['tags'] = tags!.map((v) => v.toJson()).toList(); | |
} | |
data['bytes'] = bytes; | |
data['type'] = type; | |
data['etag'] = etag; | |
data['placeholder'] = placeholder; | |
data['url'] = url; | |
data['secure_url'] = secureUrl; | |
data['folder'] = folder; | |
data['original_filename'] = originalFilename; | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment