poc: legacy HTTP download for audio clips
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Rob Watson 2022-01-10 21:35:21 +01:00
parent af0674eb11
commit b0ccf17527
3 changed files with 54 additions and 10 deletions

View File

@ -124,6 +124,7 @@ func (h *httpHandler) handleClip(w http.ResponseWriter, r *http.Request) {
} }
w.Header().Set("content-type", "audio/"+format.String()) w.Header().Set("content-type", "audio/"+format.String())
w.Header().Set("content-disposition", "attachment; filename=clip."+format.String())
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
var closing bool var closing bool

View File

@ -20,13 +20,13 @@ import (
func TestHandler(t *testing.T) { func TestHandler(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
path, body, method, contentType string path, body, method, contentType string
config config.Config config config.Config
wantStartFrame, wantEndFrame int64 wantStartFrame, wantEndFrame int64
wantAudioFormat media.AudioFormat wantAudioFormat media.AudioFormat
wantStatus int wantStatus int
wantContentType, wantBody string wantContentType, wantContentDisp, wantBody string
}{ }{
{ {
name: "assets disabled, file system store disabled, GET /", name: "assets disabled, file system store disabled, GET /",
@ -148,6 +148,7 @@ func TestHandler(t *testing.T) {
wantEndFrame: 1024, wantEndFrame: 1024,
wantAudioFormat: media.AudioFormatMP3, wantAudioFormat: media.AudioFormatMP3,
wantContentType: "audio/mp3", wantContentType: "audio/mp3",
wantContentDisp: "attachment; filename=clip.mp3",
wantStatus: http.StatusOK, wantStatus: http.StatusOK,
wantBody: "an audio file", wantBody: "an audio file",
}, },
@ -162,6 +163,7 @@ func TestHandler(t *testing.T) {
wantEndFrame: 8192, wantEndFrame: 8192,
wantAudioFormat: media.AudioFormatWAV, wantAudioFormat: media.AudioFormatWAV,
wantContentType: "audio/wav", wantContentType: "audio/wav",
wantContentDisp: "attachment; filename=clip.wav",
wantStatus: http.StatusOK, wantStatus: http.StatusOK,
wantBody: "an audio file", wantBody: "an audio file",
}, },
@ -207,6 +209,9 @@ func TestHandler(t *testing.T) {
if tc.wantContentType != "" { if tc.wantContentType != "" {
assert.Equal(t, tc.wantContentType, resp.Header.Get("content-type")) assert.Equal(t, tc.wantContentType, resp.Header.Get("content-type"))
} }
if tc.wantContentDisp != "" {
assert.Equal(t, tc.wantContentDisp, resp.Header.Get("content-disposition"))
}
}) })
} }
} }

View File

@ -248,14 +248,52 @@ function App(): JSX.Element {
}, [audio, video, selection]); }, [audio, video, selection]);
const handleClip = useCallback(() => { const handleClip = useCallback(() => {
(async function () { if (!window.showSaveFilePicker) {
console.debug('clip', selection); downloadClipHTTP();
return;
}
downloadClipFileSystemAccessAPI();
}, [mediaSet, selection]);
const downloadClipHTTP = useCallback(() => {
(async function () {
if (mediaSet == null) { if (mediaSet == null) {
return; return;
} }
// TODO: support File System Access API fallback console.debug('clip http', selection);
const form = document.createElement('form');
form.method = 'POST';
form.action = `${apiURL}/api/media_sets/${mediaSet.id}/clip`;
const startFrameInput = document.createElement('input');
startFrameInput.type = 'hidden';
startFrameInput.name = 'start_frame';
startFrameInput.value = String(selection.start);
form.appendChild(startFrameInput);
const endFrameInput = document.createElement('input');
endFrameInput.type = 'hidden';
endFrameInput.name = 'end_frame';
endFrameInput.value = String(selection.end);
form.appendChild(endFrameInput);
const formatInput = document.createElement('input');
formatInput.type = 'hidden';
formatInput.name = 'format';
formatInput.value = 'mp3';
form.appendChild(formatInput);
document.body.appendChild(form);
form.submit();
})();
}, [mediaSet, selection]);
const downloadClipFileSystemAccessAPI = useCallback(() => {
(async function () {
if (mediaSet == null) {
return;
}
console.debug('clip grpc', selection);
const h = await window.showSaveFilePicker({ suggestedName: 'clip.mp3' }); const h = await window.showSaveFilePicker({ suggestedName: 'clip.mp3' });
const fileStream = await h.createWritable(); const fileStream = await h.createWritable();