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,6 +43,7 @@ const template = `
</body> </body>
</html> </html>
` `
type ops struct { type ops struct {
URL string URL string
Res chan string Res chan string
@ -69,13 +73,35 @@ 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")
@ -89,6 +115,8 @@ 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)
@ -96,5 +124,3 @@ func main () {
log.Printf("Starting server on 8080") log.Printf("Starting server on 8080")
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)
} }