mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-02 04:22:04 -04:00
Upload text to Zipline (if enabled) for very large image lists
This commit is contained in:
@@ -10,6 +10,26 @@ namespace KfChatDotNetBot.Services;
|
||||
public static class Zipline
|
||||
{
|
||||
public static async Task<string?> Upload(Stream content, MediaTypeHeaderValue mimeType, string? expiration = null, CancellationToken ct = default)
|
||||
{
|
||||
using var formContent = new MultipartFormDataContent();
|
||||
var fileContent = new StreamContent(content);
|
||||
fileContent.Headers.ContentType = mimeType;
|
||||
formContent.Add(fileContent, "upload", Money.GenerateEventId());
|
||||
var url = await DoUpload(formContent, expiration, ct);
|
||||
return url;
|
||||
}
|
||||
|
||||
public static async Task<string?> Upload(string content, MediaTypeHeaderValue mimeType, string? expiration = null, CancellationToken ct = default)
|
||||
{
|
||||
using var formContent = new MultipartFormDataContent();
|
||||
var fileContent = new StringContent(content);
|
||||
fileContent.Headers.ContentType = mimeType;
|
||||
formContent.Add(fileContent, "upload", Money.GenerateEventId());
|
||||
var url = await DoUpload(formContent, expiration, ct);
|
||||
return url;
|
||||
}
|
||||
|
||||
private static async Task<string?> DoUpload(MultipartFormDataContent content, string? expiration = null, CancellationToken ct = default)
|
||||
{
|
||||
var logger = LogManager.GetCurrentClassLogger();
|
||||
var settings =
|
||||
@@ -30,17 +50,13 @@ public static class Zipline
|
||||
}
|
||||
|
||||
using var client = new HttpClient(handler);
|
||||
using var formContent = new MultipartFormDataContent();
|
||||
var fileContent = new StreamContent(content);
|
||||
fileContent.Headers.ContentType = mimeType;
|
||||
formContent.Add(fileContent, "upload", Money.GenerateEventId());
|
||||
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", settings[BuiltIn.Keys.ZiplineKey].Value);
|
||||
if (expiration != null)
|
||||
{
|
||||
client.DefaultRequestHeaders.Add("x-zipline-expiration", expiration);
|
||||
}
|
||||
|
||||
var response = await client.PostAsync($"{settings[BuiltIn.Keys.ZiplineUrl].Value}/api/upload", formContent, ct);
|
||||
var response = await client.PostAsync($"{settings[BuiltIn.Keys.ZiplineUrl].Value}/api/upload", content, ct);
|
||||
var json = await response.Content.ReadFromJsonAsync<JsonElement>(cancellationToken: ct);
|
||||
string url;
|
||||
try
|
||||
@@ -58,4 +74,10 @@ public static class Zipline
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
public static async Task<bool> IsZiplineEnabled()
|
||||
{
|
||||
var key = await SettingsProvider.GetValueAsync(BuiltIn.Keys.ZiplineKey);
|
||||
return !string.IsNullOrEmpty(key.Value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user