package FileRelateController import ( "bytes" "dsSzxy/Business/FileRelate/FileRelateDao" "dsSzxy/Utils/CommonUtil" "dsSzxy/Utils/ConfigUtil" "dsSzxy/Utils/MinioUtil" "encoding/base64" "fmt" "github.com/disintegration/imaging" "github.com/gin-gonic/gin" "github.com/minio/minio-go/v6" "io/ioutil" "log" "net/http" "os" "strings" ) // Routers 模块的路由配置 func Routers(r *gin.RouterGroup) { rr := r.Group("/fileRelate") //文件表 rr.POST("/fileUpload", fileUpload) rr.POST("/fileUpdate", fileUpdate) rr.POST("/fileCopy", fileCopy) //树型结构 rr.POST("/addNode", addNode) rr.POST("/updateNode", updateNode) rr.POST("/delNode", delNode) //返回指定树型结构下的子路径 rr.GET("/getFileList", GetFileList) //文件与文件夹的关系维护 rr.POST("/addNodeFile", addNodeFile) rr.POST("/delNodeFile", delNodeFile) //返回指定树型结构下的文件列表 rr.GET("/getSyncTree", GetSyncTree) rr.GET("/getAsyncTree", GetAsyncTree) } // @Summary 文件上传 // @Description 文件上传 // @Tags 文件系统管理 // @Accept application/x-www-form-urlencoded // @Produce json // @Param identity_id formData string true "上传人身份ID" // @Param person_id formData string true "上传人ID" // @Param target_group_id formData string true "目标群组ID" // @Param target_identity_id formData string true "目标人身份ID" // @Param target_person_id formData string true "目标人ID" // @Success 200 {object} Model.Res // @Router /dsSzxy/fileRelate/fileUpload [post] // @X-IntRangeLimit [{"identity_id":"5"},{"person_id":"1,999999999999"},{"target_group_id":"-1,999999999999"},{"target_identity_id":"5"},{"target_person_id":"1,999999999999"},{"system_id":"1,20"}] // @X-TableName ["t_zhxy_file"] func fileUpload(c *gin.Context) { file, err := c.FormFile("file") if err != nil { c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error())) return } //扩展名 ext := CommonUtil.Ext(file.Filename) ext = strings.ToLower(ext) //真实文件名 uuid := CommonUtil.GetUUID() //子目录 p := ConfigUtil.SavePath + uuid[0:2] exist := CommonUtil.Exists(p) if !exist { os.Mkdir(p, os.ModePerm) } trueName := uuid + ext filename := p + "/" + trueName if err := c.SaveUploadedFile(file, filename); err != nil { c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error())) return } //文件大小 content, err := ioutil.ReadFile(filename) if err == nil { fmt.Println("file size is ", len(content)) } size := len(content) // 初使化 minio client对象。 minioClient, err := MinioUtil.GetMinioClient() if err != nil { c.JSON(http.StatusOK, gin.H{"success": false, "info": "minio无法连接!"}) return } // 使用FPutObject上传一个zip文件。 contentType := "application/octet-stream" _, err = minioClient.FPutObject(ConfigUtil.MinioBucket, trueName[0:2]+"/"+trueName, filename, minio.PutObjectOptions{ContentType: contentType}) if err != nil { log.Fatalln(err) } //记录数据库 identityId := CommonUtil.ConvertStringToInt32(c.PostForm("identity_id")) personId := CommonUtil.ConvertStringToInt32(c.PostForm("person_id")) targetGroupId := CommonUtil.ConvertStringToInt32(c.PostForm("target_group_id")) //不是对群组的,填写-1 targetIdentityId := CommonUtil.ConvertStringToInt32(c.PostForm("target_identity_id")) //目标人身份ID targetPersonId := CommonUtil.ConvertStringToInt32(c.PostForm("target_person_id")) //目标人员ID systemId := CommonUtil.ConvertStringToInt32(c.PostForm("system_id")) //系统id FileRelateDao.FileRecord(uuid,filename, identityId, personId, targetGroupId, targetIdentityId, targetPersonId, systemId, int32(size), ext) //返回 s := FileRelateDao.GetFileDownLoadUrl(trueName[0:2]+"/"+trueName, file.Filename) originUrl := ConfigUtil.MinioDownloadPrefix + ConfigUtil.MinioBucket + "/" + trueName[0:2] + "/" + trueName //判断扩展名是不是图片 if ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp" { //读取本地文件 imgData, _ := ioutil.ReadFile(filename) buf := bytes.NewBuffer(imgData) image, err := imaging.Decode(buf) if err != nil { fmt.Println(err) return } //生成缩略图 image = imaging.Resize(image, 0, 150, imaging.Lanczos) thumbFileName := uuid + "_thumb" + ext thumbTrueName := p + "/" + thumbFileName err = imaging.Save(image, thumbTrueName) if err != nil { fmt.Println(err) } //读入生成图片的base64编码 ff, _ := os.Open(thumbTrueName) defer ff.Close() sourceBuffer := make([]byte, 500000) n, _ := ff.Read(sourceBuffer) //base64压缩 sourceString := base64.StdEncoding.EncodeToString(sourceBuffer[:n]) //删除原始文件 os.Remove(filename) //删除缩略图文件 os.Remove(thumbTrueName) c.JSON(200, gin.H{ "success": true, "originUrl": originUrl, "url": s, "thumbContent": sourceString, "size": size, "file_id": uuid, }) } else { //删除原始文件 os.Remove(filename) c.JSON(200, gin.H{ "success": true, "url": s, "size": size, "file_id": uuid, }) } } // @Summary 修改文件名称 // @Description 修改文件名称 // @Tags 文件系统管理 // @Accept application/x-www-form-urlencoded // @Produce json // @Param file_id formData string true "文件ID" // @Param file_name formData string true "文件名称" // @Success 200 {object} Model.Res // @Router /dsSzxy/fileRelate/fileUpdate [post] // @X-EmptyLimit ["file_id","file_name"] // @X-TableName ["t_zhxy_file"] func fileUpdate(c *gin.Context) { fileId := c.PostForm("file_id") fileName := c.PostForm("file_name") FileRelateDao.FileUpdate(fileId, fileName) c.JSON(200, gin.H{ "success": true, "message": `修改成功!`, }) } // @Summary 文件复制,一般用于文件转发 // @Description 文件复制,一般用于文件转发 // @Tags 文件系统管理 // @Accept application/x-www-form-urlencoded // @Produce json // @Param file_id formData string true "文件ID" // @Param source_identity_id formData string true "原文件所有人身份ID" // @Param source_person_id formData string true "原文件所有人ID" // @Param target_identity_id formData string true "接收文件所有人身份ID" // @Param target_person_id formData string true "接收文件所有人ID" // @Param target_group_id formData string true "接收文件群组ID" // @Success 200 {object} Model.Res // @X-EmptyLimit ["file_id"] // @Router /dsSzxy/fileRelate/FileCopy [post] // @X-IntRangeLimit [{"source_identity_id":"5"},{"source_person_id":"1,999999999999"},{"target_group_id":"-1,999999999999"},{"target_identity_id":"5"},{"target_person_id":"1,999999999999"}] // @X-TableName ["t_zhxy_file"] func fileCopy(c *gin.Context) { fileId := c.PostForm("file_id") sourceIdentityId := CommonUtil.ConvertStringToInt32(c.PostForm("source_identity_id")) sourcePersonId := CommonUtil.ConvertStringToInt32(c.PostForm("source_person_id")) targetIdentityId := CommonUtil.ConvertStringToInt32(c.PostForm("target_identity_id")) targetPersonId := CommonUtil.ConvertStringToInt32(c.PostForm("target_person_id")) targetGroupId := CommonUtil.ConvertStringToInt32(c.PostForm("target_group_id")) //文件复制 success, err := FileRelateDao.FileCopy(fileId, sourceIdentityId, sourcePersonId, targetIdentityId, targetPersonId, targetGroupId) if err != nil { c.JSON(200, gin.H{ "success": success, "message": `复制成功!`, }) } else { c.JSON(200, gin.H{ "success": success, "message": err.Error(), }) } } // @Summary 增加云盘结点 // @Description 增加云盘结点 // @Tags 文件系统管理 // @Accept application/x-www-form-urlencoded // @Produce json // @Param identity_id formData string true "创建者身份ID" // @Param person_id formData string true "创建者ID" // @Param parent_id formData string true "父结点ID" // @Param node_name formData string true "结点名称" // @Success 200 {object} Model.Res // @X-EmptyLimit ["parent_id","node_name"] // @Router /dsSzxy/fileRelate/addNode [post] // @X-IntRangeLimit [{"identity_id":"5"},{"person_id":"1,999999999999"}] // @X-TableName ["t_zhxy_clouddisk_tree"] func addNode(c *gin.Context) { identityId := CommonUtil.ConvertStringToInt32(c.PostForm("identity_id")) personId := CommonUtil.ConvertStringToInt32(c.PostForm("person_id")) parentId := c.PostForm("parent_id") //根传入-1 nodeName := c.PostForm("node_name") success, err := FileRelateDao.AddNode(identityId, personId, parentId, nodeName) if err != nil { c.JSON(200, gin.H{ "success": success, "message": `增加成功!`, }) } else { c.JSON(200, gin.H{ "success": success, "message": err.Error(), }) } } // @Summary 修改云盘结点 // @Description 修改云盘结点 // @Tags 文件系统管理 // @Accept application/x-www-form-urlencoded // @Produce json // @Param node_id formData string true "结点ID" // @Param node_name formData string true "结点名称" // @Success 200 {object} Model.Res // @X-EmptyLimit ["node_id","node_name"] // @Router /dsSzxy/fileRelate/updateNode [post] // @X-TableName ["t_zhxy_clouddisk_tree"] func updateNode(c *gin.Context) { nodeName := c.PostForm("nodeName") nodeId := c.PostForm("node_id") success, err := FileRelateDao.UpdateNode(nodeId, nodeName) if err != nil { c.JSON(200, gin.H{ "success": success, "message": `修改成功!`, }) } else { c.JSON(200, gin.H{ "success": success, "message": err.Error(), }) } } // @Summary 删除云盘结点 // @Description 删除云盘结点 // @Tags 文件系统管理 // @Accept application/x-www-form-urlencoded // @Produce json // @Param node_id formData string true "结点ID" // @Success 200 {object} Model.Res // @X-EmptyLimit ["node_id"] // @Router /dsSzxy/fileRelate/delNode [post] // @X-TableName ["t_zhxy_clouddisk_tree"] func delNode(c *gin.Context) { nodeId := c.PostForm("node_id") success, err := FileRelateDao.DelNode(nodeId) if err != nil { c.JSON(200, gin.H{ "success": success, "message": `删除成功!`, }) } else { c.JSON(200, gin.H{ "success": success, "message": err.Error(), }) } } // @Summary 增加云盘结点文件 // @Description 增加云盘结点文件 // @Tags 文件系统管理 // @Accept application/x-www-form-urlencoded // @Produce json // @Param node_id formData string true "结点ID" // @Param file_id formData string true "文件ID" // @Success 200 {object} Model.Res // @X-EmptyLimit ["node_id"] // @Router /dsSzxy/fileRelate/addNodeFile [post] // @X-TableName ["t_zhxy_clouddisk_tree"] func addNodeFile(c *gin.Context) { nodeId := c.PostForm("node_id") fileId := c.PostForm("file_id") success, err := FileRelateDao.AddNodeFile(nodeId, fileId) if err != nil { c.JSON(200, gin.H{ "success": success, "message": `增加成功!`, }) } else { c.JSON(200, gin.H{ "success": success, "message": err.Error(), }) } } // @Summary 删除云盘结点文件 // @Description 删除云盘结点文件 // @Tags 文件系统管理 // @Accept application/x-www-form-urlencoded // @Produce json // @Param node_id formData string true "结点ID" // @Param file_id formData string true "文件ID" // @Success 200 {object} Model.Res // @X-EmptyLimit ["node_id"] // @Router /dsSzxy/fileRelate/delNodeFile [post] // @X-TableName ["t_zhxy_clouddisk_tree"] func delNodeFile(c *gin.Context) { nodeId := c.PostForm("node_id") fileId := c.PostForm("file_id") success, err := FileRelateDao.DelNodeFile(nodeId, fileId) if err != nil { c.JSON(200, gin.H{ "success": success, "message": `删除成功!`, }) } else { c.JSON(200, gin.H{ "success": success, "message": err.Error(), }) } } // @Summary 获取指定结点下的文件列表 // @Description 获取指定结点下的文件列表 // @Tags 文件系统管理 // @Accept application/x-www-form-urlencoded // @Produce json // @Param identity_id formData string true "上传人身份ID" // @Param person_id formData string true "上传人ID" // @Param node_id formData string true "结点ID" // @Success 200 {object} Model.Res // @X-IntRangeLimit [{"identity_id":"5"},{"person_id":"1,999999999999"}] // @X-EmptyLimit ["node_id"] // @Router /dsSzxy/fileRelate/GetFileList [get] func GetFileList(c *gin.Context) { identityId := CommonUtil.ConvertStringToInt32(c.PostForm("identity_id")) personId := CommonUtil.ConvertStringToInt32(c.PostForm("person_id")) nodeId := c.PostForm("node_id") limit := CommonUtil.ConvertStringToInt32(c.PostForm("limit")) offset := CommonUtil.ConvertStringToInt32(c.PostForm("offset")) success, list := FileRelateDao.GetFileList(identityId, personId, nodeId, int(limit), int(offset)) c.JSON(200, gin.H{ "success": success, "data": list, }) } // @Summary 获取指定树型结构下的子目录(同步) // @Description 获取指定树型结构下的子目录(同步) // @Tags 文件系统管理 // @Accept application/x-www-form-urlencoded // @Produce json // @Param identity_id formData string true "上传人身份ID" // @Param person_id formData string true "上传人ID" // @Success 200 {object} Model.Res // @X-IntRangeLimit [{"identity_id":"5"},{"person_id":"1,999999999999"}] // @Router /dsSzxy/fileRelate/GetSyncTree [get] func GetSyncTree(c *gin.Context) { identityId := CommonUtil.ConvertStringToInt32(c.PostForm("identity_id")) personId := CommonUtil.ConvertStringToInt32(c.PostForm("person_id")) success, list := FileRelateDao.GetSyncTree(identityId, personId) c.JSON(200, gin.H{ "success": success, "data": list, }) } // @Summary 获取指定树型结构下的子目录(异步) // @Description 获取指定树型结构下的子目录(异步) // @Tags 文件系统管理 // @Accept application/x-www-form-urlencoded // @Produce json // @Param identity_id formData string true "上传人身份ID" // @Param person_id formData string true "上传人ID" // @Param node_id formData string true "结点ID" // @Success 200 {object} Model.Res // @X-IntRangeLimit [{"identity_id":"5"},{"person_id":"1,999999999999"}] // @X-EmptyLimit ["node_id"] // @Router /dsSzxy/fileRelate/GetAsyncTree [get] func GetAsyncTree(c *gin.Context) { identityId := CommonUtil.ConvertStringToInt32(c.PostForm("identity_id")) personId := CommonUtil.ConvertStringToInt32(c.PostForm("person_id")) nodeId := c.PostForm("node_id") success, list := FileRelateDao.GetAsyncTree(identityId, personId, nodeId) c.JSON(200, gin.H{ "success": success, "data": list, }) }