Download file are stored in .viddown while downloading

testing
Justin Reichardt 2022-11-09 17:13:10 -06:00
parent e4ef9da436
commit 19ec9225f5
1 changed files with 48 additions and 22 deletions

View File

@ -18,13 +18,16 @@
*/
package main
import (
"log"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"net/http"
"path"
"strconv"
)
const template = `
@ -40,48 +43,71 @@ const template = `
</body>
</html>
`
type ops struct{
type ops struct {
URL string
Res chan string
}
var config struct{
var config struct {
saveLocation string
}
var opsc = make(chan ops)
func handleRequest (w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet{
func handleRequest(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
log.Print("New Get request")
}else if r.Method == http.MethodPost {
} else if r.Method == http.MethodPost {
r.ParseForm()
go func() {
var o ops
o.URL = r.FormValue("URL")
opsc <- o
} ()
} else{
}()
} else {
}
io.WriteString (w,template)
io.WriteString(w, template)
}
func downloadVid (r *http.Request) {
func downloadVid(r *http.Request) {
}
func operationHandler(){
for true {
o := <- opsc
func operationHandler() {
for i := 0; true; i++ {
o := <-opsc
log.Print("Start Downloading: " + o.URL)
go func() {
dir := path.Join(config.saveLocation, ".viddown", strconv.Itoa(i))
os.MkdirAll(dir, 0755)
cmd := exec.Command("youtube-dl", o.URL)
cmd.Dir = config.saveLocation
cmd.Start()
}()
cmd.Dir = dir + "/"
log.Println(dir + "/")
cmd.Run()
files, err := ioutil.ReadDir(dir + "/")
if err != nil {
log.Print(err)
}
for _, file := range files {
err := os.Rename(dir+"/"+file.Name(), file.Name())
if err != nil {
log.Print(err)
log.Print("Failed to move: " + dir + "/" + file.Name() + " to: " + file.Name())
}
}
err = os.RemoveAll(dir)
if err != nil {
log.Print("Failed to remove: " + dir)
}
}()
}
log.Fatal("Exited operationsHandler")
}
func main () {
func main() {
if os.Getenv("VIDDOWNLOC") == "" {
config.saveLocation = "./"
} else {
@ -89,12 +115,12 @@ func main () {
}
log.Println("Downloading files to: " + config.saveLocation)
os.MkdirAll(path.Join(config.saveLocation, ".viddown"), 0755)
go operationHandler()
handler := http.HandlerFunc(handleRequest)
http.Handle("/",handler)
http.Handle("/", handler)
log.Printf("Starting server on 8080")
http.ListenAndServe(":8080",nil)
http.ListenAndServe(":8080", nil)
}