diff options
-rw-r--r-- | rhimport/core.go | 10 | ||||
-rw-r--r-- | rhimport/fetcher.go | 26 | ||||
-rw-r--r-- | rhimport/importer.go | 8 | ||||
-rw-r--r-- | rhimport/session.go | 21 |
4 files changed, 44 insertions, 21 deletions
diff --git a/rhimport/core.go b/rhimport/core.go index fd481e4..efb6e58 100644 --- a/rhimport/core.go +++ b/rhimport/core.go @@ -64,6 +64,14 @@ type Result struct { SourceFile string } +type FilePolicy int + +const ( + Auto FilePolicy = iota + Keep + Delete +) + type Context struct { conf *Config db *rddb.DBChan @@ -84,6 +92,7 @@ type Context struct { SourceFile string DeleteSourceFile bool DeleteSourceDir bool + SourceFilePolicy FilePolicy ProgressCallBack ProgressCB ProgressCallBackData interface{} Cancel <-chan bool @@ -109,6 +118,7 @@ func NewContext(conf *Config, db *rddb.DBChan) *Context { ctx.SourceFile = "" ctx.DeleteSourceFile = false ctx.DeleteSourceDir = false + ctx.SourceFilePolicy = Auto ctx.ProgressCallBack = nil ctx.Cancel = nil diff --git a/rhimport/fetcher.go b/rhimport/fetcher.go index a253eb4..1f1e84f 100644 --- a/rhimport/fetcher.go +++ b/rhimport/fetcher.go @@ -149,8 +149,10 @@ func fetchFileCurl(ctx *Context, res *Result, uri *url.URL) (err error) { } ctx.SourceFile = cbdata.filename - ctx.DeleteSourceFile = true - ctx.DeleteSourceDir = true + if ctx.SourceFilePolicy == Auto { + ctx.DeleteSourceFile = true + ctx.DeleteSourceDir = true + } } else { err = fmt.Errorf("Error initializing libcurl") } @@ -251,8 +253,10 @@ func fetchFileArchiv(ctx *Context, res *Result, uri *url.URL) (err error) { } ctx.SourceFile = cbdata.filename - ctx.DeleteSourceFile = true - ctx.DeleteSourceDir = true + if ctx.SourceFilePolicy == Auto { + ctx.DeleteSourceFile = true + ctx.DeleteSourceDir = true + } } else { err = fmt.Errorf("Error initializing libcurl") } @@ -292,8 +296,10 @@ func fetchFileLocal(ctx *Context, res *Result, uri *url.URL) (err error) { ctx.ProgressCallBack = nil } } - ctx.DeleteSourceFile = false - ctx.DeleteSourceDir = false + if ctx.SourceFilePolicy == Auto { + ctx.DeleteSourceFile = false + ctx.DeleteSourceDir = false + } return } @@ -325,8 +331,10 @@ func fetchFileFake(ctx *Context, res *Result, uri *url.URL) error { } } ctx.SourceFile = "/nonexistend/fake.mp3" - ctx.DeleteSourceFile = false - ctx.DeleteSourceDir = false + if ctx.SourceFilePolicy == Auto { + ctx.DeleteSourceFile = false + ctx.DeleteSourceDir = false + } } return nil } @@ -370,7 +378,7 @@ func fetcherInit() { } } if !archiveEnabled { - rhl.Printf("archiv: fetcher is disabled because the installed curl library version doesn't support scp!") + rhl.Printf("archiv: fetcher is disabled because the installed curl library version doesn't support sFTP!") } } diff --git a/rhimport/importer.go b/rhimport/importer.go index 6d994c3..c840bae 100644 --- a/rhimport/importer.go +++ b/rhimport/importer.go @@ -410,7 +410,7 @@ func addShowCartCut(ctx *Context, res *Result, carts []uint) (err error) { return } -func cleanupFiles(ctx *Context) { +func cleanupFiles(ctx *Context, res *Result) { if ctx.DeleteSourceFile { rhdl.Printf("importer: removing file: %s", ctx.SourceFile) if err := os.Remove(ctx.SourceFile); err != nil { @@ -424,12 +424,15 @@ func cleanupFiles(ctx *Context) { rhl.Printf("importer: error removing source directory: %s", err) } } + } else { + res.SourceFile = ctx.SourceFile } return } func ImportFile(ctx *Context) (res *Result, err error) { - defer cleanupFiles(ctx) + res = &Result{ResponseCode: http.StatusOK} + defer cleanupFiles(ctx, res) rhl.Printf("importer: ImportFile called with: show-id: %d, pool-name: '%s', cart/cut: %d/%d", ctx.ShowId, ctx.GroupName, ctx.Cart, ctx.Cut) @@ -448,7 +451,6 @@ func ImportFile(ctx *Context) (res *Result, err error) { rmCartOnErr := false rmCutOnErr := false - res = &Result{ResponseCode: http.StatusOK} if ctx.ShowId != 0 { // Import to a show var showCarts []uint if showCarts, err = ctx.getShowInfo(); err != nil { diff --git a/rhimport/session.go b/rhimport/session.go index f61c8fe..13d31cc 100644 --- a/rhimport/session.go +++ b/rhimport/session.go @@ -99,26 +99,29 @@ func sessionProgressCallback(step int, stepName string, progress float64, userda } func sessionRun(ctx Context, done chan<- Result) { - if err := ctx.SanityCheck(); err != nil { + err := ctx.SanityCheck() + if err != nil { done <- Result{ResponseCode: http.StatusBadRequest, ErrorString: err.Error()} return } - if res, err := FetchFile(&ctx); err != nil { + var res *Result + res, err = FetchFile(&ctx) + if err != nil { done <- Result{ResponseCode: http.StatusInternalServerError, ErrorString: err.Error()} return - } else if res.ResponseCode != http.StatusOK { + } + if res.ResponseCode != http.StatusOK { done <- *res return } - if res, err := ImportFile(&ctx); err != nil { - done <- Result{ResponseCode: http.StatusInternalServerError, ErrorString: err.Error()} - return - } else { - done <- *res - return + res, err = ImportFile(&ctx) + if err != nil { + res.ResponseCode = http.StatusInternalServerError + res.ErrorString = err.Error() } + done <- *res } func (self *Session) run(timeout time.Duration) { |