@@ -3,15 +3,57 @@ package http
33import (
44 "fmt"
55 "mime"
6+ "mime/multipart"
67 "net/http"
78 "path/filepath"
89 "strings"
910
1011 "github.com/gin-gonic/gin"
12+ "github.com/go-logr/logr"
1113 "github.com/google/uuid"
1214)
1315
1416func (s * PostServer ) postFile (ctx * gin.Context ) {
17+ processPostedFiles (ctx , s .Server , s .saveFormFilesCallback , s .saveBodyFileCallback )
18+ }
19+
20+ func (s * PostServer ) saveFormFilesCallback (ctx * gin.Context , postLog logr.Logger , executionID string , node string , file * multipart.FileHeader ) error {
21+ if err := s .mkdir (executionID ); err != nil {
22+ ctx .String (http .StatusInternalServerError , err .Error ())
23+ postLog .Error (err , "error creating upload directory" )
24+ return err
25+ }
26+
27+ err := ctx .SaveUploadedFile (file , filepath .Join (s .ReportPath , executionID , fmt .Sprintf ("%s-%s" , node , file .Filename )))
28+ if err != nil {
29+ ctx .String (http .StatusInternalServerError , err .Error ())
30+ postLog .Error (err , "error saving file" )
31+ return err
32+ }
33+ return nil
34+ }
35+
36+ func (s * PostServer ) saveBodyFileCallback (ctx * gin.Context , postLog logr.Logger , executionID string , node string , fileName string , body []byte ) error {
37+ fileName , err := s .SaveFile (executionID , fmt .Sprintf ("%s-%s" , node , fileName ), body )
38+ postLog = postLog .WithValues (
39+ "name" , filepath .Base (fileName ),
40+ "path" , fileName ,
41+ "length" , len (body ),
42+ )
43+ if err != nil {
44+ ctx .String (http .StatusInternalServerError , err .Error ())
45+ postLog .Error (err , "error receiving file" )
46+ return err
47+ }
48+ return nil
49+ }
50+
51+ type (
52+ saveFormFiles func (ctx * gin.Context , postLog logr.Logger , executionID string , node string , file * multipart.FileHeader ) error
53+ saveBodyFile func (ctx * gin.Context , postLog logr.Logger , executionID string , node string , fileName string , body []byte ) error
54+ )
55+
56+ func processPostedFiles (ctx * gin.Context , s * Server , ffCallback saveFormFiles , bfCallback saveBodyFile ) {
1557 node , executionID := nodeAndID (ctx )
1658 postLog := s .Log .WithValues (
1759 "node" , node ,
@@ -24,19 +66,10 @@ func (s *PostServer) postFile(ctx *gin.Context) {
2466 for _ , files := range form .File {
2567 for _ , file := range files {
2668
27- // Upload the file to specific dst.
28- if err := s .mkdir (executionID ); err != nil {
29- ctx .String (http .StatusInternalServerError , err .Error ())
30- postLog .Error (err , "error creating upload directory" )
69+ if ffCallback (ctx , postLog , executionID , node , file ) != nil {
3170 return
3271 }
3372
34- err := ctx .SaveUploadedFile (file , filepath .Join (s .ReportPath , executionID , fmt .Sprintf ("%s-%s" , node , file .Filename )))
35- if err != nil {
36- ctx .String (http .StatusInternalServerError , err .Error ())
37- postLog .Error (err , "error saving file" )
38- return
39- }
4073 names = append (names , file .Filename )
4174 }
4275 }
@@ -59,25 +92,18 @@ func (s *PostServer) postFile(ctx *gin.Context) {
5992 if fileName == "" {
6093 fileName = uuid .New ().String ()
6194
62- fileName += s . evaluateExtension (ctx .Request )
95+ fileName += evaluateExtension (ctx .Request )
6396 }
6497
65- fileName , err = s .SaveFile (executionID , fmt .Sprintf ("%s-%s" , node , fileName ), body )
66- postLog = postLog .WithValues (
67- "name" , filepath .Base (fileName ),
68- "path" , fileName ,
69- "length" , len (body ),
70- )
71- if err != nil {
72- ctx .String (http .StatusInternalServerError , err .Error ())
73- postLog .Error (err , "error receiving file" )
98+ if bfCallback (ctx , postLog , executionID , node , fileName , body ) != nil {
7499 return
75100 }
101+
76102 postLog .Info ("received 1 file" )
77103 }
78104}
79105
80- func ( s * PostServer ) evaluateExtension (r * http.Request ) string {
106+ func evaluateExtension (r * http.Request ) string {
81107 ct := r .Header .Get ("Content-Type" )
82108
83109 mt , _ , _ := mime .ParseMediaType (ct )
0 commit comments