2020-10-31 11:02:59 +00:00
|
|
|
package workers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/deletescape/goop/internal/utils"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2020-11-21 17:52:38 +00:00
|
|
|
func DownloadWorker(c *fasthttp.Client, queue <-chan string, baseUrl, baseDir string, wg *sync.WaitGroup, allowHtml bool) {
|
2020-10-31 11:02:59 +00:00
|
|
|
defer wg.Done()
|
2020-10-31 12:38:17 +00:00
|
|
|
for file := range queue {
|
|
|
|
if file == "" {
|
|
|
|
continue
|
|
|
|
}
|
2020-11-12 13:32:12 +00:00
|
|
|
targetFile := utils.Url(baseDir, file)
|
|
|
|
if utils.Exists(targetFile) {
|
|
|
|
fmt.Printf("%s was downloaded already, skipping\n", targetFile)
|
|
|
|
continue
|
|
|
|
}
|
2020-10-31 11:02:59 +00:00
|
|
|
uri := utils.Url(baseUrl, file)
|
|
|
|
code, body, err := c.Get(nil, uri)
|
|
|
|
fmt.Printf("[-] Fetching %s [%d]\n", uri, code)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if code == 200 {
|
2020-11-21 17:52:38 +00:00
|
|
|
if !allowHtml && utils.IsHtml(body) {
|
2020-10-31 11:02:59 +00:00
|
|
|
fmt.Printf("warning: %s appears to be an html file, skipping\n", uri)
|
|
|
|
continue
|
|
|
|
}
|
2020-11-17 22:22:47 +00:00
|
|
|
if utils.IsEmptyBytes(body) {
|
2020-11-12 13:32:12 +00:00
|
|
|
fmt.Printf("warning: %s appears to be an empty file, skipping\n", uri)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := utils.CreateParentFolders(targetFile); err != nil {
|
2020-10-31 11:02:59 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
|
|
|
continue
|
|
|
|
}
|
2020-11-12 13:32:12 +00:00
|
|
|
if err := ioutil.WriteFile(targetFile, body, os.ModePerm); err != nil {
|
2020-10-31 11:02:59 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|