From db0d524595106b1e907df04baa3da8eb17527f6d Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 10 Dec 2015 15:56:01 +0100 Subject: importer deletes source file and dir if requested diff --git a/fetcher.go b/fetcher.go index 1770343..b4e2f0c 100644 --- a/fetcher.go +++ b/fetcher.go @@ -27,6 +27,7 @@ package rhimport import ( "fmt" "github.com/golang-basic/go-curl" + "io/ioutil" "mime" "net/url" "os" @@ -95,7 +96,9 @@ func FetchFileCurl(ctx *ImportContext, uri *url.URL) (err error) { cbdata := &CurlCBData{remotename: path.Base(uri.Path)} defer cbdata.Cleanup() - cbdata.basepath = ctx.Config.TempDir // TODO: create temporary directory + if cbdata.basepath, err = ioutil.TempDir(ctx.Config.TempDir, "rhimportd-"); err != nil { + return + } easy.Setopt(curl.OPT_HEADERFUNCTION, curlHeaderCallback) easy.Setopt(curl.OPT_HEADERDATA, cbdata) @@ -121,6 +124,7 @@ func FetchFileCurl(ctx *ImportContext, uri *url.URL) (err error) { ctx.SourceFile = cbdata.filename ctx.DeleteSourceFile = true + ctx.DeleteSourceDir = true } return } @@ -129,6 +133,7 @@ func FetchFileLocal(ctx *ImportContext, uri *url.URL) (err error) { rhl.Printf("Local fetcher called for '%s'", ctx.SourceUri) ctx.SourceFile = uri.Path ctx.DeleteSourceFile = false + ctx.DeleteSourceDir = false return } diff --git a/importer.go b/importer.go index 0f8eea8..0d0a836 100644 --- a/importer.go +++ b/importer.go @@ -55,6 +55,7 @@ type ImportContext struct { SourceUri string SourceFile string DeleteSourceFile bool + DeleteSourceDir bool } func NewImportContext(conf *Config, rddb *RdDb, user string, group string) *ImportContext { @@ -74,6 +75,7 @@ func NewImportContext(conf *Config, rddb *RdDb, user string, group string) *Impo ctx.UseMetaData = false ctx.SourceFile = "" ctx.DeleteSourceFile = false + ctx.DeleteSourceDir = false return ctx } @@ -173,8 +175,27 @@ func import_audio(ctx *ImportContext) (err error) { return } +func cleanup_files(ctx *ImportContext) { + if(ctx.DeleteSourceFile) { + rhdl.Printf("importer: removing file: %s", ctx.SourceFile) + if err := os.Remove(ctx.SourceFile); err != nil { + rhl.Printf("importer: error removing source file: %s", err) + return + } + if(ctx.DeleteSourceDir) { + dir := path.Dir(ctx.SourceFile) + rhdl.Printf("importer: also removing directory: %s", dir) + if err := os.Remove(dir); err != nil { + rhl.Printf("importer: error removing source directory: %s", err) + } + } + } + return +} + func ImportFile(ctx *ImportContext) (err error) { rhl.Println("ImportFile called for", ctx.SourceFile) + defer cleanup_files(ctx) if ctx.Trusted { if err = ctx.getPassword(true); err != nil { @@ -182,5 +203,9 @@ func ImportFile(ctx *ImportContext) (err error) { } } - return import_audio(ctx) + if err = import_audio(ctx); err != nil { + return + } + + return } -- cgit v0.10.2