diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/rhimportd/ctrlWeb.go | 4 | ||||
-rw-r--r-- | src/rhimportd/main.go | 33 | ||||
-rw-r--r-- | src/rhimportd/uploadWeb.go | 35 |
3 files changed, 61 insertions, 11 deletions
diff --git a/src/rhimportd/ctrlWeb.go b/src/rhimportd/ctrlWeb.go index 6ea641b..6bee325 100644 --- a/src/rhimportd/ctrlWeb.go +++ b/src/rhimportd/ctrlWeb.go @@ -44,13 +44,13 @@ func (self webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { self.H(self.Config, self.DBChan, self.SessionStoreChan, self.trusted, w, r) } -func StartControlWeb(addr string, conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport.SessionStoreChan) { +func StartControlWeb(addr string, uploadMaxAge time.Duration, conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport.SessionStoreChan) { // http.Handle("/trusted/simple", webHandler{conf, db, sessions, true, webSimpleHandler}) http.Handle("/public/simple", webHandler{conf, db, sessions, false, webSimpleHandler}) http.Handle("/public/socket", webHandler{conf, db, sessions, false, webSocketHandler}) http.Handle("/public/upload", webHandler{conf, db, sessions, false, webUploadHandler}) - go webUploadCleaner(conf) + go webUploadCleaner(conf, uploadMaxAge) rhl.Println("web-ctrl: listening on", addr) server := &http.Server{Addr: addr, ReadTimeout: 60 * time.Second, WriteTimeout: 60 * time.Second} diff --git a/src/rhimportd/main.go b/src/rhimportd/main.go index 4b0514b..68b0177 100644 --- a/src/rhimportd/main.go +++ b/src/rhimportd/main.go @@ -34,6 +34,7 @@ import ( "os" "os/signal" "sync" + "time" ) var ( @@ -66,6 +67,28 @@ func (s *envStringValue) Get() interface{} { return string(*s) } func (s *envStringValue) String() string { return fmt.Sprintf("%s", *s) } +type envDurationValue time.Duration + +func newEnvDurationValue(key string, dflt time.Duration) (*envDurationValue, error) { + if envval, exists := os.LookupEnv(key); exists { + var val envDurationValue + err := (&val).Set(envval) + return &val, err + } else { + return (*envDurationValue)(&dflt), nil + } +} + +func (d *envDurationValue) Set(val string) error { + dval, err := time.ParseDuration(val) + *d = envDurationValue(dval) + return err +} + +func (d *envDurationValue) Get() interface{} { return time.Duration(*d) } + +func (d *envDurationValue) String() string { return fmt.Sprintf("%v", *d) } + func main() { webAddr := newEnvStringValue("RHIMPORTD_WEB_ADDR", "localhost:4080") flag.Var(webAddr, "web-addr", "addr:port to listen on (environment: RHIMPORTD_WEB_ADDR)") @@ -81,6 +104,14 @@ func main() { flag.Var(tempDir, "tmp-dir", "path to temporary files (environment: RHIMPORTD_TEMP_DIR)") localFetchDir := newEnvStringValue("RHIMPORTD_LOCAL_FETCH_DIR", os.TempDir()) flag.Var(localFetchDir, "local-fetch-dir", "base path for local:// urls (environment: RHIMPORTD_LOCAL_FETCH_DIR)") + + uploadMaxAge, err := newEnvDurationValue("RHIMPORTD_UPLOAD_MAX_AGE", 30*time.Minute) + if err != nil { + rhl.Println("Error parsing RHIMPORTD_UPLOAD_MAX_AGE from environment:", err) + return + } + flag.Var(uploadMaxAge, "upload-max-age", "maximum age of uploaded files before the get deleted (environment: RHIMPORTD_UPLOAD_MAX_AGE)") + help := flag.Bool("help", false, "show usage") flag.Parse() @@ -112,7 +143,7 @@ func main() { go func() { defer wg.Done() rhl.Println("starting web-ctrl") - StartControlWeb(webAddr.Get().(string), conf, db.GetInterface(), sessions.GetInterface()) + StartControlWeb(webAddr.Get().(string), uploadMaxAge.Get().(time.Duration), conf, db.GetInterface(), sessions.GetInterface()) rhl.Println("web-ctrl finished") }() } 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() } |