diff --git a/lib/urlutils.ts b/lib/urlutils.ts index 2a0f8d4..35e4a40 100644 --- a/lib/urlutils.ts +++ b/lib/urlutils.ts @@ -1,9 +1,9 @@ const deliveryDomain = process.env.NEXT_PUBLIC_MUX_BYO_DOMAIN || "mux.com"; -export function getStreamBaseUrl() { +export function getStreamBaseUrl() { // eslint-disable-line return `https://stream.${deliveryDomain}`; } -export function getImageBaseUrl() { - return `https://image.${deliveryDomain}`; +export function getImageBaseUrl() { // eslint-disable-line + return `https://image.${deliveryDomain}`; } diff --git a/pages/api/assets/index.ts b/pages/api/assets/index.ts new file mode 100644 index 0000000..9e70598 --- /dev/null +++ b/pages/api/assets/index.ts @@ -0,0 +1,33 @@ +import { NextApiRequest, NextApiResponse } from 'next'; +import Mux from '@mux/mux-node'; + +const { Video } = new Mux(); + +export default async (req: NextApiRequest, res: NextApiResponse): Promise => { + const { method, body } = req; // eslint-disable-line + + switch (method) { + case 'POST': + try { + const asset = await Video.Assets.create({ + // this is the text box contents from index.ts + input: req.body, + "playback_policy": [ + "public" + ], + }); + res.json({ + id: asset.id, + status: asset.status, + }); + } catch (e) { + res.statusCode = 500; + console.error('Request error', e); // eslint-disable-line no-console + res.json({ error: 'Error creating asset' }); + } + break; + default: + res.setHeader('Allow', ['POST']); + res.status(405).end(`Method ${method} Not Allowed`); + } +}; diff --git a/pages/index.tsx b/pages/index.tsx index cc199b7..b9ea927 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -11,6 +11,12 @@ const Index: React.FC = () => { const [file, setFile] = useState(null); const [showUploadPage, setShowUploadPage] = useState(true); const inputRef = useRef(null); + // const [inputRef, setInputRef] = useState(null); // not really working + + const [assetID, setAssetId] = useState(''); // eslint-disable-line + const [errorMessage, setErrorMessage] = useState('');// eslint-disable-line + const myEndpoint = useRef(null); + const [resetButton, setResetButton] = useState(true); // eslint-disable-line const onDrop = useCallback((acceptedFiles) => { if (acceptedFiles && acceptedFiles[0]) { @@ -32,6 +38,65 @@ const Index: React.FC = () => { return setShowUploadPage(false)}/>; } + function isValidHTTPURL(inputString: string | URL) { + let url; + try { + url = new URL(inputString); + } catch (_) { + return false; + } + return url.protocol === "http:" || url.protocol === "https:"; + } + + const myFunc2 = async () => { + if (! isValidHTTPURL(myEndpoint.current?.value)) { + setErrorMessage('not a valid url, try again '); + } + else { + if (myEndpoint && isValidHTTPURL(myEndpoint.current?.value)) { + try { + return fetch('/api/assets', { + method: 'POST', + body: myEndpoint.current.value // Error: Endpoint is possibly null + }) + .then((res) => res.json()) + // this needs to change because + // a direct upload isn't happening, + // if something different should happen + .then(({ id, status }) => { + setAssetId(id); + setErrorMessage(''); + console.log('new asset id', id); + console.log('status', status); + return status; + }); + } catch (e) { + console.error('Error in createUpload', e); // eslint-disable-line no-console + setErrorMessage('Error creating upload'); + return Promise.reject(e); + } + } + try { + return fetch('/api/assets', { + method: 'POST', + }) + .then((res) => res.json()) + .then(({ id, status }) => { + setAssetId(id); + console.log('new asset id', id); + console.log('status', status); + return status; + }); + } catch (e) { + console.error('Error in createUpload', e); // eslint-disable-line no-console + setErrorMessage('Error creating upload'); + return Promise.reject(e); + } + } + }; + + // const resetInputRef = async () => { ... }; // todo + return ( = () => {

Add a video.

Get a shareable link to stream it.

+ + + {assetID &&

Asset created: {assetID}

} + {errorMessage &&

Error with upload please try again

} + + {/* {resetButton && Reset and upload again} */} + {/* */} +
@@ -61,12 +134,20 @@ const Index: React.FC = () => {