refactor: further curl refactoring

This commit is contained in:
jabuxas 2024-10-15 14:38:01 -03:00
parent 0d6c6d02b1
commit 6ef22dd93f
2 changed files with 22 additions and 36 deletions

View File

@ -7,7 +7,6 @@ import (
"html/template" "html/template"
"io" "io"
"log/slog" "log/slog"
"mime/multipart"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -148,6 +147,11 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
} }
func (app *Application) parserHandler(w http.ResponseWriter, r *http.Request) { func (app *Application) parserHandler(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(app.filesDir); err != nil {
if err := os.Mkdir(app.filesDir, 0750); err != nil {
http.Error(w, "Error creating storage directory", http.StatusInternalServerError)
}
}
if contentType := r.Header.Get("Content-Type"); contentType == "application/x-www-form-urlencoded" { if contentType := r.Header.Get("Content-Type"); contentType == "application/x-www-form-urlencoded" {
app.formHandler(w, r) app.formHandler(w, r)
} else if strings.Split(contentType, ";")[0] == "multipart/form-data" { } else if strings.Split(contentType, ";")[0] == "multipart/form-data" {
@ -158,15 +162,9 @@ func (app *Application) parserHandler(w http.ResponseWriter, r *http.Request) {
} }
func (app *Application) formHandler(w http.ResponseWriter, r *http.Request) { func (app *Application) formHandler(w http.ResponseWriter, r *http.Request) {
file, handler, err := r.FormFile("content") // content := r.FormValue("content")
if err != nil { //
http.Error(w, "Error retrieving the file", http.StatusBadRequest) // app.saveFile(w, r, file, ".txt", "content")
slog.Warn(error.Error(err))
return
}
defer file.Close()
app.saveFile(w, r, file, handler, "content")
} }
func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) { func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) {
@ -188,35 +186,17 @@ func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) {
} }
defer file.Close() defer file.Close()
app.saveFile(w, r, file, handler, "file") url := app.publicURL(file, filepath.Ext(handler.Filename))
}
func (app *Application) saveFile(
w http.ResponseWriter,
r *http.Request,
file multipart.File,
handler *multipart.FileHeader,
form string,
) {
if _, err := os.Stat(app.filesDir); err != nil {
if err := os.Mkdir(app.filesDir, 0750); err != nil {
http.Error(w, "Error creating storage directory", http.StatusInternalServerError)
}
}
filename, _ := HashFile(file, handler)
filepath := filepath.Join(app.filesDir, filename)
// reopen the file for copying, as the hash process consumed the file reader // reopen the file for copying, as the hash process consumed the file reader
file, _, err := r.FormFile(form) file, _, err = r.FormFile("file")
if err != nil { if err != nil {
http.Error(w, "Error retrieving the file", http.StatusBadRequest) http.Error(w, "Error retrieving the file", http.StatusBadRequest)
return return
} }
defer file.Close() defer file.Close()
dst, err := os.Create(filepath) dst, err := os.Create(app.lastUploadedFile)
if err != nil { if err != nil {
http.Error(w, "Error creating file\n", http.StatusInternalServerError) http.Error(w, "Error creating file\n", http.StatusInternalServerError)
} }
@ -226,9 +206,17 @@ func (app *Application) saveFile(
http.Error(w, "Error copying the file", http.StatusInternalServerError) http.Error(w, "Error copying the file", http.StatusInternalServerError)
} }
fmt.Fprintf(w, "%s", url)
}
func (app *Application) publicURL(file io.Reader, extension string) string {
filename, _ := HashFile(file, extension)
filepath := filepath.Join(app.filesDir, filename)
app.lastUploadedFile = filepath app.lastUploadedFile = filepath
fmt.Fprintf(w, "http://%s/%s\n", app.url, filename) return fmt.Sprintf("http://%s/%s\n", app.url, filename)
} }
func (app *Application) basicAuth(next http.HandlerFunc) http.HandlerFunc { func (app *Application) basicAuth(next http.HandlerFunc) http.HandlerFunc {

View File

@ -5,9 +5,7 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io" "io"
"mime/multipart"
"net/http" "net/http"
"path/filepath"
) )
func CheckAuth(r *http.Request, key string) bool { func CheckAuth(r *http.Request, key string) bool {
@ -25,7 +23,7 @@ func FormatFileSize(size int64) string {
return fmt.Sprintf("%.2f GB", float64(size)/(1024*1024*1024)) return fmt.Sprintf("%.2f GB", float64(size)/(1024*1024*1024))
} }
func HashFile(file multipart.File, handler *multipart.FileHeader) (string, error) { func HashFile(file io.Reader, extension string) (string, error) {
hasher := md5.New() hasher := md5.New()
if _, err := io.Copy(hasher, file); err != nil { if _, err := io.Copy(hasher, file); err != nil {
return "", err return "", err
@ -33,7 +31,7 @@ func HashFile(file multipart.File, handler *multipart.FileHeader) (string, error
sha1Hash := hex.EncodeToString(hasher.Sum(nil))[:8] sha1Hash := hex.EncodeToString(hasher.Sum(nil))[:8]
filename := fmt.Sprintf("%s%s", sha1Hash, filepath.Ext(handler.Filename)) filename := fmt.Sprintf("%s%s", sha1Hash, extension)
return filename, nil return filename, nil
} }