diff options
-rw-r--r-- | rhimport/core.go | 4 | ||||
-rw-r--r-- | rhimport/fetcher.go | 6 | ||||
-rw-r--r-- | rhimport/importer.go | 2 | ||||
-rw-r--r-- | rhimport/normalizer.go | 30 | ||||
-rw-r--r-- | rhimport/session.go | 9 | ||||
-rw-r--r-- | rhimport/youtubedl_responses.go | 15 |
6 files changed, 42 insertions, 24 deletions
diff --git a/rhimport/core.go b/rhimport/core.go index d5d7549..cf51c54 100644 --- a/rhimport/core.go +++ b/rhimport/core.go @@ -264,3 +264,7 @@ func (ctx *Context) reportProgress(step int, stepName string, current, total flo } } } + +func (ctx *Context) isCanceled() bool { + return ctx.Cancel != nil && len(ctx.Cancel) > 0 +} diff --git a/rhimport/fetcher.go b/rhimport/fetcher.go index b4380fa..f176e03 100644 --- a/rhimport/fetcher.go +++ b/rhimport/fetcher.go @@ -115,7 +115,7 @@ func curlProgressCallback(dltotal, dlnow, ultotal, ulnow float64, userdata inter return false } - if data.ctx.Cancel != nil && len(data.ctx.Cancel) > 0 { + if data.ctx.isCanceled() { data.res.ResponseCode = http.StatusNoContent data.res.ErrorString = "canceled" return false @@ -443,7 +443,7 @@ func fetchFileDirConvert(ctx *Context, res *Result, origSrc *os.File, sizeTotal written += uint64(w) ctx.reportProgress(1, "fetching", float64(written), float64(sizeTotal)) - if ctx.Cancel != nil && len(ctx.Cancel) > 0 { + if ctx.isCanceled() { res.ResponseCode = http.StatusNoContent res.ErrorString = "canceled" break @@ -549,7 +549,7 @@ func fetchFileFake(ctx *Context, res *Result, uri *url.URL) error { } for i := uint(0); i < uint(duration); i++ { - if ctx.Cancel != nil && len(ctx.Cancel) > 0 { + if ctx.isCanceled() { rhl.Printf("faking got canceled") res.ResponseCode = http.StatusNoContent res.ErrorString = "canceled" diff --git a/rhimport/importer.go b/rhimport/importer.go index 37bbc01..d7365a5 100644 --- a/rhimport/importer.go +++ b/rhimport/importer.go @@ -305,7 +305,7 @@ func importAudio(ctx *Context, res *Result) (err error) { easy.Setopt(curl.OPT_NOPROGRESS, false) easy.Setopt(curl.OPT_PROGRESSFUNCTION, func(dltotal, dlnow, ultotal, ulnow float64, userdata interface{}) bool { - if ctx.Cancel != nil && len(ctx.Cancel) > 0 { + if ctx.isCanceled() { res.ResponseCode = http.StatusNoContent res.ErrorString = "canceled" return false diff --git a/rhimport/normalizer.go b/rhimport/normalizer.go index 422acaa..0be9938 100644 --- a/rhimport/normalizer.go +++ b/rhimport/normalizer.go @@ -27,6 +27,7 @@ package rhimport import ( "fmt" "io" + "net/http" "os" "os/exec" "path" @@ -39,11 +40,13 @@ type ffmpegResult struct { err error } -func runNormalizer(ctx *Context, src *os.File, size int64) (err error) { +func runNormalizer(ctx *Context, res *Result, src *os.File, size int64) (err error) { if ctx.DeleteSourceFile { defer os.Remove(src.Name()) } + ctx.reportProgress(2, "normalizing", 0.0, float64(size)) + basepath, filename := filepath.Split(src.Name()) ext := filepath.Ext(filename) destName := strings.TrimSuffix(filename, ext) + "_normalized.flac" @@ -65,6 +68,13 @@ func runNormalizer(ctx *Context, src *os.File, size int64) (err error) { var written int64 for { + if ctx.isCanceled() { + ffmpeg.Process.Kill() + res.ResponseCode = http.StatusNoContent + res.ErrorString = "canceled" + return nil + } + var w int64 w, err = io.CopyN(ffstdin, src, 512*1024) if err != nil { @@ -74,17 +84,20 @@ func runNormalizer(ctx *Context, src *os.File, size int64) (err error) { ctx.reportProgress(2, "normalizing", float64(written), float64(size)) } ffstdin.Close() - res := <-ffresult - if res.err != nil { - return fmt.Errorf("normalizer exited with error: %q, ffmpeg output: %s", res.err, res.output) + r := <-ffresult + if r.err != nil { + return fmt.Errorf("normalizer exited with error: %q, ffmpeg output: %s", r.err, r.output) } if err != nil && err != io.EOF { return } + ctx.reportProgress(2, "normalizing", float64(size), float64(size)) return nil } -func NormalizeFile(ctx *Context) (err error) { +func NormalizeFile(ctx *Context) (res *Result, err error) { + res = &Result{ResponseCode: http.StatusOK} + if ctx.LoudnessCorr == 0.0 { rhl.Println("NormalizeFile: skipping normalization since the gain = 0.0dB") ctx.reportProgress(2, "normalizing", 1.0, 1.0) @@ -101,13 +114,12 @@ func NormalizeFile(ctx *Context) (err error) { size := int64(0) if info, err := src.Stat(); err != nil { - return err + return res, err } else { size = info.Size() } - ctx.reportProgress(2, "normalizing", 0.0, float64(size)) - if err = runNormalizer(ctx, src, size); err != nil { + if err = runNormalizer(ctx, res, src, size); err != nil { rhl.Println("NormalizeFile error:", err) if ctx.DeleteSourceFile { os.Remove(ctx.SourceFile) @@ -117,7 +129,5 @@ func NormalizeFile(ctx *Context) (err error) { } return } - ctx.reportProgress(2, "normalizing", float64(size), float64(size)) - return } diff --git a/rhimport/session.go b/rhimport/session.go index 2059525..a5f4a04 100644 --- a/rhimport/session.go +++ b/rhimport/session.go @@ -120,13 +120,16 @@ func sessionRun(ctx Context, done chan<- Result) { return } - if err = NormalizeFile(&ctx); err != nil { + if res, err = NormalizeFile(&ctx); err != nil { done <- Result{ResponseCode: http.StatusInternalServerError, ErrorString: err.Error()} return } + if res.ResponseCode != http.StatusOK { + done <- *res + return + } - res, err = ImportFile(&ctx) - if err != nil { + if res, err = ImportFile(&ctx); err != nil { res.ResponseCode = http.StatusInternalServerError res.ErrorString = err.Error() } diff --git a/rhimport/youtubedl_responses.go b/rhimport/youtubedl_responses.go index 1a8c0ca..6f6d7ee 100644 --- a/rhimport/youtubedl_responses.go +++ b/rhimport/youtubedl_responses.go @@ -31,13 +31,14 @@ import ( ) type YoutubeDLInfo struct { - ID string `json:"id"` - Title string `json:"title"` - URL string `json:"url"` - Extractor string `json:"extractor"` - Ext string `json:"ext"` - Protocol string `json:"protocol"` - HTTPHeaders map[string]string `json:"http_headers"` + ID string `json:"id"` + Title string `json:"title"` + URL string `json:"url"` + Extractor string `json:"extractor"` + ExtractorKey string `json:"extractor_key"` + Ext string `json:"ext"` + Protocol string `json:"protocol"` + HTTPHeaders map[string]string `json:"http_headers"` } func NewYoutubeDLInfoFromJSON(data io.Reader) (res *YoutubeDLInfo, err error) { |