-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImgBBApi.cs
50 lines (43 loc) · 2.5 KB
/
ImgBBApi.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace Iminetsoft
{
public class ImgBBApi
{
public string ApiKey { get; set; } = String.Empty;
public ImgBBApi() { }
public ImgBBApi(string apikey) { ApiKey = apikey; }
public async Task<List<string>> UploadImgbbAsync(List<string> ImagePaths)
{
List<string> LinksToUploadedImages = new List<string>();
string ResponseJsonContent = string.Empty;
using (HttpClient client = new HttpClient())
{
try
{
foreach (string imagePath in ImagePaths)
{
/* SORGU GONDERDIK VE JSON RESPONSE-U ELDE ETDIK: */
string requestUrl = $"https://api.imgbb.com/1/upload?key={ApiKey}";
MultipartFormDataContent content = new MultipartFormDataContent();
string base64OfImage = Convert.ToBase64String(File.ReadAllBytes(imagePath));
content.Add(new StringContent(base64OfImage), "image");
content.Add(new StringContent(Path.GetFileNameWithoutExtension(imagePath)), "name");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl) { Content = content };
HttpResponseMessage response = await client.SendAsync(request);
ResponseJsonContent = await response.Content.ReadAsStringAsync();
/* JSON RESPONSE-DAN LAZIMI HISSELERI EXTRACT EDEK: */
JObject jsonObject = JObject.Parse(ResponseJsonContent);
LinksToUploadedImages.Add(jsonObject.SelectToken("data.url").ToString());
}
}
catch { }
}
return LinksToUploadedImages;
}
}
}