@@ -3,7 +3,7 @@ package handlers
33
44import (
55 "fmt"
6- "io/ioutil "
6+ "io"
77 "log"
88 "net/http"
99 "os"
@@ -22,10 +22,19 @@ import (
2222// It accepts form data for type (text/file), optional password, one-time use, and expiration.
2323func CreateSend (cfg config.Config , db * gorm.DB ) gin.HandlerFunc {
2424 return func (c * gin.Context ) {
25- stype := c .PostForm ("type" )
26- pw := c .PostForm ("password" )
27- ot := c .PostForm ("onetime" )
28- exp := c .PostForm ("expires" )
25+ // Explicitly parse the multipart form before accessing form values.
26+ // This is crucial for handling mixed file/text forms reliably in Gin.
27+ if err := c .Request .ParseMultipartForm (cfg .MaxFileSize + 1024 * 1024 ); err != nil { // Add buffer to max size
28+ log .Println ("Error parsing multipart form:" , err )
29+ c .AbortWithStatusJSON (http .StatusBadRequest , gin.H {"error" : "Invalid form data or file too large" })
30+ return
31+ }
32+
33+ // Use c.Request.FormValue now that the form is parsed.
34+ stype := c .Request .FormValue ("type" )
35+ pw := c .Request .FormValue ("password" )
36+ ot := c .Request .FormValue ("onetime" )
37+ exp := c .Request .FormValue ("expires" )
2938
3039 log .Println ("CreateSend called with type:" , stype )
3140
@@ -63,7 +72,7 @@ func CreateSend(cfg config.Config, db *gorm.DB) gin.HandlerFunc {
6372 key := deriveKey (pw , cfg )
6473
6574 if stype == "text" {
66- text := c .PostForm ("data" )
75+ text := c .Request . FormValue ("data" )
6776 if text == "" {
6877 log .Println ("Error: 'data' field is empty for text type" )
6978 c .AbortWithStatusJSON (http .StatusBadRequest , gin.H {"error" : "Data field is required for text type" })
@@ -92,30 +101,24 @@ func CreateSend(cfg config.Config, db *gorm.DB) gin.HandlerFunc {
92101 }
93102
94103 if stype == "file" {
95- file , err := c .FormFile ("file" )
104+ // Use c.Request.FormFile now that the form is parsed.
105+ file , header , err := c .Request .FormFile ("file" )
96106 if err != nil {
97107 log .Println ("Error retrieving file from form data:" , err )
98108 c .AbortWithStatusJSON (http .StatusBadRequest , gin.H {"error" : "Failed to retrieve file from form data" })
99109 return
100110 }
111+ defer file .Close ()
101112
102- log .Println ("Received file:" , file .Filename , "Size:" , file .Size )
113+ log .Println ("Received file:" , header .Filename , "Size:" , header .Size )
103114
104- if file .Size > cfg .MaxFileSize {
105- log .Printf ("Error: File size (%d bytes) exceeds maximum allowed size (%d bytes)\n " , file .Size , cfg .MaxFileSize )
115+ if header .Size > cfg .MaxFileSize {
116+ log .Printf ("Error: File size (%d bytes) exceeds maximum allowed size (%d bytes)\n " , header .Size , cfg .MaxFileSize )
106117 c .AbortWithStatusJSON (http .StatusRequestEntityTooLarge , gin.H {"error" : "File size exceeds the maximum allowed limit" })
107118 return
108119 }
109120
110- f , err := file .Open ()
111- if err != nil {
112- log .Println ("Error opening uploaded file:" , err )
113- c .AbortWithStatusJSON (http .StatusInternalServerError , gin.H {"error" : "Failed to open uploaded file" })
114- return
115- }
116- defer f .Close ()
117-
118- data , err := ioutil .ReadAll (f )
121+ data , err := io .ReadAll (file )
119122 if err != nil {
120123 log .Println ("Error reading file data:" , err )
121124 c .AbortWithStatusJSON (http .StatusInternalServerError , gin.H {"error" : "Failed to read file data" })
@@ -130,8 +133,7 @@ func CreateSend(cfg config.Config, db *gorm.DB) gin.HandlerFunc {
130133 }
131134
132135 fp := filepath .Join (cfg .StoragePath , hash )
133- err = ioutil .WriteFile (fp , []byte (enc ), 0600 )
134- if err != nil {
136+ if err := os .WriteFile (fp , []byte (enc ), 0600 ); err != nil {
135137 log .Println ("Error writing encrypted file to storage:" , err )
136138 c .AbortWithStatusJSON (http .StatusInternalServerError , gin.H {"error" : "Failed to write encrypted file to storage" })
137139 return
@@ -143,7 +145,7 @@ func CreateSend(cfg config.Config, db *gorm.DB) gin.HandlerFunc {
143145 Hash : hash ,
144146 Type : "file" ,
145147 FilePath : fp ,
146- FileName : file .Filename ,
148+ FileName : header .Filename ,
147149 Password : pw ,
148150 OneTime : oneTime ,
149151 ExpiresAt : expiresAt ,
@@ -192,7 +194,7 @@ func GetSend(cfg config.Config, db *gorm.DB) gin.HandlerFunc {
192194 }
193195 c .String (http .StatusOK , string (d ))
194196 } else {
195- d , err := ioutil .ReadFile (s .FilePath )
197+ d , err := os .ReadFile (s .FilePath )
196198 if err != nil {
197199 c .AbortWithStatus (http .StatusInternalServerError )
198200 return
@@ -246,4 +248,4 @@ func CheckPasswordProtection(db *gorm.DB) gin.HandlerFunc {
246248 // Return whether the send requires a password
247249 c .JSON (http .StatusOK , gin.H {"requiresPassword" : s .Password != "" })
248250 }
249- }
251+ }
0 commit comments