-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add API for downloading media file metadata #1930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rmunn
wants to merge
14
commits into
develop
Choose a base branch
from
feat/media-file-metadata-apis
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b16bb5d
Add API for downloading media file metadata
rmunn 8a24f83
Add API to up/download all pending resources
rmunn e14fab2
Add API for up/downloading only specific resources
rmunn 693e720
Add JsInvokable version of MediaFilesService
rmunn 24bf298
Call correct service methods for up/download all
rmunn b46bb0d
Create VERY basic UI for testing media files API
rmunn 7fa38a8
Fix DownloadAllResources
rmunn f1e5c30
Fix notification in barebones test UI
rmunn a636f41
Add GetFileMetadata to JsInvokable API
rmunn 51e5622
Slightly nicer display of file types
rmunn ba611da
Tweak down/upload JS API to take GUIDs, not objects
rmunn 0ae4a8d
Test UI now allows selecting files to download
rmunn 35e07a3
Also expose allResources to JSInvokable API
rmunn 9add1da
Add very ugly test UI for allResources API call
rmunn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
57 changes: 57 additions & 0 deletions
57
backend/FwLite/FwLiteShared/Services/MediaFilesServiceJsInvokable.cs
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Microsoft.JSInterop; | ||
using LcmCrdt.MediaServer; | ||
using SIL.Harmony.Resource; | ||
using MiniLcm.Media; | ||
|
||
namespace FwLiteShared.Services; | ||
|
||
public class MediaFilesServiceJsInvokable(LcmMediaService mediaService) | ||
{ | ||
[JSInvokable] | ||
public async Task<HarmonyResource[]> AllResources() | ||
{ | ||
return await mediaService.AllResources(); | ||
} | ||
|
||
[JSInvokable] | ||
public async Task<RemoteResource[]> ResourcesPendingDownload() | ||
{ | ||
return await mediaService.ResourcesPendingDownload(); | ||
} | ||
|
||
[JSInvokable] | ||
public async Task<LocalResource[]> ResourcesPendingUpload() | ||
{ | ||
return await mediaService.ResourcesPendingUpload(); | ||
} | ||
|
||
[JSInvokable] | ||
public async Task DownloadAllResources() | ||
{ | ||
await mediaService.DownloadAllResources(); | ||
} | ||
|
||
[JSInvokable] | ||
public async Task UploadAllResources() | ||
{ | ||
await mediaService.UploadAllResources(); | ||
} | ||
|
||
[JSInvokable] | ||
public async Task DownloadResources(IEnumerable<Guid> resourceIds) | ||
{ | ||
await mediaService.DownloadResources(resourceIds); | ||
} | ||
|
||
[JSInvokable] | ||
public async Task UploadResources(IEnumerable<Guid> resourceIds) | ||
{ | ||
await mediaService.UploadResources(resourceIds); | ||
} | ||
|
||
[JSInvokable] | ||
public async Task<LcmFileMetadata> GetFileMetadata(Guid fileId) | ||
{ | ||
return await mediaService.GetFileMetadata(fileId); | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
using LcmCrdt.RemoteSync; | ||
using Microsoft.Extensions.Logging; | ||
using MiniLcm.Media; | ||
using System.Net.Http.Json; | ||
|
||
namespace LcmCrdt.MediaServer; | ||
|
||
|
@@ -24,6 +25,16 @@ public async Task<HarmonyResource[]> AllResources() | |
return await resourceService.AllResources(); | ||
} | ||
|
||
public async Task<RemoteResource[]> ResourcesPendingDownload() | ||
{ | ||
return await resourceService.ListResourcesPendingDownload(); | ||
} | ||
|
||
public async Task<LocalResource[]> ResourcesPendingUpload() | ||
{ | ||
return await resourceService.ListResourcesPendingUpload(); | ||
} | ||
|
||
/// <summary> | ||
/// should only be used in fw-headless for files which already exist in the lexbox db | ||
/// </summary> | ||
|
@@ -42,6 +53,74 @@ public async Task DeleteResource(Guid fileId) | |
await resourceService.DeleteResource(currentProjectService.ProjectData.ClientId, fileId); | ||
} | ||
|
||
public async Task<LocalResource?> DownloadResourceIfNeeded(Guid fileId) | ||
{ | ||
var localResource = await resourceService.GetLocalResource(fileId); | ||
if (localResource is null) | ||
{ | ||
var connectionStatus = await httpClientProvider.ConnectionStatus(); | ||
if (connectionStatus == ConnectionStatus.Online) | ||
{ | ||
return await resourceService.DownloadResource(fileId, this); | ||
} | ||
} | ||
return localResource; | ||
} | ||
|
||
public async Task DownloadAllResources() | ||
{ | ||
var connectionStatus = await httpClientProvider.ConnectionStatus(); | ||
if (connectionStatus == ConnectionStatus.Online) | ||
{ | ||
var resources = await ResourcesPendingDownload(); | ||
foreach (var resource in resources) | ||
{ | ||
if (resource.RemoteId is null) continue; | ||
await resourceService.DownloadResource(resource.Id, this); | ||
} | ||
} | ||
// TODO: Gracefully handle other connection statuses, e.g. "not logged in" | ||
} | ||
|
||
public async Task UploadAllResources() | ||
{ | ||
await UploadResources((await ResourcesPendingUpload()).Select(r => r.Id)); | ||
} | ||
|
||
public async Task DownloadResources(IEnumerable<Guid> resourceIds) | ||
{ | ||
foreach (var resourceId in resourceIds) | ||
{ | ||
await DownloadResourceIfNeeded(resourceId); | ||
} | ||
} | ||
|
||
public async Task DownloadResources(IEnumerable<RemoteResource> resources) | ||
{ | ||
foreach (var resource in resources) | ||
{ | ||
await DownloadResourceIfNeeded(resource.Id); | ||
} | ||
} | ||
|
||
public async Task UploadResources(IEnumerable<Guid> resourceIds) | ||
{ | ||
var clientId = currentProjectService.ProjectData.ClientId; | ||
foreach (var resourceId in resourceIds) | ||
{ | ||
await resourceService.UploadPendingResource(resourceId, clientId, this); | ||
} | ||
} | ||
|
||
public async Task UploadResources(IEnumerable<LocalResource> resources) | ||
{ | ||
var clientId = currentProjectService.ProjectData.ClientId; | ||
foreach (var resource in resources) | ||
{ | ||
await resourceService.UploadPendingResource(resource, clientId, this); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// return a stream for the file, if it's not cached locally, it will be downloaded | ||
/// </summary> | ||
|
@@ -50,25 +129,44 @@ public async Task DeleteResource(Guid fileId) | |
/// <exception cref="FileNotFoundException"></exception> | ||
public async Task<ReadFileResponse> GetFileStream(Guid fileId) | ||
{ | ||
var localResource = await resourceService.GetLocalResource(fileId); | ||
var localResource = await DownloadResourceIfNeeded(fileId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like that we're not calling |
||
if (localResource is null) | ||
{ | ||
var connectionStatus = await httpClientProvider.ConnectionStatus(); | ||
if (connectionStatus == ConnectionStatus.Online) | ||
{ | ||
localResource = await resourceService.DownloadResource(fileId, this); | ||
// Try again, maybe earlier failure was a blip | ||
localResource = await DownloadResourceIfNeeded(fileId); | ||
} | ||
else | ||
{ | ||
return new ReadFileResponse(ReadFileResult.Offline); | ||
} | ||
} | ||
//todo, consider trying to download the file again, maybe the cache was cleared | ||
if (localResource is null || !File.Exists(localResource.LocalPath)) | ||
{ | ||
// One more attempt to download again, maybe the cache was cleared | ||
localResource = await DownloadResourceIfNeeded(fileId); | ||
// If still null then connection is offline or unreliable enough to consider as offline | ||
if (localResource is null) return new ReadFileResponse(ReadFileResult.Offline); | ||
} | ||
// If still can't find local path then this is where we give up | ||
if (!File.Exists(localResource.LocalPath)) | ||
throw new FileNotFoundException("Unable to find the file with Id" + fileId, localResource.LocalPath); | ||
return new(File.OpenRead(localResource.LocalPath), Path.GetFileName(localResource.LocalPath)); | ||
} | ||
|
||
public async Task<LcmFileMetadata> GetFileMetadata(Guid fileId) | ||
{ | ||
var mediaClient = await MediaServerClient(); | ||
var metadata = await mediaClient.GetFileMetadata(fileId); | ||
if (metadata is null) | ||
{ | ||
throw new Exception($"Failed to retrieve metadata for file {fileId}"); | ||
} | ||
return metadata; | ||
} | ||
|
||
private async Task<(Stream? stream, string? filename)> RequestMediaFile(Guid fileId) | ||
{ | ||
var mediaClient = await MediaServerClient(); | ||
|
This file contains hidden or 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
10 changes: 10 additions & 0 deletions
10
frontend/viewer/src/lib/components/ui/checkbox/checkbox-group.svelte
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<script lang="ts"> | ||
import {Checkbox as CheckboxPrimitive} from 'bits-ui'; | ||
|
||
let { | ||
value = $bindable<string[]>([]), | ||
...restProps | ||
}: CheckboxPrimitive.GroupProps = $props(); | ||
</script> | ||
|
||
<CheckboxPrimitive.Group bind:value {...restProps} /> |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import Root from './checkbox.svelte'; | ||
import Group from './checkbox-group.svelte'; | ||
export { | ||
// | ||
Root as Checkbox, Root | ||
Root as Checkbox, Group as CheckboxGroup, Root | ||
}; |
This file contains hidden or 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
22 changes: 22 additions & 0 deletions
22
...c/lib/dotnet-types/generated-types/FwLiteShared/Services/IMediaFilesServiceJsInvokable.ts
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* eslint-disable */ | ||
// This code was generated by a Reinforced.Typings tool. | ||
// Changes to this file may cause incorrect behavior and will be lost if | ||
// the code is regenerated. | ||
|
||
import type {IHarmonyResource} from '../../SIL/Harmony/Resource/IHarmonyResource'; | ||
import type {IRemoteResource} from '../../SIL/Harmony/Resource/IRemoteResource'; | ||
import type {ILocalResource} from '../../SIL/Harmony/Resource/ILocalResource'; | ||
import type {ILcmFileMetadata} from '../../MiniLcm/Media/ILcmFileMetadata'; | ||
|
||
export interface IMediaFilesServiceJsInvokable | ||
{ | ||
allResources() : Promise<IHarmonyResource[]>; | ||
resourcesPendingDownload() : Promise<IRemoteResource[]>; | ||
resourcesPendingUpload() : Promise<ILocalResource[]>; | ||
downloadAllResources() : Promise<void>; | ||
uploadAllResources() : Promise<void>; | ||
downloadResources(resourceIds: string[]) : Promise<void>; | ||
uploadResources(resourceIds: string[]) : Promise<void>; | ||
getFileMetadata(fileId: string) : Promise<ILcmFileMetadata>; | ||
} | ||
/* eslint-enable */ |
This file contains hidden or 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
14 changes: 14 additions & 0 deletions
14
...tend/viewer/src/lib/dotnet-types/generated-types/SIL/Harmony/Resource/IHarmonyResource.ts
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* eslint-disable */ | ||
// This code was generated by a Reinforced.Typings tool. | ||
// Changes to this file may cause incorrect behavior and will be lost if | ||
// the code is regenerated. | ||
|
||
export interface IHarmonyResource | ||
{ | ||
id: string; | ||
remoteId?: string; | ||
localPath?: string; | ||
local: boolean; | ||
remote: boolean; | ||
} | ||
/* eslint-enable */ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ues we should always use
OnlyJsInvokable
for services which are executed over js like this, and in this caseAlwaysReturnPromise
is a good idea too.