Skip to content

Commit

Permalink
feat: read raw file endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelgautier committed Jan 17, 2024
1 parent 003d336 commit 515f0d4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
37 changes: 26 additions & 11 deletions api/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,33 @@ func (h *Handler) GetFiles(c *gin.Context) {
}

repo := hosting.Repository{Owner: uri.Owner, Name: uri.Repo}
file, files, err := hsting.(hosting.Hosting).GetFiles(c.Request.Context(), &repo, uri.Path)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadGateway, gin.H{
"error": err.Error(),
})
switch c.NegotiateFormat(gin.MIMEJSON, RawMimeTypes) {
case RawMimeTypes:
file, err := hsting.(hosting.Hosting).GetRawFile(c.Request.Context(), &repo, uri.Path)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadGateway, gin.H{
"error": err.Error(),
})
return
}

c.Data(200, RawMimeTypes, file)
return
}

if file != nil {
c.JSON(200, file)
return
default:
file, files, err := hsting.(hosting.Hosting).GetFiles(c.Request.Context(), &repo, uri.Path)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadGateway, gin.H{
"error": err.Error(),
})
return
}

if file != nil {
c.JSON(200, file)
return
}

c.JSON(200, files)
}

c.JSON(200, files)
}
4 changes: 4 additions & 0 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"github.com/thegalactiks/giteway/internal/config"
)

const (
RawMimeTypes string = "application/vnd.giteway.raw"
)

type Handler struct {
c *config.Config
e *gin.Engine
Expand Down
1 change: 1 addition & 0 deletions hosting/hosting.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ type Hosting interface {
GetBranches(ctx context.Context, repo *Repository) ([]Branch, error)
GetCommits(ctx context.Context, repo *Repository) ([]Commit, error)
GetFiles(ctx context.Context, repo *Repository, path string) (*File, []File, error)
GetRawFile(ctx context.Context, repo *Repository, path string) ([]byte, error)
}
19 changes: 19 additions & 0 deletions internal/hosting/github/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"context"
"errors"

"github.com/google/go-github/v58/github"
"github.com/thegalactiks/giteway/hosting"
Expand Down Expand Up @@ -42,3 +43,21 @@ func (h *HostingGithub) GetFiles(ctx context.Context, repo *hosting.Repository,

return nil, files, nil
}

func (h *HostingGithub) GetRawFile(ctx context.Context, repo *hosting.Repository, path string) ([]byte, error) {
fileContent, _, _, err := h.client.Repositories.GetContents(ctx, repo.Owner, repo.Name, path, &github.RepositoryContentGetOptions{})
if err != nil {
return nil, err
}

if fileContent == nil {
return nil, errors.New("the path should be a valid file path")
}

c, err := fileContent.GetContent()
if err != nil {
return nil, err
}

return []byte(c), nil
}
12 changes: 12 additions & 0 deletions internal/hosting/gitlab/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ func (h *HostingGitlab) GetFiles(ctx context.Context, repo *hosting.Repository,

return nil, files, nil
}

func (h *HostingGitlab) GetRawFile(ctx context.Context, repo *hosting.Repository, path string) ([]byte, error) {
pid := createPid(repo)
pathWithoutSlash := strings.TrimLeft(path, "/")
ref := "master"

file, _, err := h.client.RepositoryFiles.GetRawFile(pid, pathWithoutSlash, &gitlab.GetRawFileOptions{
Ref: &ref,
})

return file, err
}

0 comments on commit 515f0d4

Please sign in to comment.