1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-24 10:07:21 +02:00
woodpecker/store/datastore/files.go

64 lines
1.6 KiB
Go
Raw Normal View History

2017-03-28 10:53:06 +02:00
package datastore
import (
"bytes"
"io"
"io/ioutil"
"github.com/drone/drone/model"
"github.com/drone/drone/store/datastore/sql"
"github.com/russross/meddler"
)
func (db *datastore) FileList(build *model.Build) ([]*model.File, error) {
2017-03-28 11:13:13 +02:00
stmt := sql.Lookup(db.driver, "files-find-build")
2017-03-28 10:53:06 +02:00
list := []*model.File{}
err := meddler.QueryAll(db, &list, stmt, build.ID)
return list, err
}
func (db *datastore) FileFind(proc *model.Proc, name string) (*model.File, error) {
2017-03-28 11:13:13 +02:00
stmt := sql.Lookup(db.driver, "files-find-proc-name")
2017-03-28 10:53:06 +02:00
file := new(model.File)
err := meddler.QueryRow(db, file, stmt, proc.ID, name)
return file, err
}
func (db *datastore) FileRead(proc *model.Proc, name string) (io.ReadCloser, error) {
2017-03-28 11:13:13 +02:00
stmt := sql.Lookup(db.driver, "files-find-proc-name-data")
2017-03-28 10:53:06 +02:00
file := new(fileData)
err := meddler.QueryRow(db, file, stmt, proc.ID, name)
buf := bytes.NewBuffer(file.Data)
return ioutil.NopCloser(buf), err
}
func (db *datastore) FileCreate(file *model.File, r io.Reader) error {
d, err := ioutil.ReadAll(r)
if err != nil {
return err
}
f := fileData{
ID: file.ID,
BuildID: file.BuildID,
ProcID: file.ProcID,
Name: file.Name,
Size: file.Size,
Mime: file.Mime,
Time: file.Time,
Data: d,
}
return meddler.Insert(db, "files", &f)
}
type fileData struct {
ID int64 `meddler:"file_id,pk"`
BuildID int64 `meddler:"file_build_id"`
ProcID int64 `meddler:"file_proc_id"`
Name string `meddler:"file_name"`
Size int `meddler:"file_size"`
Mime string `meddler:"file_mime"`
Time int64 `meddler:"file_time"`
Data []byte `meddler:"file_data"`
}