Go file for parsing, if vid ffmpeg to HLS (inprogress)

This commit is contained in:
sam-pich 2024-11-19 00:27:01 -05:00
parent 37044fe071
commit b793711537
2 changed files with 240 additions and 35 deletions

View file

@ -1,37 +1,78 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Third Culture Upload</title>
<link rel="stylesheet" href="styles/main.css" />
</head>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Third Culture Upload</title>
<link rel="stylesheet" href="styles/main.css">
</head>
<body>
<h1><span class="accent">Third Culture</span> Upload</h1>
<form action="https://up.thirdculture.top" method="POST" enctype="multipart/form-data">
<label for="fileInput">Choose file:</label>
<input type="file" name="file" id="fileInput" required />
<br><br>
<button type="submit">Upload</button>
</form>
<p><br></p>
<p>Make sure to copy the link in the next page!</p>
<p>More info on third culture upload <a href="scripts/info.html">here</a>.</p>
<p><br></p>
<svg version="1.1" style="fill:none;width:75px;height:75px;" width="50" height="50" viewBox="0 0 50 50" id="svg3" sodipodi:docname="logo.svg"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<defs id="defs3" />
<path style="fill:#000000;stroke:#000000;stroke-width:1.51515px;stroke-linecap:round;stroke-linejoin:round"
d="m 0.75757498,49.242423 v 0 V 0.75757498 H 49.242425 V 49.242423 Z" id="path1" />
<path style="fill:none;stroke:#a4d1ad;stroke-width:1.51515px;stroke-linecap:round;stroke-linejoin:round"
d="m 20.454546,38.636362 v 0 l 9.090908,-9.090909 m -9.090908,0 v 0 l 9.090908,9.090909" id="path2" />
<path style="fill:none;stroke:#aea3f0;stroke-width:4.24243px;stroke-linecap:round;stroke-linejoin:round"
d="m 6.8181812,40.151514 v 0 L 25,9.8484837 43.181819,40.151514" id="path3" />
</svg>
</body>
</html>
<body>
<h1><span class="accent">Third Culture</span> Upload</h1>
<form
action="http://localhost:3880"
method="POST"
enctype="multipart/form-data"
>
<label for="fileInput">Choose file:</label>
<input type="file" name="file" id="fileInput" required />
<br /><br />
<button type="submit">Upload</button>
</form>
<p><br /></p>
<p>Make sure to copy the link in the next page!</p>
<p>
More info on third culture upload
<a href="scripts/info.html">here</a>.
</p>
<p><br /></p>
<svg
version="1.1"
style="fill: none; width: 75px; height: 75px"
width="50"
height="50"
viewBox="0 0 50 50"
id="svg3"
sodipodi:docname="logo.svg"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"
>
<defs id="defs3" />
<path
style="
fill: #000000;
stroke: #000000;
stroke-width: 1.51515px;
stroke-linecap: round;
stroke-linejoin: round;
"
d="m 0.75757498,49.242423 v 0 V 0.75757498 H 49.242425 V 49.242423 Z"
id="path1"
/>
<path
style="
fill: none;
stroke: #a4d1ad;
stroke-width: 1.51515px;
stroke-linecap: round;
stroke-linejoin: round;
"
d="m 20.454546,38.636362 v 0 l 9.090908,-9.090909 m -9.090908,0 v 0 l 9.090908,9.090909"
id="path2"
/>
<path
style="
fill: none;
stroke: #aea3f0;
stroke-width: 4.24243px;
stroke-linecap: round;
stroke-linejoin: round;
"
d="m 6.8181812,40.151514 v 0 L 25,9.8484837 43.181819,40.151514"
id="path3"
/>
</svg>
</body>
</html>

164
transfer/tshweb/transfer.go Normal file
View file

@ -0,0 +1,164 @@
package main
import (
"fmt"
"io"
"net/http"
"os/exec"
"strings"
)
func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
err := r.ParseMultipartForm(15 * 1024 * 1024 * 1024) // limit 15 gb
if err != nil {
http.Error(w, "Unable to parse form", http.StatusBadRequest)
return
}
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, "Unable to retrieve file", http.StatusBadRequest)
return
}
defer file.Close()
// Upload the file
req, err := http.NewRequest("POST", "http://localhost:3880", file)
if err != nil {
http.Error(w, "Error creating request", http.StatusInternalServerError)
return
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
http.Error(w, "Error uploading to transfer.sh", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
// Unique ID to track file for ffmpeg etc
responseURL, err := io.ReadAll(resp.Body)
if err != nil {
http.Error(w, "Error reading response", http.StatusInternalServerError)
return
}
urlParts := strings.Split(string(responseURL), "/")
uniqueID := urlParts[len(urlParts)-1]
// Convert file to HLS. Work on error catching and flags tomorrow
// IF VIDEO=DO THIS. IF NOT CONTINUE!
// ALSO CHANGE THE CODE. IF VIDEO, REDIRECT TO S3 BUCKET
// CURRENTLY FILE IS STANDARD UPLOAD TO SERVER
cmd := exec.Command("ffmpeg")
// HTML
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Third Culture Upload</title>
<link rel="stylesheet" href="styles/main.css" />
</head>
<body>
<h1>Upload Success</h1>
<p><strong>Unique ID:</strong> %s</p>
<p><a href="%s" target="_blank">%s</a></p>
<a href="/">Upload Another</a>
</body>
</html>
`, uniqueID, string(responseURL), string(responseURL))
return
}
// Original GET form
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Third Culture Upload</title>
<link rel="stylesheet" href="styles/main.css" />
</head>
<body>
<h1><span class="accent">Third Culture</span> Upload</h1>
<form
action="http://localhost:3880"
method="POST"
enctype="multipart/form-data"
>
<label for="fileInput">Choose file:</label>
<input type="file" name="file" id="fileInput" required />
<br /><br />
<button type="submit">Upload</button>
</form>
<p><br /></p>
<p>Make sure to copy the link in the next page!</p>
<p>
More info on third culture upload
<a href="scripts/info.html">here</a>.
</p>
<p><br /></p>
<svg
version="1.1"
style="fill: none; width: 75px; height: 75px"
width="50"
height="50"
viewBox="0 0 50 50"
id="svg3"
sodipodi:docname="logo.svg"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"
>
<defs id="defs3" />
<path
style="
fill: #000000;
stroke: #000000;
stroke-width: 1.51515px;
stroke-linecap: round;
stroke-linejoin: round;
"
d="m 0.75757498,49.242423 v 0 V 0.75757498 H 49.242425 V 49.242423 Z"
id="path1"
/>
<path
style="
fill: none;
stroke: #a4d1ad;
stroke-width: 1.51515px;
stroke-linecap: round;
stroke-linejoin: round;
"
d="m 20.454546,38.636362 v 0 l 9.090908,-9.090909 m -9.090908,0 v 0 l 9.090908,9.090909"
id="path2"
/>
<path
style="
fill: none;
stroke: #aea3f0;
stroke-width: 4.24243px;
stroke-linecap: round;
stroke-linejoin: round;
"
d="m 6.8181812,40.151514 v 0 L 25,9.8484837 43.181819,40.151514"
id="path3"
/>
</svg>
</body>
</html>
`)
}
func main() {
http.HandleFunc("/", uploadHandler)
fmt.Println("Server started at localhost")
http.ListenAndServe(":3880", nil)
}