From 534a84e51ab8a0388bab1dac440fb973ff4cd0c0 Mon Sep 17 00:00:00 2001 From: hladu357 Date: Thu, 13 Jun 2024 14:25:35 +0200 Subject: [PATCH] admin panel added --- go.mod | 5 ++ go.sum | 2 + html/admin.html | 49 ++++++++++++++ html/user.html | 45 +++++++++++++ upload.go | 166 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 267 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 html/admin.html create mode 100644 html/user.html create mode 100644 upload.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4eff742 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module upload + +go 1.22.4 + +require github.com/google/uuid v1.6.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7790d7c --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/html/admin.html b/html/admin.html new file mode 100644 index 0000000..ebf2b90 --- /dev/null +++ b/html/admin.html @@ -0,0 +1,49 @@ + + + + + + Admin Panel + + +

Admin Panel

+
+ + +

+ + +

+ + +
+ + + + + diff --git a/html/user.html b/html/user.html new file mode 100644 index 0000000..929ea2b --- /dev/null +++ b/html/user.html @@ -0,0 +1,45 @@ + + + + + + Chunked File Upload + + + + + + + + \ No newline at end of file diff --git a/upload.go b/upload.go new file mode 100644 index 0000000..6f5f30b --- /dev/null +++ b/upload.go @@ -0,0 +1,166 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" + "os" + "strconv" + "sync" + "time" + + "github.com/google/uuid" +) + +type Admin struct { + name string +} + +type UploadState int + +const ( + inactive UploadState = iota + active + completed +) + +type UploadContext struct { + maxSize int64 + created time.Time + state UploadState + finalName string +} + +type UploadDatabase struct { + data map[uuid.UUID]UploadContext + mtx sync.Mutex +} + +var db UploadDatabase + +func (d *UploadDatabase) commit(size int64, finalName string, lifefile time.Duration) uuid.UUID { + id := uuid.New() + d.mtx.Lock() + defer d.mtx.Unlock() + + _, found := d.data[id] + + if found { + return uuid.Max + } + + d.data[id] = UploadContext{ + created: time.Now(), + finalName: finalName, + maxSize: size, + state: inactive, + } + + return id +} + +func (d *UploadDatabase) activate(id uuid.UUID) *UploadContext { + d.mtx.Lock() + defer d.mtx.Unlock() + + // i, found := d.data[id] + + return nil +} + +func (d *UploadDatabase) deactivate(id uuid.UUID) { + +} + +func initialize() { + db.data = make(map[uuid.UUID]UploadContext) +} + +func adminHandler(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "./html/admin.html") +} + +func generateHandler(w http.ResponseWriter, r *http.Request) { + var maxSizeBytes int64 + var linkLifetime int64 + var id uuid.UUID + var uploadLink string + + if r.Method != "POST" { + w.WriteHeader(http.StatusMethodNotAllowed) + } + + var linkSettings struct { + Lifetime string `json:"lifetime"` + MaxSize string `json:"maxSize"` + SizeUnit string `json:"sizeUnit"` + } + err := json.NewDecoder(r.Body).Decode(&linkSettings) + if err != nil { + http.Error(w, fmt.Sprintf("Invalid input: %v", err), http.StatusBadRequest) + return + } + + linkLifetime, err = strconv.ParseInt(linkSettings.Lifetime, 10, 64) + if err != nil { + http.Error(w, fmt.Sprintf("Invalid input: %v", err), http.StatusBadRequest) + return + } + + maxSizeBytes, err = strconv.ParseInt(linkSettings.MaxSize, 10, 64) + if err != nil { + http.Error(w, fmt.Sprintf("Invalid input: %v", err), http.StatusBadRequest) + return + } + + if linkSettings.SizeUnit == "GB" { + maxSizeBytes *= 1024 * 1024 * 1024 + } else if linkSettings.SizeUnit == "MB" { + maxSizeBytes *= 1024 * 1024 + } else { + http.Error(w, fmt.Sprintf("Invalid input: %v", err), http.StatusBadRequest) + return + } + + id = db.commit(int64(maxSizeBytes), "untitled"+time.Now().String(), time.Duration(linkLifetime)) + if id == uuid.Max { + http.Error(w, "Commit failed", http.StatusBadRequest) + return + } + uploadLink = fmt.Sprintf("https://localhost:8080/upload?key=%s", id) + + response := map[string]string{"uploadLink": uploadLink} + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(response); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + +func uploadHandler(w http.ResponseWriter, r *http.Request) { + +} + +func fileHandler(w http.ResponseWriter, r *http.Request) { + +} + +func main() { + initialize() + + os.MkdirAll("uploads", os.ModePerm) + + http.HandleFunc("/admin", adminHandler) + http.HandleFunc("/generate", generateHandler) + http.HandleFunc("/upload", uploadHandler) + http.HandleFunc("/file", fileHandler) + + // http.Handle("/", http.FileServer(http.Dir("."))) + + // Start the server + fmt.Println("Starting server on :8080") + err := http.ListenAndServe(":8080", nil) + if err != nil { + fmt.Printf("Error starting server: %v", err) + } +}