@@ -290,7 +290,26 @@ func DownloadManifest(ctx *context.Context) {
290290 })
291291}
292292
293- // https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#endpoint-6
293+ // formFileOptionalReadCloser returns (nil, nil) if the formKey is not present.
294+ func formFileOptionalReadCloser (ctx * context.Context , formKey string ) (io.ReadCloser , error ) {
295+ var file io.ReadCloser
296+ multipartFile , _ , err := ctx .Req .FormFile (formKey ) // it calls ParseMultipartForm automatically
297+ if err != nil && ! errors .Is (err , http .ErrMissingFile ) {
298+ return nil , err
299+ }
300+ if multipartFile != nil {
301+ return multipartFile , nil
302+ }
303+
304+ _ = ctx .Req .ParseForm () // although ParseForm should have been called by FormFile->ParseMultipartForm, it's safe to call it again
305+ if ! ctx .Req .Form .Has (formKey ) {
306+ return nil , nil
307+ }
308+ file = io .NopCloser (strings .NewReader (ctx .Req .FormValue (formKey )))
309+ return file , nil
310+ }
311+
312+ // UploadPackageFile refers to https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#endpoint-6
294313func UploadPackageFile (ctx * context.Context ) {
295314 packageScope := ctx .PathParam ("scope" )
296315 packageName := ctx .PathParam ("name" )
@@ -304,23 +323,11 @@ func UploadPackageFile(ctx *context.Context) {
304323
305324 packageVersion := v .Core ().String ()
306325
307- var file io.ReadCloser
308- multipartFile , _ , err := ctx .Req .FormFile ("source-archive" )
309- if err != nil && ! errors .Is (err , http .ErrMissingFile ) {
310- apiError (ctx , http .StatusBadRequest , err )
326+ file , err := formFileOptionalReadCloser (ctx , "source-archive" )
327+ if file == nil || err != nil {
328+ apiError (ctx , http .StatusBadRequest , "unable to read source-archive file" )
311329 return
312330 }
313-
314- if multipartFile != nil {
315- file = multipartFile
316- } else {
317- content := ctx .Req .FormValue ("source-archive" )
318- if content == "" {
319- apiError (ctx , http .StatusBadRequest , "source-archive is required either as file or form value" )
320- return
321- }
322- file = io .NopCloser (strings .NewReader (content ))
323- }
324331 defer file .Close ()
325332
326333 buf , err := packages_module .CreateHashedBufferFromReader (file )
@@ -330,15 +337,10 @@ func UploadPackageFile(ctx *context.Context) {
330337 }
331338 defer buf .Close ()
332339
333- var mr io.ReadCloser
334- metadataFile , _ , err := ctx .Req .FormFile ("metadata" )
340+ mr , err := formFileOptionalReadCloser (ctx , "metadata" )
335341 if err != nil {
336- metadata := ctx .Req .FormValue ("metadata" )
337- if metadata != "" {
338- mr = io .NopCloser (strings .NewReader (metadata ))
339- }
340- } else {
341- mr = metadataFile
342+ apiError (ctx , http .StatusBadRequest , "unable to read metadata file" )
343+ return
342344 }
343345 if mr != nil {
344346 defer mr .Close ()
0 commit comments