github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/server/controller/data.go (about)

     1  package controller
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	consts "github.com/easysoft/zendata/internal/pkg/const"
    10  	"github.com/easysoft/zendata/internal/pkg/domain"
    11  	"github.com/easysoft/zendata/internal/pkg/service"
    12  	fileUtils "github.com/easysoft/zendata/pkg/utils/file"
    13  	logUtils "github.com/easysoft/zendata/pkg/utils/log"
    14  	"github.com/easysoft/zendata/pkg/utils/vari"
    15  	"github.com/kataras/iris/v12"
    16  )
    17  
    18  type DataCtrl struct {
    19  	DecodeService *service.DecodeService `inject:""`
    20  	MainService   *service.MainService   `inject:""`
    21  	FileService   *service.FileService   `inject:""`
    22  	BaseCtrl
    23  }
    24  
    25  func (c *DataCtrl) GenerateByFile(ctx iris.Context) {
    26  	c.DealwithParams(ctx)
    27  
    28  	defaultFile := ctx.URLParam("default")
    29  	configFile := ctx.URLParam("config")
    30  
    31  	vari.GlobalVars.ExportChildField = ""
    32  	vari.GlobalVars.OutputFormat = ctx.URLParamDefault("format", "json")
    33  	if vari.GlobalVars.OutputFormat == "text" {
    34  		vari.GlobalVars.OutputFormat = "txt"
    35  	}
    36  
    37  	vari.GlobalVars.ExportFields = nil
    38  	field := ctx.URLParamDefault("field", "")
    39  	if field != "" {
    40  		if strings.Contains(field, "~~") {
    41  			vari.GlobalVars.ExportChildField = field
    42  		} else {
    43  			vari.GlobalVars.ExportFields = strings.Split(field, ",")
    44  		}
    45  	}
    46  
    47  	//root := ctx.URLParam("root")
    48  	//if root != "" {
    49  	//	configUtils.UpdateRootDir(root)
    50  	//
    51  	//	if defaultFile != "" {
    52  	//		defaultFile = filepath.Join(root, defaultFile)
    53  	//	}
    54  	//
    55  	//	if configFile != "" {
    56  	//		configFile = filepath.Join(root, configFile)
    57  	//	}
    58  	//}
    59  
    60  	if defaultFile != "" {
    61  		defaultFile = filepath.Join(vari.WorkDir, defaultFile)
    62  	}
    63  	if configFile != "" {
    64  		configFile = filepath.Join(vari.WorkDir, configFile)
    65  	}
    66  
    67  	vari.GlobalVars.DefData = domain.DefData{}
    68  
    69  	if defaultFile != "" {
    70  		vari.GlobalVars.ConfigFileDir = fileUtils.GetAbsDir(defaultFile)
    71  	} else {
    72  		vari.GlobalVars.ConfigFileDir = fileUtils.GetAbsDir(configFile)
    73  	}
    74  
    75  	defaultContent := c.GetDistFileContent(defaultFile)
    76  	configContent := c.GetDistFileContent(configFile)
    77  
    78  	contents := [][]byte{defaultContent, configContent}
    79  	contents = c.FileService.HandleFileBuffers(contents)
    80  
    81  	c.MainService.GenerateDataByContents(contents)
    82  	c.MainService.PrintOutput()
    83  }
    84  
    85  func (c *DataCtrl) DealwithParams(ctx iris.Context) {
    86  	vari.GlobalVars.RunMode = consts.RunModeServerRequest
    87  	logUtils.OutputHttpWriter = ctx.ResponseWriter()
    88  
    89  	vari.GlobalVars.Total, _ = ctx.URLParamInt("lines")
    90  	vari.GlobalVars.Trim, _ = ctx.URLParamBool("trim")
    91  	vari.GlobalVars.Table = ctx.URLParam("table")
    92  	vari.GlobalVars.Output = ctx.URLParam("outputFile")
    93  	vari.GlobalVars.OutputFormat = ctx.URLParam("format")
    94  
    95  	fields := strings.TrimSpace(ctx.URLParam("fields"))
    96  
    97  	if fields != "" {
    98  		vari.GlobalVars.ExportFields = strings.Split(fields, ",")
    99  	}
   100  
   101  	if vari.GlobalVars.OutputFormat == "" {
   102  		vari.GlobalVars.OutputFormat = consts.FormatJson
   103  	}
   104  
   105  	return
   106  }
   107  
   108  func (c *DataCtrl) GetDistFileContent(file string) (ret []byte) {
   109  	if fileUtils.FileExist(file) {
   110  		ret = fileUtils.ReadFileBuf(file)
   111  	}
   112  
   113  	return
   114  }
   115  
   116  func (c *DataCtrl) GetFormFileContent(ctx iris.Context, name string) (ret []byte) {
   117  	postFile, _, _ := ctx.FormFile(name)
   118  	if postFile != nil {
   119  		defer postFile.Close()
   120  
   121  		buf := bytes.NewBuffer(nil)
   122  		io.Copy(buf, postFile)
   123  
   124  		ret = buf.Bytes()
   125  	}
   126  
   127  	return
   128  }