From 053fa37ae1cd98f765784161f0c59928d956bf3b Mon Sep 17 00:00:00 2001 From: Kamil Marut Date: Mon, 11 Sep 2023 21:34:49 +0800 Subject: [PATCH] feat(server): Improve form handlers --- cmd/cmd.go | 4 ++-- cmd/runserver.go | 6 ------ server/handlers.go | 31 ++++++++++++++++++++----------- server/server.go | 8 +------- server/templates/index.html | 9 ++++++--- storage/local.go | 35 ++--------------------------------- storage/s3.go | 22 ++++------------------ storage/storage.go | 3 --- 8 files changed, 35 insertions(+), 83 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 5696c01..5799b2b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -22,7 +22,7 @@ func GetStorage(cCtx *cli.Context) (chosenStorage storage.Storage, err error) { return nil, errors.New("no upload directory specified") } - chosenStorage, err = storage.NewLocalStorage(udir, cCtx.Duration("retention-time")) + chosenStorage, err = storage.NewLocalStorage(udir) case "s3": bucket := cCtx.String("s3-bucket") region := cCtx.String("s3-region") @@ -31,7 +31,7 @@ func GetStorage(cCtx *cli.Context) (chosenStorage storage.Storage, err error) { sessionToken := cCtx.String("aws-session-token") endpointUrl := cCtx.String("aws-endpoint-url") - chosenStorage, err = storage.NewS3Storage(accessKey, secretKey, sessionToken, endpointUrl, region, bucket, cCtx.Duration("retention-time")) + chosenStorage, err = storage.NewS3Storage(accessKey, secretKey, sessionToken, endpointUrl, region, bucket) default: return nil, errors.New("wrong storage provider") } diff --git a/cmd/runserver.go b/cmd/runserver.go index b50eb72..ce8a950 100644 --- a/cmd/runserver.go +++ b/cmd/runserver.go @@ -2,7 +2,6 @@ package cmd import ( "log" - "time" "github.com/exler/fileigloo/logger" "github.com/exler/fileigloo/server" @@ -29,11 +28,6 @@ var serverCmd = &cli.Command{ Value: 20, EnvVars: []string{"RATE_LIMIT"}, }, - &cli.DurationFlag{ - Name: "retention-time", - Value: 24 * time.Hour, - EnvVars: []string{"RETENTION_TIME"}, - }, &cli.StringFlag{ Name: "storage", Value: "local", diff --git a/server/handlers.go b/server/handlers.go index 54010f1..f1e2da6 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -22,12 +22,26 @@ func generateFileId() string { } func (s *Server) indexHandler(w http.ResponseWriter, r *http.Request) { - renderTemplate(w, "index", map[string]interface{}{ - "retentionTime": s.storage.RetentionTime().String(), - }) + renderTemplate(w, "index", map[string]interface{}{}) +} + +func (s *Server) formHandler(w http.ResponseWriter, r *http.Request) { + if formDefender := r.FormValue("defender"); formDefender != "" { + http.Error(w, http.StatusText(http.StatusTeapot), http.StatusTeapot) + return + } + + if formType := r.FormValue("form-type"); formType == "file-upload" { + s.fileUploadHandler(w, r) + } else if formType == "pastebin" { + s.pastebinHandler(w, r) + } else { + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + return + } } -func (s *Server) uploadHandler(w http.ResponseWriter, r *http.Request) { +func (s *Server) fileUploadHandler(w http.ResponseWriter, r *http.Request) { if !ValidateContentType(r.Header) { http.Error(w, "Request Content-Type must be 'multipart/form-data'", http.StatusBadRequest) return @@ -80,8 +94,7 @@ func (s *Server) uploadHandler(w http.ResponseWriter, r *http.Request) { s.logger.Info(fmt.Sprintf("New file uploaded [url=%s]", fileUrl)) renderTemplate(w, "index", map[string]interface{}{ - "fileUrl": fileUrl, - "retentionTime": s.storage.RetentionTime().String(), + "fileUrl": fileUrl, }) } @@ -90,9 +103,6 @@ func (s *Server) pastebinHandler(w http.ResponseWriter, r *http.Request) { if pasteContent = r.FormValue("paste"); pasteContent == "" { http.Error(w, "Paste is empty", http.StatusBadRequest) return - } else if formDefender := r.FormValue("defender"); formDefender != "" { - http.Error(w, http.StatusText(http.StatusTeapot), http.StatusTeapot) - return } buf := []byte(pasteContent) @@ -131,8 +141,7 @@ func (s *Server) pastebinHandler(w http.ResponseWriter, r *http.Request) { s.logger.Info(fmt.Sprintf("New file uploaded [url=%s]", fileUrl)) renderTemplate(w, "index", map[string]interface{}{ - "fileUrl": fileUrl, - "retentionTime": s.storage.RetentionTime().String(), + "fileUrl": fileUrl, }) } diff --git a/server/server.go b/server/server.go index b2fcfe6..36b8a59 100644 --- a/server/server.go +++ b/server/server.go @@ -79,8 +79,7 @@ func (s *Server) Run() { s.router.Handle("/static/*", fs) s.router.Get("/", s.indexHandler) - s.router.Post("/upload", s.uploadHandler) - s.router.Post("/pastebin", s.pastebinHandler) + s.router.Post("/", s.formHandler) s.router.Get("/{view:(?:view)}/{fileId}", s.downloadHandler) s.router.Get("/{fileId}", s.downloadHandler) @@ -93,11 +92,6 @@ func (s *Server) Run() { } s.logger.Debug(fmt.Sprintf("Server started [storage=%s]", s.storage.Type())) - err := s.storage.Purge() - if err != nil { - s.logger.Error(err) - } - go func() { err := srv.ListenAndServe() if err != nil { diff --git a/server/templates/index.html b/server/templates/index.html index e42684d..dd3775a 100644 --- a/server/templates/index.html +++ b/server/templates/index.html @@ -112,7 +112,7 @@ {{ end }} -
+
File Upload @@ -122,6 +122,8 @@
+ +
@@ -129,19 +131,20 @@
-
+
Pastebin +
diff --git a/storage/local.go b/storage/local.go index 72453a2..b94b4cc 100644 --- a/storage/local.go +++ b/storage/local.go @@ -8,24 +8,20 @@ import ( "io" "os" "path/filepath" - "time" ) type LocalStorage struct { Storage basedir string - - retentionTime time.Duration } -func NewLocalStorage(basedir string, retentionTime time.Duration) (*LocalStorage, error) { +func NewLocalStorage(basedir string) (*LocalStorage, error) { if basedir[len(basedir)-1:] != "/" { basedir += "/" } storage := &LocalStorage{ - basedir: basedir, - retentionTime: retentionTime, + basedir: basedir, } return storage, nil @@ -35,10 +31,6 @@ func (s *LocalStorage) Type() string { return "local" } -func (s *LocalStorage) RetentionTime() time.Duration { - return s.retentionTime -} - func (s *LocalStorage) List(ctx context.Context) (filenames []string, metadata []Metadata, err error) { err = filepath.WalkDir(s.basedir, func(path string, d os.DirEntry, err error) error { if err != nil { @@ -147,29 +139,6 @@ func (s *LocalStorage) Delete(ctx context.Context, filename string) error { return nil } -func (s *LocalStorage) Purge() error { - err := filepath.Walk(s.basedir, func(path string, info os.FileInfo, err error) error { - if err != nil && s.FileNotExists(err) { - return nil - } else if err != nil { - return err - } - - if info.IsDir() { - return nil - } - - if info.ModTime().Before(time.Now().Add(-s.retentionTime)) { - err = os.Remove(path) - return err - } - - return nil - }) - - return err -} - func (s *LocalStorage) FileNotExists(err error) bool { if err == nil { return false diff --git a/storage/s3.go b/storage/s3.go index 1c0edba..683f182 100644 --- a/storage/s3.go +++ b/storage/s3.go @@ -4,7 +4,6 @@ import ( "context" "io" "strconv" - "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" @@ -19,8 +18,6 @@ type S3Storage struct { s3 *s3.S3 session *session.Session bucket string - - retentionTime time.Duration } func newAWSSession(accessKey, secretKey, sessionToken, endpointUrl, region string) *session.Session { @@ -35,18 +32,13 @@ func (s *S3Storage) Type() string { return "s3" } -func (s *S3Storage) RetentionTime() time.Duration { - return s.retentionTime -} - -func NewS3Storage(accessKey, secretKey, sessionToken, endpointUrl, region, bucket string, retentionTime time.Duration) (*S3Storage, error) { +func NewS3Storage(accessKey, secretKey, sessionToken, endpointUrl, region, bucket string) (*S3Storage, error) { session := newAWSSession(accessKey, secretKey, sessionToken, endpointUrl, region) return &S3Storage{ - s3: s3.New(session), - session: session, - bucket: bucket, - retentionTime: retentionTime, + s3: s3.New(session), + session: session, + bucket: bucket, }, nil } @@ -122,7 +114,6 @@ func (s *S3Storage) Put(ctx context.Context, filename string, reader io.Reader, Key: aws.String(filename), Body: reader, Metadata: mMap, - Expires: aws.Time(time.Now().Add(s.retentionTime)), }) return err @@ -138,11 +129,6 @@ func (s *S3Storage) Delete(ctx context.Context, filename string) error { return err } -// Noop: S3 has expiration set on file creation -func (s *S3Storage) Purge() error { - return nil -} - func (s *S3Storage) FileNotExists(err error) bool { if err == nil { return false diff --git a/storage/storage.go b/storage/storage.go index c8796e0..53b5ab8 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -3,7 +3,6 @@ package storage import ( "context" "io" - "time" ) type Metadata struct { @@ -39,8 +38,6 @@ type Storage interface { GetOnlyMetadata(ctx context.Context, filename string) (metadata Metadata, err error) Put(ctx context.Context, filename string, reader io.Reader, metadata Metadata) error Delete(ctx context.Context, filename string) error - Purge() error FileNotExists(err error) bool Type() string - RetentionTime() time.Duration }