diff --git a/src/pages/index.tsx b/src/pages/index.tsx index c0efbb1..b9cdab1 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -22,16 +22,16 @@ export default function Home() { } }; - const handleMp4 = async () => { + const handle = async (type: string) => { const videoID = getVideoID(url); setInfo("Processing the video..."); if (videoID) { const title = await getTitle(videoID); try { - fetch(`/api/download`, { + fetch("/api/download", { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ url, type: "mp4" }), + body: JSON.stringify({ url, type }), }) .then(res => res.blob()) .then(blob => { @@ -41,7 +41,11 @@ export default function Home() { "Unable to download! Maybe File size is too high. Try to download video less than 5MB" ); } else { - download(blob, `${title}.mp4`, "video/mp4"); + download( + blob, + `${title}.${type}`, + type === "mp3" ? "audio/mpeg" : "video/mp4" + ); setInfo("Ready for download!"); } }); @@ -55,39 +59,6 @@ export default function Home() { } }; - const handleMp3 = async () => { - const videoID = getVideoID(url); - setInfo("Processing the video..."); - if (videoID) { - const title = await getTitle(videoID); - try { - const requestOptions = { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ url, type: "mp3" }), - }; - fetch(`/api/download`, requestOptions) - .then(res => res.blob()) - .then(blob => { - const sizeInBytes = blob.size; - console.log("sizeInBytes: ", sizeInBytes); - if (sizeInBytes <= 0) { - setInfo( - "Unable to download! Maybe File size is too high. Try to download video less than 5MB" - ); - } else { - download(blob, `${title}.mp3`, "audio/mpeg"); - setInfo("Ready for download!"); - } - }); - } catch (err) { - console.log("err: ", err); - } - } else { - setInfo("Invalid URL"); - } - }; - return (
@@ -126,13 +97,13 @@ export default function Home() {