Skip to content

Commit dafbca0

Browse files
committed
download from artefact store
1 parent 3189bd9 commit dafbca0

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

backend/models/storage.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,14 @@ func (db *Database) DeleteJobTokenArtefacts(jobTokenId uint) error {
11371137

11381138
}
11391139

1140+
func (db *Database) GetJobArtefact(jobTokenId uint) (*JobArtefact, error) {
1141+
var artefact JobArtefact
1142+
if err := DB.GormDB.Where("job_token_id = ?", jobTokenId).First(&artefact).Error; err != nil {
1143+
return nil, err
1144+
}
1145+
return &artefact, nil
1146+
}
1147+
11401148
func (db *Database) CreateGithubAppInstallation(installationId int64, githubAppId int64, login string, accountId int, repoFullName string) (*GithubAppInstallation, error) {
11411149
installation := &GithubAppInstallation{
11421150
GithubInstallationId: installationId,

ee/backend/controllers/artefacts.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package controllers
22

33
import (
4+
"fmt"
45
"github.com/diggerhq/digger/backend/middleware"
56
"github.com/diggerhq/digger/backend/models"
67
"github.com/gin-gonic/gin"
8+
"github.com/google/uuid"
79
"io"
810
"log"
911
"net/http"
@@ -55,7 +57,7 @@ func SetJobArtefact(c *gin.Context) {
5557
// Create a new File record
5658
artefactRecord := models.JobArtefact{
5759
JobTokenID: jobToken.ID,
58-
Filename: file.Filename,
60+
Filename: uuid.NewString(),
5961
Contents: content,
6062
Size: file.Size,
6163
ContentType: file.Header.Get("Content-Type"),
@@ -72,5 +74,29 @@ func SetJobArtefact(c *gin.Context) {
7274
}
7375

7476
func DownloadJobArtefact(c *gin.Context) {
77+
jobTokenValue, exists := c.Get(middleware.JOB_TOKEN_KEY)
78+
if !exists {
79+
c.String(http.StatusBadRequest, "missing value: bearer job token")
80+
return
81+
}
82+
83+
jobToken, err := models.DB.GetJobToken(jobTokenValue)
84+
if err != nil {
85+
c.String(http.StatusBadRequest, "could not find job token")
86+
return
87+
}
88+
89+
artefact, err := models.DB.GetJobArtefact(jobToken.ID)
90+
if err != nil {
91+
c.String(http.StatusBadRequest, "could not find any artefacts for job token")
92+
return
93+
}
94+
95+
// Set the appropriate headers
96+
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", artefact.Filename))
97+
c.Header("Content-Type", artefact.ContentType)
98+
c.Header("Content-Length", fmt.Sprintf("%d", artefact.Size))
7599

100+
// Write the file contents to the response
101+
c.Data(http.StatusOK, artefact.ContentType, artefact.Contents)
76102
}

ee/backend/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ func main() {
8282
jobArtefactsGroup := r.Group("/job_artefacts")
8383
jobArtefactsGroup.Use(middleware.GetApiMiddleware())
8484
jobArtefactsGroup.PUT("/", controllers.SetJobArtefact)
85+
jobArtefactsGroup.GET("/", controllers.DownloadJobArtefact)
86+
8587
port := config.GetPort()
8688
r.Run(fmt.Sprintf(":%d", port))
8789
}

0 commit comments

Comments
 (0)