diff options
author | Christian Pointner <equinox@helsinki.at> | 2016-08-04 16:25:04 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2016-08-04 16:25:04 (GMT) |
commit | 9561889b4c683f602eb9623deeec2bd020d486da (patch) | |
tree | ec7c18db53fef5b3d51ead735ed3f2712aed236c | |
parent | 6107b08a3f4f66bcbb5da9a84b43f19030d0fbc6 (diff) |
some more bugfixes for new source file policy semantics
-rw-r--r-- | rhimport/core.go | 11 | ||||
-rw-r--r-- | rhimport/fetcher.go | 100 | ||||
-rw-r--r-- | rhimport/session.go | 5 |
3 files changed, 55 insertions, 61 deletions
diff --git a/rhimport/core.go b/rhimport/core.go index 2acec51..12ad35c 100644 --- a/rhimport/core.go +++ b/rhimport/core.go @@ -206,6 +206,9 @@ func (ctx *Context) SanityCheck() error { if ctx.Password == "" && !ctx.Trusted { return fmt.Errorf("empty Password on untrusted control interface is not allowed") } + if ctx.WorkDir == "" { + return fmt.Errorf("empty WorkDir is not allowed") + } if ctx.ShowId != 0 { if ctx.ShowId != 0 && ctx.ShowId > CART_MAX { return fmt.Errorf("ShowId %d is outside of allowed range (0 < show-id < %d)", ctx.ShowId, CART_MAX) @@ -259,6 +262,12 @@ func (ctx *Context) CreateTempWorkDir() (err error) { return } +func (ctx *Context) SwitchTempWorkDir(newDir string) { + ctx.RemoveTempWorkDir() + ctx.WorkDir = newDir + return +} + func (ctx *Context) RemoveTempWorkDir() { if err := os.RemoveAll(ctx.WorkDir); err != nil { ctx.stdlog.Printf("Error removing WorkDir: %s", err) @@ -313,7 +322,7 @@ func (ctx *Context) reportProgress(step int, stepName string, current, total flo title := ctx.Title if title == "" { title = path.Base(ctx.OrigFilename) - if title == "" { + if title == "" || title == "." { title = path.Base(ctx.SourceFile) } } diff --git a/rhimport/fetcher.go b/rhimport/fetcher.go index 6c113a4..ea12806 100644 --- a/rhimport/fetcher.go +++ b/rhimport/fetcher.go @@ -247,11 +247,6 @@ func fetchFileCurl(ctx *Context, res *Result, uri *url.URL) (err error) { convOut, convErr = cbdata.conv.GetResult(ctx, res) } if err != nil || cbdata.writeError != nil || convErr != nil { - if cbdata.conv != nil { - ctx.dbglog.Printf("Removing stale file: %s", cbdata.filename) - os.Remove(cbdata.filename) - os.Remove(path.Dir(cbdata.filename)) - } if res.ResponseCode == http.StatusNoContent { ctx.stdlog.Printf("download of '%s' got canceled", ctx.SourceUri) return nil @@ -369,11 +364,6 @@ func fetchFileArchiv(ctx *Context, res *Result, uri *url.URL) (err error) { convOut, convErr = cbdata.conv.GetResult(ctx, res) } if err != nil || cbdata.writeError != nil || convErr != nil { - if cbdata.conv != nil { - ctx.dbglog.Printf("Removing stale file: %s", cbdata.filename) - os.Remove(cbdata.filename) - os.Remove(path.Dir(cbdata.filename)) - } if res.ResponseCode == http.StatusNoContent { ctx.stdlog.Printf("download of '%s' got canceled", ctx.SourceUri) return nil @@ -395,15 +385,35 @@ func fetchFileArchiv(ctx *Context, res *Result, uri *url.URL) (err error) { return } -func fetchFileLocal(ctx *Context, res *Result, uri *url.URL) (err error) { - return fetchFileDir(ctx, res, uri, ctx.conf.LocalFetchDir, true) -} - func fetchFileTmp(ctx *Context, res *Result, uri *url.URL) (err error) { - return fetchFileDir(ctx, res, uri, ctx.conf.TempDir, false) + ctx.stdlog.Printf("tmp fetcher called for '%s'", ctx.SourceUri) + + ctx.SourceFile = filepath.Join(ctx.conf.TempDir, path.Clean("/"+uri.Path)) + + size := int64(0) + if info, err := os.Stat(ctx.SourceFile); err != nil { + res.ResponseCode = http.StatusBadRequest + res.ErrorString = fmt.Sprintf("local-file stat(): %s", err) + return nil + } else { + size = info.Size() + if info.IsDir() { + res.ResponseCode = http.StatusBadRequest + res.ErrorString = fmt.Sprintf("'%s' is a directory", ctx.SourceFile) + return nil + } + } + ctx.dbglog.Printf("1: Title = '%s', OrigName = '%s', SourceFile = '%s'", ctx.Title, ctx.OrigFilename, ctx.SourceFile) + ctx.reportProgress(1, "fetching", 0.0, float64(size)) + oldWorkDir := filepath.Dir(ctx.SourceFile) + ctx.dbglog.Printf("switching over to old workdir: %s", oldWorkDir) + ctx.SwitchTempWorkDir(oldWorkDir) + ctx.reportProgress(1, "fetching", float64(size), float64(size)) + ctx.dbglog.Printf("2: Title = '%s', OrigName = '%s', SourceFile = '%s'", ctx.Title, ctx.OrigFilename, ctx.SourceFile) + return } -func fetchFileDirConvert(ctx *Context, res *Result, origSrc *os.File, sizeTotal int64) (err error) { +func fetchFileConvert(ctx *Context, res *Result, origSrc io.Reader, sizeTotal int64) (err error) { origDir, origFile := path.Split(ctx.SourceFile) ctx.OrigFilename = ctx.SourceFile @@ -421,7 +431,9 @@ func fetchFileDirConvert(ctx *Context, res *Result, origSrc *os.File, sizeTotal r, err = origSrc.Read(buffer[:]) if err == io.EOF { err = nil - break + if r <= 0 { + break + } } if err != nil { ctx.stdlog.Printf("Unable to read from source file %s: %s", origDir+origFile, err) @@ -462,13 +474,14 @@ func fetchFileDirConvert(ctx *Context, res *Result, origSrc *os.File, sizeTotal if err != nil { return err } + ctx.reportProgress(1, "fetching", float64(sizeTotal), float64(sizeTotal)) return } -func fetchFileDir(ctx *Context, res *Result, uri *url.URL, dir string, convert bool) (err error) { - ctx.stdlog.Printf("Dir fetcher called for '%s'", ctx.SourceUri) +func fetchFileLocal(ctx *Context, res *Result, uri *url.URL) (err error) { + ctx.stdlog.Printf("local fetcher called for '%s'", ctx.SourceUri) - ctx.SourceFile = filepath.Join(dir, path.Clean("/"+uri.Path)) + ctx.SourceFile = filepath.Join(ctx.conf.LocalFetchDir, path.Clean("/"+uri.Path)) var src *os.File if src, err = os.Open(ctx.SourceFile); err != nil { res.ResponseCode = http.StatusBadRequest @@ -497,13 +510,7 @@ func fetchFileDir(ctx *Context, res *Result, uri *url.URL, dir string, convert b } ctx.reportProgress(1, "fetching", 0.0, float64(size)) - if convert { - if err = fetchFileDirConvert(ctx, res, src, size); err != nil { - return - } - } - ctx.reportProgress(1, "fetching", float64(size), float64(size)) - return + return fetchFileConvert(ctx, res, src, size) } func fetchFileSilence(ctx *Context, res *Result, uri *url.URL) error { @@ -517,43 +524,18 @@ func fetchFileSilence(ctx *Context, res *Result, uri *url.URL) error { } duration := time.Duration(d) * 100 * time.Millisecond + ctx.SourceFile = "silence.wav" + ctx.Title = fmt.Sprintf("%v of total silence...", duration) + ctx.ExtraMetaData["TITLE"] = ctx.Title wav, err := newPCMWavFile(uint32(ctx.conf.SampleRate), 16, uint16(ctx.Channels), duration) if err != nil { return err } - fileSize := wav.GetFileSize() + size := wav.GetFileSize() wav.generator = NewSilenceGenerator() - uri.Scheme = "attachment" - uri.Host = strconv.FormatUint(uint64(fileSize), 10) - uri.Path = "silence.wav" - - go func() { - for { - chunk := AttachmentChunk{} - var data [32 * 1024]byte - n, err := wav.Read(data[:]) - if n > 0 { - chunk.Data = data[:n] - if err == io.EOF { - err = nil - } - } - chunk.Error = err - if err == io.EOF { - return - } - - // TODO: handle cancel - ctx.AttachmentChan <- chunk - - if err != nil { - return - } - } - }() - - return fetchFileAttachment(ctx, res, uri) + ctx.reportProgress(1, "fetching", 0.0, float64(size)) + return fetchFileConvert(ctx, res, wav, int64(size)) } func writeAttachmentFile(ctx *Context, res *Result, sizeTotal uint64, conv fetchConverter) error { @@ -706,7 +688,7 @@ func checkPassword(ctx *Context, res *Result) (err error) { func FetchFile(ctx *Context) (res *Result, err error) { res = &Result{ResponseCode: http.StatusOK} - ctx.stdlog.Println("FetchFile: called for '%s'", ctx.SourceUri) + ctx.stdlog.Printf("FetchFile: called for '%s'", ctx.SourceUri) var uri *url.URL if uri, err = url.Parse(ctx.SourceUri); err != nil { diff --git a/rhimport/session.go b/rhimport/session.go index 1e6b329..5bec54d 100644 --- a/rhimport/session.go +++ b/rhimport/session.go @@ -27,6 +27,7 @@ package rhimport import ( "fmt" "net/http" + "path" "strings" "time" ) @@ -140,7 +141,9 @@ func (s *session) runner() { res.ResponseCode = http.StatusInternalServerError res.ErrorString = err.Error() } - res.SourceFile = "tmp://" + strings.TrimPrefix(s.ctx.SourceFile, s.ctx.conf.TempDir) + if s.ctx.SourceFilePolicy == Keep { + res.SourceFile = "tmp://" + path.Clean("/"+strings.TrimPrefix(s.ctx.SourceFile, s.ctx.conf.TempDir)) + } s.doneIntChan <- *res } |