diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-12-10 14:56:01 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-12-10 14:56:01 (GMT) |
commit | db0d524595106b1e907df04baa3da8eb17527f6d (patch) | |
tree | 56b0c6a9f3041a79d75cefc0a4eca06655771c91 | |
parent | 24f401a03888c0d2456b472f55866269ce121007 (diff) |
importer deletes source file and dir if requested
-rw-r--r-- | fetcher.go | 7 | ||||
-rw-r--r-- | importer.go | 27 |
2 files changed, 32 insertions, 2 deletions
@@ -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 } |