mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-22 17:31:03 +01:00
- Updated Go version in go.work and backend/go.mod to 1.23.7 - Updated various dependencies in go.sum and backend/go.sum - Refactored models to remove generic type parameters from BaseModel - Introduced new utility functions for consistent API responses - Removed unused utility files from controllers
295 lines
6.1 KiB
Go
295 lines
6.1 KiB
Go
package controllers
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"github.com/crawlab-team/crawlab/core/fs"
|
|
"github.com/crawlab-team/crawlab/core/interfaces"
|
|
"github.com/gin-gonic/gin"
|
|
"io"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
type GetBaseFileListDirParams struct {
|
|
Path string `path:"path"`
|
|
}
|
|
|
|
func GetBaseFileListDir(rootPath string, params *GetBaseFileListDirParams) (response *Response[[]interfaces.FsFileInfo], err error) {
|
|
path := params.Path
|
|
|
|
fsSvc, err := fs.GetBaseFileFsSvc(rootPath)
|
|
if err != nil {
|
|
return GetErrorResponse[[]interfaces.FsFileInfo](err)
|
|
}
|
|
|
|
files, err := fsSvc.List(path)
|
|
if err != nil {
|
|
if !errors.Is(err, os.ErrNotExist) {
|
|
return GetErrorResponse[[]interfaces.FsFileInfo](err)
|
|
}
|
|
}
|
|
|
|
//HandleSuccessWithData(c, files)
|
|
return GetDataResponse[[]interfaces.FsFileInfo](files)
|
|
}
|
|
|
|
type GetBaseFileFileParams struct {
|
|
Path string `path:"path"`
|
|
}
|
|
|
|
func GetBaseFileFile(rootPath string, params *GetBaseFileFileParams) (response *Response[string], err error) {
|
|
path := params.Path
|
|
|
|
fsSvc, err := fs.GetBaseFileFsSvc(rootPath)
|
|
if err != nil {
|
|
return GetErrorResponse[string](err)
|
|
}
|
|
|
|
data, err := fsSvc.GetFile(path)
|
|
if err != nil {
|
|
return GetErrorResponse[string](err)
|
|
}
|
|
|
|
return GetDataResponse[string](string(data))
|
|
}
|
|
|
|
func GetBaseFileFileInfo(rootPath string, c *gin.Context) {
|
|
path := c.Query("path")
|
|
|
|
fsSvc, err := fs.GetBaseFileFsSvc(rootPath)
|
|
if err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
|
|
info, err := fsSvc.GetFileInfo(path)
|
|
if err != nil {
|
|
HandleErrorInternalServerError(c, err)
|
|
return
|
|
}
|
|
|
|
HandleSuccessWithData(c, info)
|
|
}
|
|
|
|
func PostBaseFileSaveFile(rootPath string, c *gin.Context) {
|
|
fsSvc, err := fs.GetBaseFileFsSvc(rootPath)
|
|
if err != nil {
|
|
HandleErrorInternalServerError(c, err)
|
|
return
|
|
}
|
|
|
|
if c.GetHeader("Content-Type") == "application/json" {
|
|
var payload struct {
|
|
Path string `json:"path"`
|
|
Data string `json:"data"`
|
|
}
|
|
if err := c.ShouldBindJSON(&payload); err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
if err := fsSvc.Save(payload.Path, []byte(payload.Data)); err != nil {
|
|
HandleErrorInternalServerError(c, err)
|
|
return
|
|
}
|
|
} else {
|
|
path, ok := c.GetPostForm("path")
|
|
if !ok {
|
|
HandleErrorBadRequest(c, errors.New("missing required field 'path'"))
|
|
return
|
|
}
|
|
file, err := c.FormFile("file")
|
|
if err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
f, err := file.Open()
|
|
if err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
fileData, err := io.ReadAll(f)
|
|
if err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
if err := fsSvc.Save(path, fileData); err != nil {
|
|
HandleErrorInternalServerError(c, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
HandleSuccess(c)
|
|
}
|
|
|
|
func PostBaseFileSaveFiles(rootPath string, c *gin.Context) {
|
|
fsSvc, err := fs.GetBaseFileFsSvc(rootPath)
|
|
if err != nil {
|
|
HandleErrorInternalServerError(c, err)
|
|
return
|
|
}
|
|
|
|
form, err := c.MultipartForm()
|
|
if err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(len(form.File))
|
|
for path := range form.File {
|
|
go func(path string) {
|
|
file, err := c.FormFile(path)
|
|
if err != nil {
|
|
logger.Warnf("invalid file header: %s", path)
|
|
logger.Error(err.Error())
|
|
wg.Done()
|
|
return
|
|
}
|
|
f, err := file.Open()
|
|
if err != nil {
|
|
logger.Warnf("unable to open file: %s", path)
|
|
logger.Error(err.Error())
|
|
wg.Done()
|
|
return
|
|
}
|
|
fileData, err := io.ReadAll(f)
|
|
if err != nil {
|
|
logger.Warnf("unable to read file: %s", path)
|
|
logger.Error(err.Error())
|
|
wg.Done()
|
|
return
|
|
}
|
|
if err := fsSvc.Save(path, fileData); err != nil {
|
|
logger.Warnf("unable to save file: %s", path)
|
|
logger.Error(err.Error())
|
|
wg.Done()
|
|
return
|
|
}
|
|
wg.Done()
|
|
}(path)
|
|
}
|
|
wg.Wait()
|
|
|
|
HandleSuccess(c)
|
|
}
|
|
|
|
func PostBaseFileSaveDir(rootPath string, c *gin.Context) {
|
|
var payload struct {
|
|
Path string `json:"path"`
|
|
NewPath string `json:"new_path"`
|
|
Data string `json:"data"`
|
|
}
|
|
if err := c.ShouldBindJSON(&payload); err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
|
|
fsSvc, err := fs.GetBaseFileFsSvc(rootPath)
|
|
if err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
|
|
if err := fsSvc.CreateDir(payload.Path); err != nil {
|
|
HandleErrorInternalServerError(c, err)
|
|
return
|
|
}
|
|
|
|
HandleSuccess(c)
|
|
}
|
|
|
|
func PostBaseFileRenameFile(rootPath string, c *gin.Context) {
|
|
var payload struct {
|
|
Path string `json:"path"`
|
|
NewPath string `json:"new_path"`
|
|
}
|
|
if err := c.ShouldBindJSON(&payload); err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
|
|
fsSvc, err := fs.GetBaseFileFsSvc(rootPath)
|
|
if err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
|
|
if err := fsSvc.Rename(payload.Path, payload.NewPath); err != nil {
|
|
HandleErrorInternalServerError(c, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func DeleteBaseFileFile(rootPath string, c *gin.Context) {
|
|
var payload struct {
|
|
Path string `json:"path"`
|
|
}
|
|
if err := c.ShouldBindJSON(&payload); err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
if payload.Path == "~" {
|
|
payload.Path = "."
|
|
}
|
|
|
|
fsSvc, err := fs.GetBaseFileFsSvc(rootPath)
|
|
if err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
|
|
if err := fsSvc.Delete(payload.Path); err != nil {
|
|
HandleErrorInternalServerError(c, err)
|
|
return
|
|
}
|
|
_, err = fsSvc.GetFileInfo(".")
|
|
if err != nil {
|
|
_ = fsSvc.CreateDir("/")
|
|
}
|
|
|
|
HandleSuccess(c)
|
|
}
|
|
|
|
func PostBaseFileCopyFile(rootPath string, c *gin.Context) {
|
|
var payload struct {
|
|
Path string `json:"path"`
|
|
NewPath string `json:"new_path"`
|
|
}
|
|
if err := c.ShouldBindJSON(&payload); err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
|
|
fsSvc, err := fs.GetBaseFileFsSvc(rootPath)
|
|
if err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
|
|
if err := fsSvc.Copy(payload.Path, payload.NewPath); err != nil {
|
|
HandleErrorInternalServerError(c, err)
|
|
return
|
|
}
|
|
|
|
HandleSuccess(c)
|
|
}
|
|
|
|
func PostBaseFileExport(rootPath string, c *gin.Context) {
|
|
fsSvc, err := fs.GetBaseFileFsSvc(rootPath)
|
|
if err != nil {
|
|
HandleErrorBadRequest(c, err)
|
|
return
|
|
}
|
|
|
|
// zip file path
|
|
zipFilePath, err := fsSvc.Export()
|
|
if err != nil {
|
|
HandleErrorInternalServerError(c, err)
|
|
return
|
|
}
|
|
|
|
// download
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", zipFilePath))
|
|
c.File(zipFilePath)
|
|
}
|