@@ -50,39 +50,54 @@ func (s *Server) PostTreeHandler(c *gin.Context) {
50
50
path += "/"
51
51
}
52
52
53
- // Create a channel to listen to the responses
54
- res := make (chan repository.PathResultMessage )
55
- go func () {
56
- defer close (res )
53
+ // Get the data from the request body, which must be a multipart/form-data
54
+ mpf , err := c .MultipartForm ()
55
+ if err != nil {
56
+ c .AbortWithError (http .StatusBadRequest , err )
57
+ return
58
+ }
57
59
58
- // Get the data from the request body, which must be a multipart/form-data
59
- mpf , err := c . MultipartForm ()
60
- if err != nil {
61
- c . AbortWithError ( http . StatusBadRequest , err )
62
- return
63
- }
60
+ // Check if we're forcing the request
61
+ force := false
62
+ forceVal , ok := mpf . Value [ "force" ]
63
+ if ok && len ( forceVal ) > 0 {
64
+ force = utils . IsTruthy ( forceVal [ 0 ])
65
+ }
64
66
65
- // Check if we're forcing the request
66
- force := false
67
- forceVal , ok := mpf .Value ["force" ]
68
- if ok && len (forceVal ) > 0 {
69
- force = utils .IsTruthy (forceVal [0 ])
70
- }
67
+ // Create a channel to listen to the responses
68
+ res := make (chan repository.PathResultMessage )
71
69
72
- // Check if we have a path from the local filesystem or a file uploaded
73
- uploadFiles := mpf .File ["file" ]
74
- localPaths := mpf .Value ["localpath" ]
75
- if localPaths != nil && len (localPaths ) > 0 && (uploadFiles == nil || len (uploadFiles ) == 0 ) {
70
+ // Check if we have a path from the local filesystem or a file uploaded
71
+ uploadFiles := mpf .File ["file" ]
72
+ localPaths := mpf .Value ["localpath" ]
73
+ var method func ()
74
+ if localPaths != nil && len (localPaths ) > 0 && (uploadFiles == nil || len (uploadFiles ) == 0 ) {
75
+ method = func () {
76
76
s .addLocalPath (ctx , localPaths , path , force , res )
77
- } else if uploadFiles != nil && len (uploadFiles ) > 0 && (localPaths == nil || len (localPaths ) == 0 ) {
77
+ }
78
+ } else if uploadFiles != nil && len (uploadFiles ) > 0 && (localPaths == nil || len (localPaths ) == 0 ) {
79
+ method = func () {
78
80
s .addUploadedFiles (ctx , uploadFiles , path , force , res )
79
- } else {
80
- c .AbortWithError (http .StatusBadRequest , errors .New ("need to specify one and only one of 'file' or 'localpath' form fields" ))
81
- return
82
81
}
82
+ } else {
83
+ c .AbortWithError (http .StatusBadRequest , errors .New ("need to specify one and only one of 'file' or 'localpath' form fields" ))
84
+ return
85
+ }
86
+
87
+ // Start a transaction
88
+ err = s .Repo .BeginTransaction ()
89
+ if err != nil {
90
+ c .AbortWithError (http .StatusInternalServerError , err )
91
+ return
92
+ }
93
+
94
+ // Add the file(s) in a goroutine
95
+ go func () {
96
+ method ()
97
+ close (res )
83
98
}()
84
99
85
- // Response
100
+ // Read each response from the channel
86
101
response := make ([]TreeOperationResponse , 0 )
87
102
for el := range res {
88
103
r := TreeOperationResponse {
@@ -106,6 +121,13 @@ func (s *Server) PostTreeHandler(c *gin.Context) {
106
121
response = append (response , r )
107
122
}
108
123
124
+ // Commit the transaction
125
+ err = s .Repo .CommitTransaction ()
126
+ if err != nil {
127
+ c .AbortWithError (http .StatusInternalServerError , err )
128
+ return
129
+ }
130
+
109
131
c .JSON (http .StatusOK , response )
110
132
}
111
133
0 commit comments