diff options
Diffstat (limited to 'rhimport')
-rw-r--r-- | rhimport/fetcher.go | 83 |
1 files changed, 73 insertions, 10 deletions
diff --git a/rhimport/fetcher.go b/rhimport/fetcher.go index 77f00e2..5c26637 100644 --- a/rhimport/fetcher.go +++ b/rhimport/fetcher.go @@ -31,6 +31,7 @@ import ( "net/http" "net/url" "os" + "os/user" "path" "path/filepath" "strconv" @@ -157,6 +158,77 @@ func fetchFileCurl(ctx *Context, res *Result, uri *url.URL) (err error) { return } +func generateArchivFilePath(uri string) (file, path string, err error) { + //TODO: parse date/time from uri and create filename + file = "2016-02-29-1500.ogg" + path = "/srv/archiv/2016/02-Februar/29-Montag" + + return +} + +func fetchFileArchiv(ctx *Context, res *Result, uri *url.URL) (err error) { + rhdl.Printf("archiv fetcher called for '%s'", ctx.SourceUri) + + var srcfile, srcpath string + if srcfile, srcpath, err = generateArchivFilePath(ctx.SourceUri); err != nil { + res.ResponseCode = http.StatusBadRequest + res.ErrorString = fmt.Sprintf("date/time is invalid: %s", err) + return nil + } + + easy := curl.EasyInit() + if easy != nil { + defer easy.Cleanup() + + // TODO: make user and host configurable + scpuri := fmt.Sprintf("sftp://archiv.helsinki.at%s/%s", srcpath, srcfile) + easy.Setopt(curl.OPT_URL, scpuri) + easy.Setopt(curl.OPT_USERNAME, "rhimport") + + u, _ := user.Current() + 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.filename = fmt.Sprintf("%s/%s", destpath, srcfile) + + easy.Setopt(curl.OPT_WRITEFUNCTION, curlWriteCallback) + easy.Setopt(curl.OPT_WRITEDATA, cbdata) + + easy.Setopt(curl.OPT_NOPROGRESS, false) + easy.Setopt(curl.OPT_PROGRESSFUNCTION, curlProgressCallback) + easy.Setopt(curl.OPT_PROGRESSDATA, cbdata) + + rhdl.Printf("importing archiv file using from %s", scpuri) + if err = easy.Perform(); err != nil { + if cbdata.file != nil { + rhdl.Printf("Removing stale file: %s", cbdata.filename) + os.Remove(cbdata.filename) + os.Remove(path.Dir(cbdata.filename)) + } + if res.ResponseCode == http.StatusNoContent { + err = nil + } else { + err = fmt.Errorf("archiv fetcher('%s'): %s", ctx.SourceUri, err) + } + return + } + + ctx.SourceFile = cbdata.filename + ctx.DeleteSourceFile = true + ctx.DeleteSourceDir = true + } else { + err = fmt.Errorf("Error initializing libcurl") + } + + return nil +} + func fetchFileLocal(ctx *Context, res *Result, uri *url.URL) (err error) { rhl.Printf("Local fetcher called for '%s'", ctx.SourceUri) if ctx.ProgressCallBack != nil { @@ -228,15 +300,6 @@ func fetchFileFake(ctx *Context, res *Result, uri *url.URL) error { return nil } -func fetchFileArchiv(ctx *Context, res *Result, uri *url.URL) error { - rhdl.Printf("archiv fetcher for '%s'", ctx.SourceUri) - - res.ResponseCode = http.StatusNotImplemented - res.ErrorString = "archiv fetcher is not finished yet" - // TODO: implement this - return nil -} - type FetchFunc func(*Context, *Result, *url.URL) (err error) // TODO: implement fetchers for: @@ -258,7 +321,7 @@ func fetcherInit() { info := curl.VersionInfo(curl.VERSION_FIRST) protos := info.Protocols for _, proto := range protos { - if proto == "scp" { + if proto == "sftp" { rhdl.Printf("curl: enabling protocol %s", proto) fetchers["archiv"] = fetchFileArchiv archiveEnabled = true |