summaryrefslogtreecommitdiff
path: root/src/rhimportd/uploadWeb.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/rhimportd/uploadWeb.go')
-rw-r--r--src/rhimportd/uploadWeb.go135
1 files changed, 18 insertions, 117 deletions
diff --git a/src/rhimportd/uploadWeb.go b/src/rhimportd/uploadWeb.go
index 63d04f4..412df12 100644
--- a/src/rhimportd/uploadWeb.go
+++ b/src/rhimportd/uploadWeb.go
@@ -26,40 +26,35 @@ package main
import (
"bytes"
- "code.helsinki.at/rhrd-go/rddb"
- "code.helsinki.at/rhrd-go/rhimport"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
- "os"
- "path"
- "path/filepath"
- "strings"
- "time"
+
+ "code.helsinki.at/rhrd-go/rddb"
+ "code.helsinki.at/rhrd-go/rhimport"
)
type webUploadResponseData struct {
ResponseCode int `json:"RESPONSE_CODE"`
ErrorString string `json:"ERROR_STRING"`
- SourceFile string `json:"SOURCE_FILE"`
}
func webUploadErrorResponse(w http.ResponseWriter, code int, errStr string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
encoder := json.NewEncoder(w)
- respdata := webUploadResponseData{code, errStr, ""}
+ respdata := webUploadResponseData{code, errStr}
encoder.Encode(respdata)
}
-func webUploadResponse(w http.ResponseWriter, file string) {
+func webUploadSuccessResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
encoder := json.NewEncoder(w)
- respdata := webUploadResponseData{http.StatusOK, "success", file}
+ respdata := webUploadResponseData{http.StatusOK, "success"}
encoder.Encode(respdata)
}
@@ -67,7 +62,7 @@ const (
webUploadMaxRequestSize = (2 << 30) - 1 // 2GB, (2 << 30) overflows int on 32-bit systems therefore we use 2GB - 1 Byte
)
-func webUploadParseForm(w http.ResponseWriter, r *http.Request) (username, password, srcfile string, src *multipart.Part, ok bool) {
+func webUploadParseForm(w http.ResponseWriter, r *http.Request) (username, sessionid, srcfile string, src *multipart.Part, ok bool) {
mpr, err := r.MultipartReader()
if err != nil {
rhl.Printf("WebUploadHandler: error while parsing multipart-form: %v", err)
@@ -86,8 +81,8 @@ func webUploadParseForm(w http.ResponseWriter, r *http.Request) (username, passw
switch p.FormName() {
case "LOGIN_NAME":
dstfield = &username
- case "PASSWORD":
- dstfield = &password
+ case "SESSION_ID":
+ dstfield = &sessionid
case "FILENAME":
srcfile = p.FileName()
src = p
@@ -130,7 +125,7 @@ func webUploadHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport
}
r.Body = http.MaxBytesReader(w, r.Body, webUploadMaxRequestSize)
- username, password, srcfile, src, ok := webUploadParseForm(w, r)
+ username, sessionid, srcfile, src, ok := webUploadParseForm(w, r)
if !ok {
return
}
@@ -138,111 +133,17 @@ func webUploadHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport
webUploadErrorResponse(w, http.StatusBadRequest, "missing field LOGIN_NAME")
return
}
- if password == "" {
- webUploadErrorResponse(w, http.StatusBadRequest, "missing field PASSWORD")
- 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
- }
-
- rhdl.Printf("WebUploadHandler: got request from user '%s', filename='%s'", username, srcfile)
-
- dstpath, err := ioutil.TempDir(conf.TempDir, "webupload-"+username+"-")
- if err != nil {
- rhl.Printf("WebUploadHandler: error creating temporary directory: %v", err)
- webUploadErrorResponse(w, http.StatusInternalServerError, err.Error())
- return
- }
-
- dstfile := filepath.Join(dstpath, path.Clean("/"+srcfile))
- dst, err := os.OpenFile(dstfile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
- if err != nil {
- rhl.Printf("WebUploadHandler: Unable to create file '%s': %v", dstfile, err)
- webUploadErrorResponse(w, http.StatusInternalServerError, err.Error())
- return
- }
- defer dst.Close()
-
- 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())
+ if sessionid == "" {
+ webUploadErrorResponse(w, http.StatusBadRequest, "missing field SESSION_ID")
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, maxAge time.Duration) {
- t := time.NewTicker(1 * time.Minute)
- defer t.Stop()
- for now := range t.C {
- var err error
- if _, err = dir.Seek(0, 0); err != nil {
- rhl.Printf("webUploadCleaner: reading directory contents failed: %s", err)
- return
- }
+ // TODO: get session->attachmentChan from store -> 401 if not found
+ // TODO: take session -> 403 if already taken
- var entries []os.FileInfo
- if entries, err = dir.Readdir(0); err != nil {
- rhl.Printf("webUploadCleaner: reading directory contents failed: %s", err)
- return
- }
+ // TODO: fetch file (src) in chunks and send it to attachmentChan && check for canceled
+ rhl.Printf("WebUploadHandler: fetching file from '%s' (%v)", srcfile, src)
+ io.Copy(ioutil.Discard, src)
- for _, entry := range entries {
- 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, maxAge time.Duration) {
- first := true
- for {
- if !first {
- time.Sleep(5 * time.Second)
- }
- first = false
-
- 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
- }
- }
- rhdl.Printf("webUploadCleaner: running with max age: %v", maxAge)
- webUploadCleanerRun(dir, conf, maxAge)
- rhdl.Printf("webUploadCleanerRun: returned - restarting in 5 sec...")
- dir.Close()
- }
+ webUploadSuccessResponse(w)
}