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