From 8134f3159f30da5560a3d515f4da834eb06bf8dc Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 30 Jun 2016 13:46:23 +0200 Subject: make fetch converter configurable diff --git a/rhimport/converter.go b/rhimport/converter.go index 4b68faa..418d27b 100644 --- a/rhimport/converter.go +++ b/rhimport/converter.go @@ -25,6 +25,7 @@ package rhimport import ( + "errors" "io" "os" "os/exec" @@ -32,7 +33,7 @@ import ( "strings" ) -type Converter interface { +type FetchConverter interface { io.WriteCloser GetResult() (result string, err error) } @@ -42,31 +43,41 @@ type ConverterResult struct { err error } +func NewFetchConverter(convType, filename string) (FetchConverter, string, error) { + switch convType { + case "null": + return NewNullFetchConverter(filename) + case "ffmpeg": + return NewFFMpegFetchConverter(filename) + } + return nil, "", errors.New("unknown fetch converter type: " + convType) +} + // // NUll Converter aka File Writer // -type NullConverter struct { +type NullFetchConverter struct { file *os.File } -func NewNullConverter(filename string) (n *NullConverter, newFilename string, err error) { - n = &NullConverter{} +func NewNullFetchConverter(filename string) (n *NullFetchConverter, newFilename string, err error) { + n = &NullFetchConverter{} rhl.Printf("null-converter: opening file '%s'", filename) newFilename = filename n.file, err = os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600) return } -func (c *NullConverter) Write(p []byte) (n int, err error) { +func (c *NullFetchConverter) Write(p []byte) (n int, err error) { return c.file.Write(p) } -func (c *NullConverter) Close() (err error) { +func (c *NullFetchConverter) Close() (err error) { return c.file.Close() } -func (c *NullConverter) GetResult() (result string, err error) { +func (c *NullFetchConverter) GetResult() (result string, err error) { return "", nil } @@ -74,14 +85,14 @@ func (c *NullConverter) GetResult() (result string, err error) { // FFMpeg Converter: converts all files into flac // -type FFMpegConverter struct { +type FFMpegFetchConverter struct { cmd *exec.Cmd pipe io.WriteCloser result chan ConverterResult } -func NewFFMpegConverter(filename string) (ff *FFMpegConverter, filenameFlac string, err error) { - ff = &FFMpegConverter{} +func NewFFMpegFetchConverter(filename string) (ff *FFMpegFetchConverter, filenameFlac string, err error) { + ff = &FFMpegFetchConverter{} ext := filepath.Ext(filename) filenameFlac = strings.TrimSuffix(filename, ext) + ".flac" rhl.Printf("ffmpeg-converter: starting ffmpeg for file '%s' (had extension: '%s')", filenameFlac, ext) @@ -98,15 +109,15 @@ func NewFFMpegConverter(filename string) (ff *FFMpegConverter, filenameFlac stri return } -func (ff *FFMpegConverter) Write(p []byte) (n int, err error) { +func (ff *FFMpegFetchConverter) Write(p []byte) (n int, err error) { return ff.pipe.Write(p) } -func (ff *FFMpegConverter) Close() (err error) { +func (ff *FFMpegFetchConverter) Close() (err error) { return ff.pipe.Close() } -func (ff *FFMpegConverter) GetResult() (result string, err error) { +func (ff *FFMpegFetchConverter) GetResult() (result string, err error) { if ff.result != nil { r := <-ff.result return r.output, r.err diff --git a/rhimport/core.go b/rhimport/core.go index 6a495a4..413e39c 100644 --- a/rhimport/core.go +++ b/rhimport/core.go @@ -123,6 +123,7 @@ type Context struct { UseMetaData bool SourceUri string AttachmentChan <-chan []byte + FetchConverter string SourceFile string DeleteSourceFile bool DeleteSourceDir bool @@ -150,6 +151,7 @@ func NewContext(conf *Config, db *rddb.DBChan) *Context { ctx.AutotrimLevel = conf.ImportParamDefaults.AutotrimLevel ctx.UseMetaData = conf.ImportParamDefaults.UseMetaData ctx.AttachmentChan = nil + ctx.FetchConverter = "ffmpeg" ctx.SourceFile = "" ctx.DeleteSourceFile = false ctx.DeleteSourceDir = false diff --git a/rhimport/fetcher.go b/rhimport/fetcher.go index fde9da8..f6e940c 100644 --- a/rhimport/fetcher.go +++ b/rhimport/fetcher.go @@ -47,16 +47,10 @@ type FetcherCurlCBData struct { basepath string filename string remotename string - conv Converter + conv FetchConverter writeError error } -func (self *FetcherCurlCBData) Cleanup() { - if self.conv != nil { - self.conv.Close() - } -} - func curlHeaderCallback(ptr []byte, userdata interface{}) bool { hdr := fmt.Sprintf("%s", ptr) data := userdata.(*FetcherCurlCBData) @@ -82,7 +76,7 @@ func curlWriteCallback(ptr []byte, userdata interface{}) bool { } data.filename = filepath.Join(data.basepath, name) } - nc, newFilename, err := NewFFMpegConverter(data.filename) + nc, newFilename, err := NewFetchConverter(data.ctx.FetchConverter, data.filename) if err != nil { rhl.Printf("Unable to create converter for file %s: %s", data.filename, err) data.writeError = err @@ -141,7 +135,6 @@ func fetchFileCurl(ctx *Context, res *Result, uri *url.URL) (err error) { easy.Setopt(curl.OPT_USERAGENT, "Radio Helsinki Import") cbdata := &FetcherCurlCBData{ctx: ctx, res: res, remotename: path.Base(uri.Path)} - defer cbdata.Cleanup() if cbdata.basepath, err = ioutil.TempDir(ctx.conf.TempDir, "rhimportd-"); err != nil { return } @@ -157,9 +150,13 @@ func fetchFileCurl(ctx *Context, res *Result, uri *url.URL) (err error) { easy.Setopt(curl.OPT_PROGRESSDATA, cbdata) err = easy.Perform() - cbdata.conv.Close() - rhl.Printf("waiting for converter to finish...") - convOut, convErr := cbdata.conv.GetResult() + var convOut string + var convErr error + if cbdata.conv != nil { + cbdata.conv.Close() + rhl.Printf("waiting for converter to finish...") + convOut, convErr = cbdata.conv.GetResult() + } if err != nil || cbdata.writeError != nil || convErr != nil { if cbdata.conv != nil { rhdl.Printf("Removing stale file: %s", cbdata.filename) @@ -257,12 +254,12 @@ func fetchFileArchiv(ctx *Context, res *Result, uri *url.URL) (err error) { easy.Setopt(curl.OPT_SSH_PUBLIC_KEYFILE, fmt.Sprintf("%s/.ssh/id_rsa.pub", u.HomeDir)) easy.Setopt(curl.OPT_SSH_PRIVATE_KEYFILE, fmt.Sprintf("%s/.ssh/id_rsa", u.HomeDir)) - cbdata := &FetcherCurlCBData{ctx: ctx, res: res} - defer cbdata.Cleanup() var destpath string if destpath, err = ioutil.TempDir(ctx.conf.TempDir, "rhimportd-"); err != nil { return } + + cbdata := &FetcherCurlCBData{ctx: ctx, res: res} cbdata.filename = fmt.Sprintf("%s/%s", destpath, srcfile) easy.Setopt(curl.OPT_WRITEFUNCTION, curlWriteCallback) @@ -274,9 +271,13 @@ func fetchFileArchiv(ctx *Context, res *Result, uri *url.URL) (err error) { rhdl.Printf("importing archiv file from %s", scpuri) err = easy.Perform() - cbdata.conv.Close() - rhl.Printf("waiting for converter to finish...") - convOut, convErr := cbdata.conv.GetResult() + var convOut string + var convErr error + if cbdata.conv != nil { + cbdata.conv.Close() + rhl.Printf("waiting for converter to finish...") + convOut, convErr = cbdata.conv.GetResult() + } if err != nil || cbdata.writeError != nil || convErr != nil { if cbdata.conv != nil { rhdl.Printf("Removing stale file: %s", cbdata.filename) -- cgit v0.10.2