implement file endpoints

This commit is contained in:
Brad Rydzewski 2017-07-27 13:06:24 -04:00
parent 825dc630b9
commit bcdd5c9d06
18 changed files with 168 additions and 3 deletions

View file

@ -13,11 +13,11 @@ type FileStore interface {
// File represents a pipeline artifact.
type File struct {
ID int64 `json:"id" meddler:"file_id,pk"`
BuildID int64 `json:"build_id" meddler:"file_build_id"`
BuildID int64 `json:"-" meddler:"file_build_id"`
ProcID int64 `json:"proc_id" meddler:"file_proc_id"`
PID int `json:"pid" meddler:"file_pid"`
Name string `json:"name" meddler:"file_name"`
Size int `json:"size" meddler:"file_size"`
Mime string `json:"mime" meddler:"file_mime"`
Time int64 `json:"time" meddler:"file_time"`
// Data []byte `json:"data" meddler:"file_data"`
}

View file

@ -73,6 +73,9 @@ func Load(middleware ...gin.HandlerFunc) http.Handler {
repo.GET("/builds/:number", server.GetBuild)
repo.GET("/logs/:number/:ppid/:proc", server.GetBuildLogs)
repo.GET("/files/:number", server.FileList)
repo.GET("/files/:number/:proc/*file", server.FileGet)
// requires push permissions
repo.GET("/secrets", session.MustPush, server.GetSecretList)
repo.POST("/secrets", session.MustPush, server.PostSecret)

96
server/file.go Normal file
View file

@ -0,0 +1,96 @@
package server
import (
"io"
"net/http"
"strconv"
"strings"
"github.com/drone/drone/router/middleware/session"
"github.com/drone/drone/store"
"github.com/gin-gonic/gin"
)
// FileList gets a list file by build.
func FileList(c *gin.Context) {
num, err := strconv.Atoi(c.Param("number"))
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
repo := session.Repo(c)
build, err := store.FromContext(c).GetBuildNumber(repo, num)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
files, err := store.FromContext(c).FileList(build)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.JSON(200, files)
}
// FileGet gets a file by process and name
func FileGet(c *gin.Context) {
var (
repo = session.Repo(c)
name = strings.TrimPrefix(c.Param("file"), "/")
raw = func() bool {
return c.DefaultQuery("raw", "false") == "true"
}()
)
num, err := strconv.Atoi(c.Param("number"))
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
pid, err := strconv.Atoi(c.Param("proc"))
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
build, err := store.FromContext(c).GetBuildNumber(repo, num)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
proc, err := store.FromContext(c).ProcFind(build, pid)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
file, err := store.FromContext(c).FileFind(proc, name)
if err != nil {
c.String(404, "Error getting file %q. %s", name, err)
return
}
if !raw {
c.JSON(200, file)
return
}
rc, err := store.FromContext(c).FileRead(proc, file.Name)
if err != nil {
c.String(404, "Error getting file stream %q. %s", name, err)
return
}
defer rc.Close()
switch file.Mime {
case "application/vnd.drone.test+json":
c.Header("Content-Type", "application/json")
}
io.Copy(c.Writer, rc)
}

View file

@ -235,6 +235,7 @@ func (s *RPC) Upload(c context.Context, id string, file *rpc.File) error {
return Config.Storage.Files.FileCreate(&model.File{
BuildID: proc.BuildID,
ProcID: proc.ID,
PID: proc.PID,
Mime: file.Mime,
Name: file.Name,
Size: file.Size,

View file

@ -20,7 +20,7 @@ type syncer struct {
}
func (s *syncer) Sync(user *model.User) error {
unix := time.Now().Unix() - 1 // force immediate expiration
unix := time.Now().Unix() - (3601) // force immediate expiration. note 1 hour expiration is hard coded at the moment
repos, err := s.remote.Repos(user)
if err != nil {
return err

View file

@ -136,6 +136,10 @@ var migrations = []struct {
name: "create-index-perms-user",
stmt: createIndexPermsUser,
},
{
name: "alter-table-add-file-pid",
stmt: alterTableAddFilePid,
},
}
// Migrate performs the database migration. If the migration fails
@ -575,3 +579,11 @@ CREATE INDEX ix_perms_repo ON perms (perm_repo_id);
var createIndexPermsUser = `
CREATE INDEX ix_perms_user ON perms (perm_user_id);
`
//
// 018_add_column_file_pid.sql
//
var alterTableAddFilePid = `
ALTER TABLE files ADD COLUMN file_pid INTEGER
`

View file

@ -0,0 +1,3 @@
-- name: alter-table-add-file-pid
ALTER TABLE files ADD COLUMN file_pid INTEGER

View file

@ -136,6 +136,10 @@ var migrations = []struct {
name: "create-index-perms-user",
stmt: createIndexPermsUser,
},
{
name: "alter-table-add-file-pid",
stmt: alterTableAddFilePid,
},
}
// Migrate performs the database migration. If the migration fails
@ -575,3 +579,11 @@ CREATE INDEX IF NOT EXISTS ix_perms_repo ON perms (perm_repo_id);
var createIndexPermsUser = `
CREATE INDEX IF NOT EXISTS ix_perms_user ON perms (perm_user_id);
`
//
// 018_add_column_file_pid.sql
//
var alterTableAddFilePid = `
ALTER TABLE files ADD COLUMN file_pid INTEGER
`

View file

@ -0,0 +1,3 @@
-- name: alter-table-add-file-pid
ALTER TABLE files ADD COLUMN file_pid INTEGER

View file

@ -140,6 +140,10 @@ var migrations = []struct {
name: "create-index-perms-user",
stmt: createIndexPermsUser,
},
{
name: "alter-table-add-file-pid",
stmt: alterTableAddFilePid,
},
}
// Migrate performs the database migration. If the migration fails
@ -576,3 +580,11 @@ CREATE INDEX IF NOT EXISTS ix_perms_repo ON perms (perm_repo_id);
var createIndexPermsUser = `
CREATE INDEX IF NOT EXISTS ix_perms_user ON perms (perm_user_id);
`
//
// 018_add_column_file_pid.sql
//
var alterTableAddFilePid = `
ALTER TABLE files ADD COLUMN file_pid INTEGER
`

View file

@ -0,0 +1,3 @@
-- name: alter-table-add-file-pid
ALTER TABLE files ADD COLUMN file_pid INTEGER

View file

@ -42,6 +42,7 @@ func (db *datastore) FileCreate(file *model.File, r io.Reader) error {
ID: file.ID,
BuildID: file.BuildID,
ProcID: file.ProcID,
PID: file.PID,
Name: file.Name,
Size: file.Size,
Mime: file.Mime,
@ -55,6 +56,7 @@ type fileData struct {
ID int64 `meddler:"file_id,pk"`
BuildID int64 `meddler:"file_build_id"`
ProcID int64 `meddler:"file_proc_id"`
PID int `meddler:"file_pid"`
Name string `meddler:"file_name"`
Size int `meddler:"file_size"`
Mime string `meddler:"file_mime"`

View file

@ -4,6 +4,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size
@ -17,6 +18,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size
@ -31,6 +33,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size

View file

@ -165,6 +165,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size
@ -178,6 +179,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size
@ -192,6 +194,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size

View file

@ -4,6 +4,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size
@ -17,6 +18,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size
@ -31,6 +33,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size

View file

@ -165,6 +165,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size
@ -178,6 +179,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size
@ -192,6 +194,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size

View file

@ -4,6 +4,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size
@ -17,6 +18,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size
@ -31,6 +33,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size

View file

@ -165,6 +165,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size
@ -178,6 +179,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size
@ -192,6 +194,7 @@ SELECT
file_id
,file_build_id
,file_proc_id
,file_pid
,file_name
,file_mime
,file_size