Repository: go-gitea/gitea
File: routers/api/packages/swift/swift.go (Go)

Code before:
	}

	ctx.ServeContent(strings.NewReader(m.Content), &context.ServeHeaderOptions{
		ContentType:  "text/x-swift",
		Filename:     filename,
		LastModified: pv.CreatedUnix.AsLocalTime(),
	})
}

// https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#endpoint-6
func UploadPackageFile(ctx *context.Context) {
	packageScope := ctx.PathParam("scope")
	packageName := ctx.PathParam("name")

	v, err := version.NewVersion(ctx.PathParam("version"))

	if !scopePattern.MatchString(packageScope) || !namePattern.MatchString(packageName) || err != nil {
		apiError(ctx, http.StatusBadRequest, err)
		return
	}

	packageVersion := v.Core().String()

	var file io.ReadCloser
	multipartFile, _, err := ctx.Req.FormFile("source-archive")
	if err != nil {
		content := ctx.Req.FormValue("source-archive")
		if content != "" {
			file = io.NopCloser(strings.NewReader(content))
		} else {
			apiError(ctx, http.StatusBadRequest, err)
			return
		}
	} else {
		file = multipartFile
	}
	defer file.Close()

	buf, err := packages_module.CreateHashedBufferFromReader(file)
	if err != nil {
		apiError(ctx, http.StatusInternalServerError, err)
		return
	}
	defer buf.Close()

	var mr io.ReadCloser
	metadataFile, _, err := ctx.Req.FormFile("metadata")
	if err != nil {
		metadata := ctx.Req.FormValue("metadata")
		if metadata != "" {
			mr = io.NopCloser(strings.NewReader(metadata))

Review comment (bug):
The error here I think may need to be handled separately, if it's a http.ErrMissingFile error then we need to get the file content from FromValue, otherwise it just returns an error.

Code after:
	}

	ctx.ServeContent(strings.NewReader(m.Content), &context.ServeHeaderOptions{
		ContentType:  "text/x-swift",
		Filename:     filename,
		LastModified: pv.CreatedUnix.AsLocalTime(),
	})
}

// formFileOptionalReadCloser returns (nil, nil) if the formKey is not present.
func formFileOptionalReadCloser(ctx *context.Context, formKey string) (io.ReadCloser, error) {
	multipartFile, _, err := ctx.Req.FormFile(formKey)
	if err != nil && !errors.Is(err, http.ErrMissingFile) {
		return nil, err
	}
	if multipartFile != nil {
		return multipartFile, nil
	}

	content := ctx.Req.FormValue(formKey)
	if content == "" {
		return nil, nil
	}
	return io.NopCloser(strings.NewReader(ctx.Req.FormValue(formKey))), nil
}

// UploadPackageFile refers to https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/PackageRegistry/Registry.md#endpoint-6
func UploadPackageFile(ctx *context.Context) {
	packageScope := ctx.PathParam("scope")
	packageName := ctx.PathParam("name")

	v, err := version.NewVersion(ctx.PathParam("version"))

	if !scopePattern.MatchString(packageScope) || !namePattern.MatchString(packageName) || err != nil {
		apiError(ctx, http.StatusBadRequest, err)
		return
	}

	packageVersion := v.Core().String()

	file, err := formFileOptionalReadCloser(ctx, "source-archive")
	if file == nil || err != nil {
		apiError(ctx, http.StatusBadRequest, "unable to read source-archive file")
		return
	}
	defer file.Close()

	buf, err := packages_module.CreateHashedBufferFromReader(file)
	if err != nil {
		apiError(ctx, http.StatusInternalServerError, err)
		return