feat: [PIPE-24577]: define body for file upload api in openapi def (#3274)

* define body for file upload api in openapi def
This commit is contained in:
Marko Gaćeša 2025-01-15 17:11:41 +00:00 committed by Harness
parent ad7c8d51f2
commit 0006151b23
2 changed files with 20 additions and 19 deletions

View File

@ -32,10 +32,6 @@ type Result struct {
FilePath string `json:"file_path"`
}
const (
fileNameFmt = "%s%s"
)
func (c *Controller) Upload(ctx context.Context,
session *auth.Session,
repoRef string,
@ -50,7 +46,9 @@ func (c *Controller) Upload(ctx context.Context,
if file == nil {
return nil, usererror.BadRequest("no file provided")
}
bufReader := bufio.NewReader(file)
// Check if the file is an image or video
extn, err := c.getFileExtension(bufReader)
if err != nil {
@ -58,13 +56,15 @@ func (c *Controller) Upload(ctx context.Context,
}
identifier := uuid.New().String()
fileName := fmt.Sprintf(fileNameFmt, identifier, extn)
fileName := identifier + extn
fileBucketPath := getFileBucketPath(repo.ID, fileName)
err = c.blobStore.Upload(ctx, bufReader, fileBucketPath)
if err != nil {
return nil, fmt.Errorf("failed to upload file: %w", err)
}
return &Result{
FilePath: fileName,
}, nil

View File

@ -20,26 +20,24 @@ import (
"github.com/harness/gitness/app/api/controller/upload"
"github.com/harness/gitness/app/api/usererror"
"github.com/gotidy/ptr"
"github.com/swaggest/openapi-go/openapi3"
)
type UploadRequest struct {
repoRequest
// Note: Below line won't produce the file upload interface in Swagger UI,
// ref: https://swagger.io/docs/specification/2-0/file-upload/
Content string `json:"-" format:"binary" description:"Binary file to upload"`
}
type DownloadRequest struct {
repoRequest
FilePathRef string `path:"file_ref"`
}
func uploadOperations(reflector *openapi3.Reflector) {
opUpload := openapi3.Operation{}
opUpload.WithTags("upload")
opUpload.WithMapOfAnything(map[string]interface{}{"operationId": "repoArtifactUpload"})
_ = reflector.SetRequest(&opUpload, new(UploadRequest), http.MethodPost)
opUpload.WithRequestBody(openapi3.RequestBodyOrRef{
RequestBody: &openapi3.RequestBody{
Description: ptr.String("Binary file to upload"),
Content: map[string]openapi3.MediaType{
"application/octet-stream": {Schema: &openapi3.SchemaOrRef{}},
},
Required: ptr.Bool(true),
},
})
_ = reflector.SetRequest(&opUpload, repoRequest{}, http.MethodPost)
_ = reflector.SetJSONResponse(&opUpload, new(upload.Result), http.StatusCreated)
_ = reflector.SetJSONResponse(&opUpload, new(usererror.Error), http.StatusBadRequest)
_ = reflector.SetJSONResponse(&opUpload, new(usererror.Error), http.StatusNotFound)
@ -52,7 +50,10 @@ func uploadOperations(reflector *openapi3.Reflector) {
downloadOp := openapi3.Operation{}
downloadOp.WithTags("upload")
downloadOp.WithMapOfAnything(map[string]interface{}{"operationId": "repoArtifactDownload"})
_ = reflector.SetRequest(&downloadOp, new(DownloadRequest), http.MethodGet)
_ = reflector.SetRequest(&downloadOp, struct {
repoRequest
FilePathRef string `path:"file_ref"`
}{}, http.MethodGet)
_ = reflector.SetJSONResponse(&downloadOp, nil, http.StatusTemporaryRedirect)
_ = reflector.SetJSONResponse(&downloadOp, new(usererror.Error), http.StatusNotFound)
_ = reflector.SetJSONResponse(&downloadOp, new(usererror.Error), http.StatusBadRequest)