2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
package api
|
2017-07-27 17:06:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-09-14 12:21:16 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-11-23 14:36:52 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2021-09-22 20:41:32 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
2021-09-23 11:33:59 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2017-07-27 17:06:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FileList gets a list file by build.
|
|
|
|
func FileList(c *gin.Context) {
|
2021-10-28 09:12:58 +00:00
|
|
|
store_ := store.FromContext(c)
|
2021-11-13 19:18:06 +00:00
|
|
|
num, err := strconv.ParseInt(c.Param("number"), 10, 64)
|
2017-07-27 17:06:24 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2017-07-27 17:06:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := session.Repo(c)
|
2021-10-28 09:12:58 +00:00
|
|
|
build, err := store_.GetBuildNumber(repo, num)
|
2017-07-27 17:06:24 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2017-07-27 17:06:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
files, err := store_.FileList(build)
|
2017-07-27 17:06:24 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2017-07-27 17:06:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, files)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileGet gets a file by process and name
|
|
|
|
func FileGet(c *gin.Context) {
|
|
|
|
var (
|
2021-10-28 09:12:58 +00:00
|
|
|
store_ = store.FromContext(c)
|
|
|
|
|
2017-07-27 17:06:24 +00:00
|
|
|
repo = session.Repo(c)
|
|
|
|
name = strings.TrimPrefix(c.Param("file"), "/")
|
|
|
|
raw = func() bool {
|
|
|
|
return c.DefaultQuery("raw", "false") == "true"
|
|
|
|
}()
|
|
|
|
)
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
num, err := strconv.ParseInt(c.Param("number"), 10, 64)
|
2017-07-27 17:06:24 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2017-07-27 17:06:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(c.Param("proc"))
|
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2017-07-27 17:06:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
build, err := store_.GetBuildNumber(repo, num)
|
2017-07-27 17:06:24 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2017-07-27 17:06:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
proc, err := store_.ProcFind(build, pid)
|
2017-07-27 17:06:24 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2017-07-27 17:06:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
file, err := store_.FileFind(proc, name)
|
2017-07-27 17:06:24 +00:00
|
|
|
if err != nil {
|
|
|
|
c.String(404, "Error getting file %q. %s", name, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !raw {
|
|
|
|
c.JSON(200, file)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
rc, err := store_.FileRead(proc, file.Name)
|
2017-07-27 17:06:24 +00:00
|
|
|
if err != nil {
|
|
|
|
c.String(404, "Error getting file stream %q. %s", name, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rc.Close()
|
|
|
|
|
|
|
|
switch file.Mime {
|
2021-10-02 08:59:34 +00:00
|
|
|
case "application/vnd.test+json":
|
2017-07-27 17:06:24 +00:00
|
|
|
c.Header("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
|
2021-11-23 14:36:52 +00:00
|
|
|
if _, err := io.Copy(c.Writer, rc); err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not copy file to http response")
|
|
|
|
}
|
2017-07-27 17:06:24 +00:00
|
|
|
}
|