forked from Gitlink/gitea-1120-rc1
62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
package trace
|
|
|
|
import (
|
|
// "fmt"
|
|
"os"
|
|
// "path/filepath"
|
|
"io/ioutil"
|
|
// "net/http"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/context"
|
|
)
|
|
|
|
type LogDownloadOptions struct {
|
|
FileName string `json:"filename" binding:"Required"`
|
|
}
|
|
|
|
func GetLogList(ctx *context.APIContext) {
|
|
var logList []string
|
|
|
|
dir_list, err := ioutil.ReadDir(setting.LogRootPath)
|
|
if err != nil {
|
|
ctx.ServerError("logList", err)
|
|
return
|
|
}
|
|
|
|
for i:=0; i<len(dir_list); i++ {
|
|
// fmt.Println(i, "=", v.Name())
|
|
logList = append(logList, dir_list[i].Name())
|
|
}
|
|
|
|
ctx.JSON(200, &logList)
|
|
}
|
|
|
|
type NotFoundError struct {
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
func DownloadLog(ctx *context.APIContext) {
|
|
file_path := ctx.Params(":filename")
|
|
// file_path := data.FileName
|
|
path := setting.LogRootPath+"/"+file_path
|
|
b, err := PathExists(path)
|
|
if !b || err != nil {
|
|
// ctx.Error(http.StatusNotFound, "NoSuchFile", fmt.Sprintf("no such file: %s", file_path))
|
|
ctx.JSON(200, &NotFoundError{Message: "no such file!"})
|
|
} else {
|
|
ctx.ServeFile(path, file_path)
|
|
}
|
|
}
|
|
|
|
func PathExists(path string) (bool, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
if err == nil {
|
|
return true, nil
|
|
}
|
|
if os.IsNotExist(err) {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
} |