docs: added docstrings

This commit is contained in:
Bryan Bailey
2024-08-26 22:59:37 -04:00
parent e8bd6e4179
commit b3e99092fc
5 changed files with 55 additions and 104 deletions

View File

@@ -11,75 +11,10 @@ import (
cloudflarebp "github.com/DaRealFreak/cloudflare-bp-go"
)
// func _downloadFile(wg *sync.WaitGroup, url string, page int, c *Comic) error {
// defer wg.Done()
// pageNumber := fmt.Sprintf("%03d", page)
// formattedImagePath := fmt.Sprintf("%s %s.jpg", c.Title, pageNumber)
// imageFilepath, _ := filepath.Abs(filepath.Join(c.LibraryPath, c.Title, formattedImagePath))
// if err := os.MkdirAll(
// filepath.Dir(imageFilepath),
// os.ModePerm,
// ); err != nil {
// return ComicDownloadError{
// Message: "error creating directory",
// Code: 1,
// }
// }
// // get image data
// res, err := handleRequest(url)
// if err != nil {
// return ComicDownloadError{
// Message: "invalid request",
// Code: 1,
// }
// }
// defer res.Body.Close()
// var fileChannel = make(chan *os.File)
// go func() error {
// imageFile, err := os.Create(imageFilepath)
// if err != nil {
// return ComicDownloadError{
// Message: "error creating image file",
// Code: 1,
// }
// }
// defer imageFile.Close()
// fileChannel <- imageFile
// return nil
// }()
// println("Downloading", imageFilepath)
// go func(
// fc chan *os.File,
// res *http.Response,
// ) error {
// buffer := make([]byte, 64*1024)
// defer close(fileChannel)
// // write image data
// _, err := io.CopyBuffer(<-fc, res.Body, buffer)
// if err != nil {
// return ComicDownloadError{
// Message: "Unable to save file contents",
// Code: 1,
// }
// }
// return nil
// }(fileChannel, res)
// return nil
// }
// downloadFile downloads a file from a given URL and saves it to a specified location.
//
// The function takes a URL string, a page number, and a Comic struct as parameters.
// It returns an error if the download fails, and nil otherwise.
func downloadFile(url string, page int, c *Comic) error {
pageNumber := fmt.Sprintf("%03d", page)
formattedImagePath := fmt.Sprintf("%s %s.jpg", c.Title, pageNumber)
@@ -131,6 +66,12 @@ func downloadFile(url string, page int, c *Comic) error {
return nil
}
// handleRequest sends a GET request to the provided URL, mimicking a generic browser,
// and returns the HTTP response.
//
// url - the URL to send the request to.
// *http.Response - the HTTP response from the server.
// error - an error that occurred during the request.
func handleRequest(url string) (*http.Response, error) {
// adjust timeout and keep-alive to avoid connection timeout
transport := &http.Transport{
@@ -180,16 +121,12 @@ func handleRequest(url string) (*http.Response, error) {
return res, nil
}
// Download is a method of the Comic struct that downloads multiple files concurrently.
//
// It takes an integer parameter `concurrency` which represents the number of concurrent downloads.
//
// It returns a slice of errors, each representing an error that occurred during the download process.
func (c *Comic) Download(concurrency int) []error {
// var wg sync.WaitGroup
// wg.Add(len(c.Filelist))
// for i, link := range c.Filelist {
// go downloadFile(link, i+1, c)
// }
// wg.Wait()
// return nil
jobs := make(chan Download)
results := make(chan error)
@@ -221,34 +158,12 @@ type Download struct {
Comic *Comic
}
// workerPool is a function that processes a channel of Download jobs concurrently.
//
// It takes two parameters: a receive-only channel of Download jobs and a send-only channel of errors.
// It returns no value, but sends errors to the results channel as they occur.
func workerPool(jobs <-chan Download, results chan<- error) {
for job := range jobs {
results <- downloadFile(job.URL, job.Page, job.Comic)
}
}
func DownloadComicImages(c *Comic, concurrency int) []error {
jobs := make(chan Download)
results := make(chan error)
for worker := 1; worker <= concurrency; worker++ {
go workerPool(jobs, results)
}
for i, url := range c.Filelist {
jobs <- Download{
URL: url,
Page: i + 1,
Comic: c,
}
}
var errors []error
for i := 0; i < len(c.Filelist); i++ {
err := <-results
if err != nil {
errors = append(errors, err)
}
}
return errors
}