diff --git a/gitea/git_blob.go b/gitea/git_blob.go index abdd95a..1c1e4d4 100644 --- a/gitea/git_blob.go +++ b/gitea/git_blob.go @@ -4,11 +4,38 @@ package gitea -// GitBlobResponse represents a git blob -type GitBlobResponse struct { - Content string `json:"content"` - Encoding string `json:"encoding"` - URL string `json:"url"` - SHA string `json:"sha"` - Size int64 `json:"size"` +import ( + "bytes" + "encoding/json" + + "code.gitea.io/gitea/modules/git" +) + +// BlobResponse represents a git blob +type BlobResponse struct { + Content *BlobContentResponse `json:"content"` + Encoding string `json:"encoding"` + URL string `json:"url"` + SHA string `json:"sha"` + Size int64 `json:"size"` +} + +// BlobContentResponse is a wrapper for a git.Blob to be serializable +type BlobContentResponse struct { + *git.Blob +} + +// MarshalJSON Marshals the BlobContentResponse by reading the blob so it can be encoded to base64 +func (bc *BlobContentResponse) MarshalJSON() ([]byte, error) { + reader, err := bc.DataAsync() + if err != nil { + return nil, err + } + defer reader.Close() + buf := new(bytes.Buffer) + _, err = buf.ReadFrom(reader) + if err != nil { + return nil, err + } + return json.Marshal(buf.Bytes()) } diff --git a/gitea/repo_file.go b/gitea/repo_file.go index 1e9e45e..13c4237 100644 --- a/gitea/repo_file.go +++ b/gitea/repo_file.go @@ -30,19 +30,20 @@ type CreateFileOptions struct { Content string `json:"content"` } -// DeleteFileOptions options for deleting files (used for other File structs below) -type DeleteFileOptions struct { - FileOptions - SHA string `json:"sha" binding:"Required"` -} - // UpdateFileOptions options for updating files type UpdateFileOptions struct { - DeleteFileOptions + FileOptions + SHA string `json:"sha" binding:"Required"` Content string `json:"content"` FromPath string `json:"from_path" binding:"MaxSize(500)"` } +// DeleteFileOptions options for deleting files (used for other File structs below) +type DeleteFileOptions struct { + FileOptions + SHA string `json:"sha" binding:"Required"` +} + // FileLinksResponse contains the links for a repo's file type FileLinksResponse struct { Self string `json:"url"`