forked from Blaster4385/file-share
refactor: switched to echo
This commit is contained in:
parent
f649ea8172
commit
4016aa5800
3 changed files with 82 additions and 60 deletions
|
@ -3,7 +3,19 @@ module fileshare
|
|||
go 1.22.2
|
||||
|
||||
require (
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
|
||||
github.com/gorilla/mux v1.8.1 // indirect
|
||||
github.com/labstack/echo/v4 v4.12.0 // indirect
|
||||
github.com/labstack/gommon v0.4.2 // indirect
|
||||
github.com/lib/pq v1.10.9 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/rs/cors v1.10.1 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||
golang.org/x/crypto v0.22.0 // indirect
|
||||
golang.org/x/net v0.24.0 // indirect
|
||||
golang.org/x/sys v0.19.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
golang.org/x/time v0.5.0 // indirect
|
||||
)
|
||||
|
|
|
@ -1,6 +1,33 @@
|
|||
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
|
||||
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
|
||||
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
|
||||
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
|
||||
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
|
||||
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
|
||||
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
|
||||
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
|
||||
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
|
||||
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||
|
|
103
server/main.go
103
server/main.go
|
@ -7,16 +7,15 @@ import (
|
|||
"crypto/rand"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/rs/cors"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -27,6 +26,15 @@ const (
|
|||
|
||||
var db *sql.DB
|
||||
|
||||
func registerHandlers(e *echo.Echo) {
|
||||
e.Use(middleware.Logger())
|
||||
e.Use(middleware.Recover())
|
||||
e.Use(middleware.CORS())
|
||||
e.POST("/upload", handleUpload)
|
||||
e.GET("/download/:id", handleDownload)
|
||||
e.GET("/get/:id", handleGetFileInfo)
|
||||
}
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
db, err = initDB()
|
||||
|
@ -35,18 +43,9 @@ func main() {
|
|||
}
|
||||
defer db.Close()
|
||||
|
||||
router := mux.NewRouter()
|
||||
router.HandleFunc("/upload", handleUpload).Methods("POST")
|
||||
router.HandleFunc("/download/{id}", handleDownload).Methods("GET")
|
||||
router.HandleFunc("/get/{id}", handleGetFileInfo).Methods("GET")
|
||||
|
||||
handler := cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedMethods: []string{"GET", "POST"},
|
||||
AllowedHeaders: []string{"*"},
|
||||
}).Handler(router)
|
||||
|
||||
http.ListenAndServe(":8080", handler)
|
||||
e := echo.New()
|
||||
registerHandlers(e)
|
||||
e.Logger.Fatal(e.Start(":8080"))
|
||||
}
|
||||
|
||||
func initDB() (*sql.DB, error) {
|
||||
|
@ -76,22 +75,20 @@ func createFilesTable(ctx context.Context, db *sql.DB) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||
func handleUpload(c echo.Context) error {
|
||||
r := c.Request()
|
||||
if err := r.ParseMultipartForm(maxUploadSize); err != nil {
|
||||
handleError(w, fmt.Errorf("error parsing multipart form: %v", err), http.StatusBadRequest)
|
||||
return
|
||||
return handleError(c, fmt.Errorf("error parsing multipart form: %v", err), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
key, err := generateRandomKey()
|
||||
if err != nil {
|
||||
handleError(w, fmt.Errorf("error generating encryption key: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
return handleError(c, fmt.Errorf("error generating encryption key: %v", err), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
file, handler, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
handleError(w, fmt.Errorf("error getting form file: %v", err), http.StatusBadRequest)
|
||||
return
|
||||
return handleError(c, fmt.Errorf("error getting form file: %v", err), http.StatusBadRequest)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
|
@ -99,13 +96,11 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
encryptedData, err := encryptFile(file, key)
|
||||
if err != nil {
|
||||
handleError(w, fmt.Errorf("error encrypting file: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
return handleError(c, fmt.Errorf("error encrypting file: %v", err), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if err := storeFileInDB(r.Context(), id, handler.Filename, encryptedData); err != nil {
|
||||
handleError(w, fmt.Errorf("error storing file in database: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
return handleError(c, fmt.Errorf("error storing file in database: %v", err), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
encodedKey := hex.EncodeToString(key)
|
||||
|
@ -120,58 +115,50 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
|
|||
Key: encodedKey,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
handleError(w, fmt.Errorf("error encoding response: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
return c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func handleDownload(w http.ResponseWriter, r *http.Request) {
|
||||
id := mux.Vars(r)["id"]
|
||||
keyHex := r.URL.Query().Get("key")
|
||||
func handleDownload(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
keyHex := c.QueryParam("key")
|
||||
|
||||
key, err := hex.DecodeString(keyHex)
|
||||
if err != nil {
|
||||
handleError(w, fmt.Errorf("invalid key: %v", err), http.StatusBadRequest)
|
||||
return
|
||||
return handleError(c, fmt.Errorf("invalid key: %v", err), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
fileName, encryptedData, err := getFileFromDB(r.Context(), id)
|
||||
fileName, encryptedData, err := getFileFromDB(c.Request().Context(), id)
|
||||
if err != nil {
|
||||
handleError(w, fmt.Errorf("error getting file from database: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
return handleError(c, fmt.Errorf("error getting file from database: %v", err), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
|
||||
c.Response().Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
|
||||
|
||||
err = decryptAndStreamFile(w, encryptedData, key)
|
||||
err = decryptAndStreamFile(c.Response(), encryptedData, key)
|
||||
if err != nil {
|
||||
handleError(w, fmt.Errorf("error decrypting and streaming file: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
return handleError(c, fmt.Errorf("error decrypting and streaming file: %v", err), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleGetFileInfo(w http.ResponseWriter, r *http.Request) {
|
||||
id := mux.Vars(r)["id"]
|
||||
keyHex := r.URL.Query().Get("key")
|
||||
func handleGetFileInfo(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
keyHex := c.QueryParam("key")
|
||||
|
||||
key, err := hex.DecodeString(keyHex)
|
||||
if err != nil {
|
||||
handleError(w, fmt.Errorf("invalid key: %v", err), http.StatusBadRequest)
|
||||
return
|
||||
return handleError(c, fmt.Errorf("invalid key: %v", err), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
fileName, encryptedData, err := getFileFromDB(r.Context(), id)
|
||||
fileName, encryptedData, err := getFileFromDB(c.Request().Context(), id)
|
||||
if err != nil {
|
||||
handleError(w, fmt.Errorf("error getting file from database: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
return handleError(c, fmt.Errorf("error getting file from database: %v", err), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
plaintext, err := decryptFile(encryptedData, key)
|
||||
if err != nil {
|
||||
handleError(w, fmt.Errorf("error decrypting file: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
return handleError(c, fmt.Errorf("error decrypting file: %v", err), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
fileSizeBytes := len(plaintext)
|
||||
|
@ -190,11 +177,7 @@ func handleGetFileInfo(w http.ResponseWriter, r *http.Request) {
|
|||
FileSize: fileSize,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(fileInfo); err != nil {
|
||||
handleError(w, fmt.Errorf("error encoding response: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
return c.JSON(http.StatusOK, fileInfo)
|
||||
}
|
||||
|
||||
func storeFileInDB(ctx context.Context, id, fileName string, encryptedData []byte) error {
|
||||
|
@ -220,8 +203,8 @@ func getFileFromDB(ctx context.Context, id string) (fileName string, encryptedDa
|
|||
return fileName, encryptedData, err
|
||||
}
|
||||
|
||||
func handleError(w http.ResponseWriter, err error, code int) {
|
||||
http.Error(w, err.Error(), code)
|
||||
func handleError(c echo.Context, err error, code int) error {
|
||||
return c.JSON(code, map[string]string{"error": err.Error()})
|
||||
}
|
||||
|
||||
func encryptFile(in io.Reader, key []byte) ([]byte, error) {
|
||||
|
|
Loading…
Reference in a new issue