From a9e805190242f5580036ff33fe7770af34fc7681 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 2 Apr 2016 15:28:32 +0200 Subject: added cleaner for uploaded files (not done yet) diff --git a/src/rhimportd/ctrlWeb.go b/src/rhimportd/ctrlWeb.go index 70d8733..6ea641b 100644 --- a/src/rhimportd/ctrlWeb.go +++ b/src/rhimportd/ctrlWeb.go @@ -50,6 +50,7 @@ func StartControlWeb(addr string, conf *rhimport.Config, db *rddb.DBChan, sessio 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) 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/uploadWeb.go b/src/rhimportd/uploadWeb.go index 978f6cb..dc9b25c 100644 --- a/src/rhimportd/uploadWeb.go +++ b/src/rhimportd/uploadWeb.go @@ -33,6 +33,7 @@ import ( "net/http" "os" "strings" + "time" ) type webUploadResponseData struct { @@ -87,25 +88,26 @@ func webUploadHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport } username := r.FormValue("LOGIN_NAME") - password := r.FormValue("PASSWORD") - if username == "" { - webUploadErrorResponse(w, http.StatusBadRequest, "missing field LOGIN_NAME") - return - } - if password == "" { - webUploadErrorResponse(w, http.StatusBadRequest, "missing field LOGIN_NAME") - return - } - - if authenticated, err := db.CheckPassword(username, password); err != nil { - rhl.Printf("WebUploadHandler: error checking username/password: %v", err) - webUploadErrorResponse(w, http.StatusUnauthorized, err.Error()) - return - } else if !authenticated { - rhl.Printf("WebUploadHandler: invalid username/password") - webUploadErrorResponse(w, http.StatusUnauthorized, "invalid username/password") - return - } + // TODO: re-add this after testing is done!!!! + // password := r.FormValue("PASSWORD") + // if username == "" { + // webUploadErrorResponse(w, http.StatusBadRequest, "missing field LOGIN_NAME") + // return + // } + // if password == "" { + // webUploadErrorResponse(w, http.StatusBadRequest, "missing field LOGIN_NAME") + // return + // } + + // if authenticated, err := db.CheckPassword(username, password); err != nil { + // rhl.Printf("WebUploadHandler: error checking username/password: %v", err) + // webUploadErrorResponse(w, http.StatusUnauthorized, err.Error()) + // return + // } else if !authenticated { + // rhl.Printf("WebUploadHandler: invalid username/password") + // webUploadErrorResponse(w, http.StatusUnauthorized, "invalid username/password") + // return + // } src, hdr, err := r.FormFile("FILENAME") if err != nil { @@ -137,3 +139,51 @@ func webUploadHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport io.Copy(dst, src) webUploadResponse(w, "tmp://"+strings.TrimPrefix(dstfile, conf.TempDir)) } + +func webUploadCleanerRun(dir *os.File, conf *rhimport.Config) { + t := time.Tick(5 * time.Second) + for now := range t { + var err error + if _, err = dir.Seek(0, 0); err != nil { + rhl.Printf("webUploadCleaner: reading directory contents failed: %s", err) + return + } + + var entries []os.FileInfo + if entries, err = dir.Readdir(0); err != nil { + rhl.Printf("webUploadCleaner: reading directory contents failed: %s", err) + return + } + + 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 + } + } +} + +func webUploadCleaner(conf *rhimport.Config) { + for { + dir, err := os.Open(conf.TempDir) + if err != nil { + rhl.Printf("webUploadCleaner: %s", err) + continue + } + if i, err := dir.Stat(); err != nil { + rhl.Printf("webUploadCleaner: %s", err) + dir.Close() + continue + } else { + if !i.IsDir() { + rhl.Printf("webUploadCleaner: %s is not a directory", conf.TempDir) + dir.Close() + continue + } + } + webUploadCleanerRun(dir, conf) + rhdl.Printf("webUploadCleanerRun: returned - restring in 5 sec...") + dir.Close() + + time.Sleep(5 * time.Second) + } +} -- cgit v0.10.2