diff options
Diffstat (limited to 'src/rhimportd/uploadWeb.go')
-rw-r--r-- | src/rhimportd/uploadWeb.go | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/src/rhimportd/uploadWeb.go b/src/rhimportd/uploadWeb.go index 1e3e7f0..3a4e7bc 100644 --- a/src/rhimportd/uploadWeb.go +++ b/src/rhimportd/uploadWeb.go @@ -117,7 +117,7 @@ func webUploadHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport } defer src.Close() - rhl.Printf("WebUploadHandler: got request from user '%s', filename='%s'", username, hdr.Filename) + rhdl.Printf("WebUploadHandler: got request from user '%s', filename='%s'", username, hdr.Filename) dstpath, err := ioutil.TempDir(conf.TempDir, "webupload-"+username+"-") if err != nil { @@ -135,12 +135,18 @@ func webUploadHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport } defer dst.Close() - io.Copy(dst, src) + size, err := io.Copy(dst, src) + if err != nil { + rhl.Printf("WebUploadHandler: error storing uploaded file '%s': %v", dstfile, err) + webUploadErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + rhl.Printf("WebUploadHandler: stored file '%s' (size: %d Bytes)", dstfile, size) webUploadResponse(w, "tmp://"+strings.TrimPrefix(dstfile, conf.TempDir)) } -func webUploadCleanerRun(dir *os.File, conf *rhimport.Config) { - t := time.NewTicker(5 * time.Second) +func webUploadCleanerRun(dir *os.File, conf *rhimport.Config, maxAge time.Duration) { + t := time.NewTicker(1 * time.Minute) defer t.Stop() for now := range t.C { var err error @@ -156,13 +162,25 @@ func webUploadCleanerRun(dir *os.File, conf *rhimport.Config) { } for _, entry := range entries { - rhdl.Printf("webUploadCleaner: found file/dir '%s', Age: %v", entry.Name(), now.Sub(entry.ModTime())) - // TODO: delete file if it is a webupload and it is old enough + if !strings.HasPrefix(entry.Name(), "webupload-") { + continue + } + + age := now.Sub(entry.ModTime()) + if age <= maxAge { + continue + } + + if err := os.RemoveAll(conf.TempDir + entry.Name()); err != nil { + rhl.Printf("webUploadCleaner: deleting dir '%s' failed: %v", entry.Name(), err) + continue + } + rhl.Printf("webUploadCleaner: successfully deleted dir '%s', Age: %v", entry.Name(), age) } } } -func webUploadCleaner(conf *rhimport.Config) { +func webUploadCleaner(conf *rhimport.Config, maxAge time.Duration) { first := true for { if !first { @@ -186,7 +204,8 @@ func webUploadCleaner(conf *rhimport.Config) { continue } } - webUploadCleanerRun(dir, conf) + rhdl.Printf("webUploadCleaner: running with max age: %v", maxAge) + webUploadCleanerRun(dir, conf, maxAge) rhdl.Printf("webUploadCleanerRun: returned - restring in 5 sec...") dir.Close() } |