Skip to content

Commit b16bb5d

Browse files
committed
Add API for downloading media file metadata
Will eventually add API to do this in batch form for all media files in the project, then we can (e.g.) download metadata immediately and download file contents on-demand, or download and cache all file contents prior to going offline.
1 parent 1be3ddb commit b16bb5d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

backend/FwLite/LcmCrdt/MediaServer/IMediaServerClient.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ public interface IMediaServerClient
88
[Get("/api/media/{fileId}")]
99
Task<HttpResponseMessage> DownloadFile(Guid fileId);
1010

11+
[Get("/api/media/metadata/{fileId}")]
12+
Task<HttpResponseMessage> GetFileMetadata(Guid fileId);
13+
1114
[Post("/api/media")]
1215
[Multipart]
1316
Task<MediaUploadFileResponse> UploadFile(MultipartItem file,

backend/FwLite/LcmCrdt/MediaServer/LcmMediaService.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using LcmCrdt.RemoteSync;
88
using Microsoft.Extensions.Logging;
99
using MiniLcm.Media;
10+
using System.Net.Http.Json;
1011

1112
namespace LcmCrdt.MediaServer;
1213

@@ -69,6 +70,29 @@ public async Task<ReadFileResponse> GetFileStream(Guid fileId)
6970
return new(File.OpenRead(localResource.LocalPath), Path.GetFileName(localResource.LocalPath));
7071
}
7172

73+
public async Task<LcmFileMetadata> GetFileMetadata(Guid fileId)
74+
{
75+
var mediaClient = await MediaServerClient();
76+
var response = await mediaClient.GetFileMetadata(fileId);
77+
if (!response.IsSuccessStatusCode)
78+
{
79+
throw new Exception($"Failed to retrieve metadata for file {fileId}: {response.StatusCode} {response.ReasonPhrase}");
80+
}
81+
var metadata = await response.Content.ReadFromJsonAsync<LcmFileMetadata>();
82+
if (metadata is null)
83+
{
84+
// Try to get content into error message, but if buffering not enabled for this request, give up
85+
var content = "";
86+
try
87+
{
88+
content = await response.Content.ReadAsStringAsync();
89+
}
90+
catch { } // Oh well, we tried
91+
throw new Exception($"Failed to retrieve metadata for file {fileId}: response was in incorrect format. {content}");
92+
}
93+
return metadata;
94+
}
95+
7296
private async Task<(Stream? stream, string? filename)> RequestMediaFile(Guid fileId)
7397
{
7498
var mediaClient = await MediaServerClient();

0 commit comments

Comments
 (0)