-
Notifications
You must be signed in to change notification settings - Fork 2
Assistant/add upload file option #979
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
base: beta-master
Are you sure you want to change the base?
Conversation
|
Hi @rosannamilner Do you want to reuse the component we use for tools with the URL and file upload? This could also be used. Also, we have made tabs in our UI that was using the "two big buttons" to pick between URL and local file in the tools such as CheckGIF and Keyframes. Could you have a look too? I am not sure these UIs are totally relevant with what the file upload you are trying to make, but reusing it could save you some time. |
Thanks Valentin - I'll take a look and it makes sense to reuse the URL and file upload component, it does feel a bit clunky having these two big buttons to start with rather than directly letting user choose. So the upload section is for a user to upload their own image or video and see the "Extracted media files" card with just the single media present. The user would see the Recommended tools list appear next to the image/video. The existing code has the media stored as blob files which can then be displayed. To send the media file off to the DBKF similar images service, it looks like I need to actually store the file somewhere. I tried converting the file to a data URL but the example image I tried makes the URL too long and the DBKF service returns 414 error code. Given the DBKF code/api requests are not on the backend, can these uploaded files be stored on the frontend? How is it done for the other tools? The DBKF service expects a URL to the image/video. |
|
If I understand correctly @rosannamilner DBKF does not allow to send a file (upload file) and always requires an URL. We do not really have this problem with other APIs because they can receive files. Does using the form component break things for you or was it already a problem before? If they only accept URLs, I see mainly 2 options: 1) skip the DBKF call for local files because they only accept urls, 2) have a way in the sheffield back-end to make a temporary url which means uploading the file to your back-end. |
Thanks Valentin - I will check whether their API will receive files, otherwise work something out in the backend. For this PR I will skip the DBKF call (the |
|
Not all the recommended plugin tools accept the blob format - looking into what we can do in the backend with @ianroberts |
…appear on closing results
This is probably the way we'll go - rather than the plugin directly calling the DBKF service, we'll add a kind of proxy layer in our backend so that the user can POST images to us, we host them at a temporary URL which the backend passes to DBKF, the backend receives the response from DBKF, sends it back to the client and then deletes the temporary uploaded file. |
The other issue I'm having is with the recommended tools for the uploaded media. Clicking on a tool sends the blob URL again, which doesn't work when submitted to that tool as a URL or as a local file (tried for image Magnifier and Metadata tools so far). Is there something I'm missing here, or an alternative option? Could the temporary URL provided by our backend be used across the tools (but hidden to user if necessary)? |
|
How many of those tools have a "push" API option where you upload the data to them, rather than them pulling it down from a URL? Dealing with local image files is a fundamentally different operation from dealing with URLs to remotely-hosted files. |
|
I'm thinking a fairly simple way to handle the handing off of an already-uploaded import React, {createContext, useContext, useReducer, useState} from "react";
import {useSearchParams} from "react-router-dom";
const defaultSelectionContext = {
/** @type {string | undefined} */
url: undefined,
/** @type {File | undefined} */
file: undefined,
/** @type {(item?: (string | File | undefined)) => void} */
setAssistantSelection: () => {},
}
const AssistantSelectionContext = createContext(defaultSelectionContext);
// if reducer needs to return an empty object, always use the same one
const emptyState = {};
export const AssistantSelectionProvider = ({children}) => {
const [{url, file}, setAssistantSelection] = useReducer((state, action) => {
if (typeof action === "undefined" || action === null) {
return emptyState;
}
if (typeof action === "string") {
// avoid re-render if setAssistantSelection is called with the same value twice
return state.url === action ? state : {url: action};
}
// else it must be a File - again avoid re-render if setAssistantSelection is called
// with the same value twice
return state.file === action ? state : {file: action};
}, {});
return (
<AssistantSelectionContext.Provider value={{url, file, setAssistantSelection}}>
{children}
</AssistantSelectionContext.Provider>
)
}
export const useUrlOrFile = () => {
const {url, file} = useContext(AssistantSelectionContext);
const [params] = useSearchParams();
const fromAssistant = params.has("fromAssistant");
// if we were redirected from the assistant then use the context
// values as the initial defaults for our state
const [myUrl, setUrl] = useState(fromAssistant ? url : undefined);
const [myFile, setFile] = useState(fromAssistant ? file : undefined);
return [myUrl, setUrl, myFile, setFile];
};
export const useSetInputFromAssistant = () => {
const { setAssistantSelection } = useContext(AssistantSelectionContext);
return setAssistantSelection;
}You'd need to insert the // instead of
const [input, setInput] = useState(resultUrl);
const [imageFile, setImageFile] = useState(undefined);
// use
const [input, setInput, imageFile, setImageFile] = useUrlOrFile();Now in the assistant you can get the updater function with setAssistantSelection(fileInput ?? formInput);
navigate("/path/to/tool?fromAssistant"); |
|
I suppose it might make sense to lift the actual |
|
In a way this approach is kind of re-inventing the redux wheel - we could just put the |
This removes the previous fixed list of video and image tools and replaces with
StringFileUploadFieldcomponent to either paste URL or upload an image or video to submit to the Assistant.urlMode === true)On opening the Assistant:

InVID Translations: AFP-Medialab/InVID-Translations#144