feat: CMS cover-art upload on track edit page
This commit is contained in:
@@ -241,9 +241,77 @@ public class CmsTrackService : ICmsTrackService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ResultContainer<string>> UploadImageAsync(
|
||||
Stream imageStream,
|
||||
string fileName,
|
||||
string contentType,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
using var multipart = new MultipartFormDataContent();
|
||||
var imageContent = new StreamContent(imageStream);
|
||||
imageContent.Headers.ContentType = new MediaTypeHeaderValue(
|
||||
string.IsNullOrWhiteSpace(contentType) ? "application/octet-stream" : contentType);
|
||||
multipart.Add(imageContent, "image", fileName);
|
||||
|
||||
var client = _httpClientFactory.CreateClient(ContentCmsClientName);
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, "api/image/upload") { Content = multipart };
|
||||
|
||||
HttpResponseMessage response;
|
||||
try
|
||||
{
|
||||
response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Content API call failed for image upload of {FileName}", fileName);
|
||||
return ResultContainer<string>.CreateFailResult("Content API is unreachable.");
|
||||
}
|
||||
|
||||
using (response)
|
||||
{
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var body = await response.Content.ReadAsStringAsync(ct);
|
||||
var statusCode = (int)response.StatusCode;
|
||||
if (statusCode >= 500)
|
||||
{
|
||||
_logger.LogError("Content API returned {Status} for image upload of {FileName}: {Body}", statusCode, fileName, body);
|
||||
return ResultContainer<string>.CreateFailResult("Image upload failed on the content server.");
|
||||
}
|
||||
|
||||
// 4xx: body is user-friendly validation text from DeepDrftAPI — relay as-is.
|
||||
_logger.LogWarning("Content API rejected image upload: {Status} {Body}", statusCode, body);
|
||||
return ResultContainer<string>.CreateFailResult(
|
||||
string.IsNullOrWhiteSpace(body) ? $"Image upload rejected ({statusCode})." : body);
|
||||
}
|
||||
|
||||
ImageUploadResponse? payload;
|
||||
try
|
||||
{
|
||||
payload = await response.Content.ReadFromJsonAsync<ImageUploadResponse>(ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to deserialize image upload response from Content API");
|
||||
return ResultContainer<string>.CreateFailResult("Content API returned an unexpected response.");
|
||||
}
|
||||
|
||||
if (payload is null || string.IsNullOrWhiteSpace(payload.EntryKey))
|
||||
{
|
||||
_logger.LogError("Content API returned an empty entry key for image upload");
|
||||
return ResultContainer<string>.CreateFailResult("Content API returned an empty response.");
|
||||
}
|
||||
|
||||
return ResultContainer<string>.CreatePassResult(payload.EntryKey);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed record ImageUploadResponse(string EntryKey);
|
||||
|
||||
public async Task<Result> UpdateAsync(
|
||||
long id, string trackName, string artist,
|
||||
string? album, string? genre, DateOnly? releaseDate,
|
||||
string? imagePath = null,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient(ContentCmsClientName);
|
||||
@@ -254,6 +322,7 @@ public class CmsTrackService : ICmsTrackService
|
||||
album,
|
||||
genre,
|
||||
releaseDate,
|
||||
imagePath,
|
||||
};
|
||||
|
||||
HttpResponseMessage response;
|
||||
|
||||
Reference in New Issue
Block a user