123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- package admin
- import (
- "context"
- "fmt"
- "github.com/druidcaesa/gotool"
- "github.com/qiniu/go-sdk/v7/auth/qbox"
- "github.com/qiniu/go-sdk/v7/storage"
- "image"
- "image/jpeg"
- "image/png"
- "io"
- "log"
- "os"
- "strconv"
- "strings"
- "time"
- "ulink-admin/config"
- "ulink-admin/frame"
- "ulink-admin/modules/system/models/model"
- "ulink-admin/modules/system/models/req"
- "ulink-admin/modules/system/models/response"
- "ulink-admin/modules/system/service"
- "ulink-admin/pkg/excels"
- "ulink-admin/pkg/file"
- "ulink-admin/pkg/jwt/admin"
- "ulink-admin/pkg/page"
- "ulink-admin/pkg/tool"
- )
- type SysAttachmentApi struct {
- SysAttachmentService *service.SysAttachmentService `inject:""`
- }
- // List 查询附件分页数据
- func (a SysAttachmentApi) List(c *frame.Context) {
- query := &req.SysAttachmentQuery{}
- c.ValidteError(c.ShouldBind(query), query)
- list, i := a.SysAttachmentService.FindList(query)
- c.Ok(page.Page{
- List: list,
- Total: i,
- Size: query.PageSize,
- })
- }
- // List 查询附件所有数据
- func (a SysAttachmentApi) ListAll(c *frame.Context) {
- list := a.SysAttachmentService.FindAll()
- c.Ok(list)
- }
- // Get 根据附件Id获取详细信息
- func (a SysAttachmentApi) Get(c *frame.Context) {
- param := c.Param("id")
- id, _ := strconv.ParseInt(param, 10, 64)
- c.Ok(a.SysAttachmentService.GetSysAttachmentById(id))
- }
- // DownLoad 下载文件
- func (a SysAttachmentApi) DownLoad(c *frame.Context) {
- param := c.Param("id")
- isZIp := c.Query("isZip")
- id, _ := strconv.ParseInt(param, 10, 64)
- sysAttachment := a.SysAttachmentService.GetSysAttachmentById(id)
- filePath := sysAttachment.Path
- if gotool.StrUtils.HasNotEmpty(isZIp) {
- if gotool.StrUtils.HasNotEmpty(sysAttachment.ZipPath) {
- filePath = sysAttachment.ZipPath
- }
- }
- //打开文件
- file, err := os.Open(filePath)
- if err != nil {
- fmt.Println("打开文件错误", err)
- }
- defer file.Close()
- c.File(filePath)
- c.Header("Content-Type", "application/octet-stream")
- c.Header("Content-Disposition", "attachment; filename="+sysAttachment.Label+"."+sysAttachment.Ext)
- c.Header("Content-Transfer-Encoding", "binary")
- c.Header("Cache-Control", "no-cache")
- }
- // Add 新增附件
- func (a SysAttachmentApi) Add(c *frame.Context) {
- sysAttachment := &model.SysAttachment{}
- c.ValidteError(c.ShouldBind(sysAttachment), sysAttachment)
- sysAttachment.CreateTime = time.Now()
- sysAttachment.CreateBy = admin.GetUserInfo(c).UserName
- a.SysAttachmentService.Insert(sysAttachment)
- }
- // Upload 附件上传
- func (a SysAttachmentApi) Upload(c *frame.Context) {
- path := config.GetFilePath()
- file, header, err := c.Request.FormFile("file")
- //fileType := c.Request.FormValue("fileType")
- name := c.Request.FormValue("fileName")
- ext := ""
- if gotool.StrUtils.HasNotEmpty(name) {
- ary := strings.Split(name, ".")
- ext = ary[1]
- }
- ary := strings.Split(header.Filename, ".")
- if len(ary) > 1 {
- ext = ary[1]
- }
- fileName := gotool.IdUtils.IdUUIDToRan(true) + "." + ext
- filePath := path.Path + fileName
- compressPath := path.Path + "zip/" + fileName
- fileAppend, err := gotool.FileUtils.OpenFileAppend(filePath)
- defer fileAppend.Close()
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- c.Error()
- return
- }
- _, err = io.Copy(fileAppend, file)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- c.Error()
- return
- }
- zipPath := ""
- if strings.Contains(header.Header.Get("Content-Type"), "image") {
- zipPath = a.Compress(filePath, compressPath)
- }
- sysAttachment := model.SysAttachment{}
- sysAttachment.Label = strings.Split(header.Filename, ".")[0]
- sysAttachment.Type = header.Header.Get("Content-Type")
- sysAttachment.Path = filePath
- sysAttachment.Ext = ext
- sysAttachment.CreateTime = time.Now()
- sysAttachment.CreateBy = admin.GetUserInfo(c).UserName
- sysAttachment.UpdateTime = time.Now()
- sysAttachment.ZipPath = zipPath
- sysAttachment.UpdateBy = admin.GetUserInfo(c).UserName
- a.SysAttachmentService.Insert(&sysAttachment)
- c.Ok(sysAttachment)
- }
- func (a SysAttachmentApi) Compress(imagePath string, compressPath string) string {
- //需要压缩
- imgfile, err := os.Open(imagePath)
- if err != nil {
- log.Println("os.Open::", err)
- return ""
- }
- defer imgfile.Close()
- jpgimg, layout, err := image.Decode(imgfile)
- if err != nil {
- log.Println("jpeg.Decode::", err)
- return ""
- }
- //保存到新文件中
- newfile, err := os.Create(compressPath)
- if err != nil {
- log.Println("Create::", err)
- return ""
- }
- defer newfile.Close()
- switch layout {
- case "png":
- err = png.Encode(newfile, jpgimg)
- case "jpeg", "jpg":
- err = jpeg.Encode(newfile, jpgimg, &jpeg.Options{Quality: 30})
- default:
- return ""
- }
- if err != nil {
- log.Println("Encode::", err)
- return ""
- }
- return compressPath
- }
- // 获取大小的借口
- type Sizer interface {
- Size() int64
- }
- // 文件上传接口数据
- //
- // @Summary 文件上传接口数据
- // @Description 文件上传接口数据
- // @Tags 文件上传相关接口
- // @Accept application/json
- // @Produce application/json
- // @Param Authorization header string false "Bearer 令牌"
- // @Security ApiKeyAuth
- // @Success 200 {object} resp.Response{data=string,msg=string} "文件上传结果"
- // @Router /sysattachment/qiniuUpload [post]
- func (a SysAttachmentApi) QiniuUpload(c *frame.Context) {
- file, _, err := c.Request.FormFile("file")
- fileSize := file.(Sizer).Size()
- var AccessKey = "Rt7_WT03Go8_BwO--ExFlnmsTBk9QUZX_NNnoTMp"
- var SerectKey = "NC1_hBQumLFEUCLICzPGPR8xnbn03-Gt5fDV2vlv"
- var Bucket = "longping"
- putPlicy := storage.PutPolicy{
- Scope: Bucket,
- }
- mac := qbox.NewMac(AccessKey, SerectKey)
- upToken := putPlicy.UploadToken(mac)
- cfg := storage.Config{
- Zone: tool.Zone("longping"),
- UseCdnDomains: false,
- UseHTTPS: true,
- }
- putExtra := storage.PutExtra{}
- formUploader := storage.NewFormUploader(&cfg)
- ret := storage.PutRet{}
- err = formUploader.PutWithoutKey(context.Background(), &ret, upToken, file, fileSize, &putExtra)
- if err != nil {
- c.Error("文件上传错误")
- return
- }
- url := "http://rsbl220vw.hd-bkt.clouddn.com/" + ret.Key
- data := response.QiniuResponse{}
- data.Url = url
- data.Label = ret.Key
- c.Ok(data)
- }
- // Edit 修改附件数据接口
- func (a SysAttachmentApi) Edit(c *frame.Context) {
- sysAttachment := &model.SysAttachment{}
- c.ValidteError(c.ShouldBind(sysAttachment), sysAttachment)
- sysAttachment.UpdateTime = time.Now()
- sysAttachment.UpdateBy = admin.GetUserInfo(c).UserName
- a.SysAttachmentService.Update(sysAttachment)
- c.Ok(sysAttachment)
- }
- // Delete 删除附件数据
- func (a SysAttachmentApi) Delete(c *frame.Context) {
- //获取sysAttachmentId
- param := c.Param("id")
- list := make([]int64, 0)
- if gotool.StrUtils.HasNotEmpty(param) {
- strs := strings.Split(param, ",")
- for _, str := range strs {
- id, _ := strconv.ParseInt(str, 10, 64)
- list = append(list, id)
- }
- }
- //判断是否可以删除
- a.SysAttachmentService.Delete(list)
- }
- // Export 导出excel
- func (a SysAttachmentApi) Export(c *frame.Context) {
- query := &req.SysAttachmentQuery{}
- c.ValidteError(c.ShouldBind(query), query)
- list, _ := a.SysAttachmentService.FindList(query)
- excelList := make([]interface{}, 0)
- for _, sysAttachment := range *list {
- excelList = append(excelList, sysAttachment)
- }
- _, files := excels.ExportExcel(excelList, "附件数据表")
- file.DownloadExcel(c, files)
- }
|