file struct separated

This commit is contained in:
hladu357 2024-06-14 10:45:28 +02:00
parent 2d159874f1
commit f049e36b1c
1 changed files with 70 additions and 24 deletions

View File

@ -3,8 +3,10 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strconv" "strconv"
"sync" "sync"
"time" "time"
@ -22,23 +24,29 @@ const (
inactive UploadState = iota inactive UploadState = iota
active active
completed completed
full
) )
type UploadContext struct { type UploadFile struct {
maxSize int64
created time.Time created time.Time
state UploadState state UploadState
finalName string finalName string
} }
type UploadContext struct {
maxSize int64
created time.Time
lifetime time.Duration
}
type UploadDatabase struct { type UploadDatabase struct {
data map[uuid.UUID]UploadContext data map[uuid.UUID]*UploadContext
mtx sync.Mutex mtx sync.Mutex
} }
var db UploadDatabase var db UploadDatabase
func (d *UploadDatabase) commit(size int64, finalName string, lifefile time.Duration) uuid.UUID { func (d *UploadDatabase) commit(size int64, lifefile time.Duration) uuid.UUID {
id := uuid.New() id := uuid.New()
d.mtx.Lock() d.mtx.Lock()
defer d.mtx.Unlock() defer d.mtx.Unlock()
@ -49,12 +57,12 @@ func (d *UploadDatabase) commit(size int64, finalName string, lifefile time.Dura
return uuid.Max return uuid.Max
} }
d.data[id] = UploadContext{ ctx := new(UploadContext)
created: time.Now(), d.data[id] = ctx
finalName: finalName,
maxSize: size, ctx.created = time.Now()
state: inactive, ctx.lifetime = lifefile
} ctx.maxSize = size
return id return id
} }
@ -66,18 +74,6 @@ func (d *UploadDatabase) exists(id uuid.UUID) bool {
return found return found
} }
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 (d *UploadDatabase) getSize(id uuid.UUID) int64 { func (d *UploadDatabase) getSize(id uuid.UUID) int64 {
d.mtx.Lock() d.mtx.Lock()
defer d.mtx.Unlock() defer d.mtx.Unlock()
@ -85,7 +81,7 @@ func (d *UploadDatabase) getSize(id uuid.UUID) int64 {
} }
func initialize() { func initialize() {
db.data = make(map[uuid.UUID]UploadContext) db.data = make(map[uuid.UUID]*UploadContext)
} }
func adminHandler(w http.ResponseWriter, r *http.Request) { func adminHandler(w http.ResponseWriter, r *http.Request) {
@ -100,6 +96,7 @@ func generateHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" { if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
return
} }
var linkSettings struct { var linkSettings struct {
@ -134,7 +131,7 @@ func generateHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
id = db.commit(int64(maxSizeBytes), "untitled"+time.Now().String(), time.Duration(linkLifetime)) id = db.commit(maxSizeBytes, time.Duration(linkLifetime))
if id == uuid.Max { if id == uuid.Max {
http.Error(w, "Commit failed", http.StatusBadRequest) http.Error(w, "Commit failed", http.StatusBadRequest)
return return
@ -152,6 +149,7 @@ func generateHandler(w http.ResponseWriter, r *http.Request) {
func uploadHandler(w http.ResponseWriter, r *http.Request) { func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" { if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
return
} }
id, err := uuid.Parse(r.URL.Query().Get("key")) id, err := uuid.Parse(r.URL.Query().Get("key"))
@ -172,7 +170,55 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
} }
func fileHandler(w http.ResponseWriter, r *http.Request) { func fileHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
id, err := uuid.Parse(r.URL.Query().Get("key"))
if err != nil || !db.exists(id) {
w.WriteHeader(http.StatusBadRequest)
return
}
// ctx := db.activate(id)
// defer db.deactivate(id)
// if ctx == nil {
// w.WriteHeader(http.StatusBadRequest)
// return
// }
// parse form
err = r.ParseMultipartForm(10 << 20) // 10MB memory cache
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Retrieve the file from form
file, handler, err := r.FormFile("file")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
defer file.Close()
// Create a new file in the uploads directory
dst, err := os.Create(filepath.Join("uploads", handler.Filename))
if err != nil {
fmt.Fprintf(w, "Error creating file: %v", err)
return
}
defer dst.Close()
// Copy the uploaded file to the new file
_, err = io.Copy(dst, file)
if err != nil {
fmt.Fprintf(w, "Error saving file: %v", err)
return
}
w.WriteHeader(http.StatusAccepted)
} }
func main() { func main() {