12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073 |
- package controllers
- import (
- "bufio"
- "encoding/base64"
- "eta_gn/eta_api/models"
- "eta_gn/eta_api/services"
- "eta_gn/eta_api/utils"
- "fmt"
- "github.com/h2non/filetype"
- "github.com/kgiannakakis/mp3duration/src/mp3duration"
- "io"
- "io/ioutil"
- "net/http"
- "os"
- "path"
- "regexp"
- "strconv"
- "strings"
- "time"
- rdHttp "github.com/rdlucklib/rdluck_tools/http"
- )
- type ResourceController struct {
-
- BaseCommonController
- }
- type ResourceAuthController struct {
- BaseAuthController
- }
- func (this *ResourceController) Upload() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- f, h, err := this.GetFile("file")
- if err != nil {
- br.Msg = "获取资源信息失败"
- br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
- return
- }
- fileData, e := ioutil.ReadAll(f)
- if e != nil {
- br.Msg = "上传失败"
- br.ErrMsg = "读取文件失败, Err: " + e.Error()
- return
- }
- pass := filetype.IsImage(fileData)
- if !pass {
- br.Msg = "文件格式有误"
- br.ErrMsg = "文件格式有误"
- return
- }
- uploadFileName := h.Filename
- ext := path.Ext(h.Filename)
- dateDir := time.Now().Format("20060102")
- uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
- err = os.MkdirAll(uploadDir, utils.DIR_MOD)
- if err != nil {
- br.Msg = "存储目录创建失败"
- br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
- return
- }
- randStr := utils.GetRandStringNoSpecialChar(28)
- fileName := randStr + ext
- fpath := uploadDir + "/" + fileName
- defer f.Close()
- err = this.SaveToFile("file", fpath)
- if err != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败,Err:" + err.Error()
- return
- }
- resourceUrl := ``
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ossClient := services.NewOssClient()
- if ossClient == nil {
- br.Msg = "上传失败"
- br.ErrMsg = "初始化OSS服务失败"
- return
- }
- resourceUrl, err = ossClient.UploadFile(fileName, fpath, "")
- if err != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败,Err:" + err.Error()
- return
- }
- defer func() {
- os.Remove(fpath)
- }()
- item := new(models.Resource)
- item.ResourceUrl = resourceUrl
- item.ResourceType = 1
- item.CreateTime = time.Now()
- newId, err := models.AddResource(item)
- if err != nil {
- br.Msg = "资源上传失败"
- br.ErrMsg = "资源上传失败,Err:" + err.Error()
- return
- }
- resp := models.ResourceResp{
- Id: newId,
- ResourceUrl: resourceUrl,
- ResourceName: uploadFileName,
- }
- br.Msg = "上传成功"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- return
- }
- func (this *ResourceController) VideoUpload() {
- utils.FileLog.Info("start VideoUpload ")
- utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- f, h, err := this.GetFile("file")
- if err != nil {
- br.Msg = "获取资源信息失败"
- br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
- return
- }
- fileData, e := ioutil.ReadAll(f)
- if e != nil {
- br.Msg = "上传失败"
- br.ErrMsg = "读取文件失败, Err: " + e.Error()
- return
- }
- pass := filetype.IsVideo(fileData)
- if !pass {
- br.Msg = "文件格式有误"
- br.ErrMsg = "文件格式有误"
- return
- }
- ext := path.Ext(h.Filename)
- dateDir := time.Now().Format("20060102")
- uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
-
- fmt.Println(uploadDir)
- err = os.MkdirAll(uploadDir, utils.DIR_MOD)
- if err != nil {
- br.Msg = "存储目录创建失败"
- br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
- return
- }
- randStr := utils.GetRandStringNoSpecialChar(28)
- fileName := randStr + ext
- fpath := uploadDir + "/" + fileName
- defer f.Close()
- utils.FileLog.Info("start save")
- utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
- utils.FileLog.Info("fpath %s:", fpath)
- fileHandle, err := os.OpenFile(fpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModeAppend)
- if err != nil {
- br.Msg = "创建文件失败"
- br.ErrMsg = "创建文件失败,Err:" + err.Error()
- return
- }
- defer fileHandle.Close()
-
- for {
- buffer := make([]byte, 4096)
- _, err := f.Read(buffer)
-
- if err != nil && err != io.EOF {
- br.Msg = "上传失败"
- br.ErrMsg = "读取文件内容失败,Err:" + err.Error()
- return
- }
-
- if err == io.EOF {
- break
- }
-
-
- buf := bufio.NewWriter(fileHandle)
-
-
- buf.WriteString(string(buffer))
-
- err = buf.Flush()
- if err != nil {
- br.Msg = "上传失败"
- br.ErrMsg = "写入文件内容失败,Err:" + err.Error()
- return
- }
- }
- utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
- utils.FileLog.Info("end save")
-
-
-
-
-
-
-
- utils.FileLog.Info("start update oss ")
- utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
-
-
- defer func() {
- _ = os.Remove(fpath)
- }()
-
- resourceUrl := ``
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ossClient := services.NewOssClient()
- if ossClient == nil {
- br.Msg = "上传失败"
- br.ErrMsg = "初始化OSS服务失败"
- return
- }
- resourceUrl, err = ossClient.UploadFile(fileName, fpath, "")
- if err != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败,Err:" + err.Error()
- return
- }
- utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
- utils.FileLog.Info("end update oss ")
- item := new(models.Resource)
- item.ResourceUrl = resourceUrl
- item.ResourceType = 3
- item.CreateTime = time.Now()
- newId, err := models.AddResource(item)
- if err != nil {
- br.Msg = "资源上传失败"
- br.ErrMsg = "资源上传失败,Err:" + err.Error()
- return
- }
- resp := new(models.ResourceResp)
- resp.Id = newId
- resp.ResourceUrl = resourceUrl
- utils.FileLog.Info("start GetMP4Duration ")
- utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
- duration, err := services.GetMP4Duration(f)
- if err != nil {
- panic(err)
- }
- utils.FileLog.Info("end GetMP4Duration ")
- utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
- resp.PlaySeconds = duration
- br.Msg = "上传成功"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- utils.FileLog.Info("end VideoUpload ")
- utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
- return
- }
- func (this *ResourceController) VoiceUpload() {
- utils.FileLog.Info("start VoiceUpload ")
- utils.FileLog.Info(fmt.Sprintf("%s:", time.Now().Format(utils.FormatDateTime)))
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- f, h, err := this.GetFile("file")
- if err != nil {
- br.Msg = "获取资源信息失败"
- br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
- return
- }
- uploadFileName := h.Filename
- fileData, e := ioutil.ReadAll(f)
- if e != nil {
- br.Msg = "上传失败"
- br.ErrMsg = "读取文件失败, Err: " + e.Error()
- return
- }
- pass := filetype.IsAudio(fileData)
- if !pass {
- br.Msg = "文件格式有误"
- br.ErrMsg = "文件格式有误"
- return
- }
- ext := path.Ext(h.Filename)
- dateDir := time.Now().Format("20060102")
- uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
-
- fmt.Println(uploadDir)
- err = os.MkdirAll(uploadDir, utils.DIR_MOD)
- if err != nil {
- br.Msg = "存储目录创建失败"
- br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
- return
- }
- randStr := utils.GetRandStringNoSpecialChar(28)
- fileName := randStr + ext
- fpath := uploadDir + "/" + fileName
- defer f.Close()
- utils.FileLog.Info("start save")
- utils.FileLog.Info(fmt.Sprintf("%s:", time.Now().Format(utils.FormatDateTime)))
- utils.FileLog.Info(fmt.Sprintf("fpath %s:", fpath))
- fileHandle, err := os.OpenFile(fpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0755)
- if err != nil {
- br.Msg = "创建文件失败"
- br.ErrMsg = "创建文件失败,Err:" + err.Error()
- return
- }
- defer fileHandle.Close()
- localFileBuffer := make([]byte, 0)
-
- for {
- buffer := make([]byte, 4096)
- maxLen, err := f.Read(buffer)
-
- if err != nil && err != io.EOF {
- br.Msg = "上传失败"
- br.ErrMsg = "读取文件内容失败,Err:" + err.Error()
- return
- }
-
- if err == io.EOF {
- break
- }
-
-
- buf := bufio.NewWriter(fileHandle)
-
-
- buf.WriteString(string(buffer))
- localFileBuffer = append(localFileBuffer, buffer[0:maxLen]...)
-
- err = buf.Flush()
- if err != nil {
- br.Msg = "上传失败"
- br.ErrMsg = "写入文件内容失败,Err:" + err.Error()
- return
- }
- }
- utils.FileLog.Info(fmt.Sprintf("%s:", time.Now().Format(utils.FormatDateTime)))
- utils.FileLog.Info("end save")
- defer func() {
- if utils.FileIsExist(fpath) {
- os.Remove(fpath)
- }
- }()
- var playSeconds float64
- if ext == ".m4a" {
- utils.FileLog.Info("start calculate m4a duration ")
- tmpPlaySeconds, err := services.GetMP4Duration(f)
- if err != nil {
- br.Msg = "音频资源时长计算失败"
- br.ErrMsg = "音频资源时长计算失败,Err:" + err.Error()
- return
- }
- playSeconds = float64(tmpPlaySeconds)
- utils.FileLog.Info("end calculate m4a duration ")
- } else {
- utils.FileLog.Info("start calculate mp3duration ")
- playSeconds, err = mp3duration.Calculate(fpath)
- if playSeconds <= 0 {
- playSeconds, err = utils.GetVideoPlaySeconds(fpath)
- if err != nil {
- br.Msg = "音频资源时长计算失败"
- br.ErrMsg = "音频资源时长计算失败,Err:" + err.Error()
- return
- }
- }
- utils.FileLog.Info("end calculate mp3duration ")
- }
- utils.FileLog.Info("start update oss ")
- utils.FileLog.Info(fmt.Sprintf("%s:", time.Now().Format(utils.FormatDateTime)))
- defer func() {
- _ = os.Remove(fpath)
- }()
-
-
-
- resourceUrl := ``
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ossClient := services.NewOssClient()
- if ossClient == nil {
- br.Msg = "上传失败"
- br.ErrMsg = "初始化OSS服务失败"
- return
- }
- resourceUrl, err = ossClient.UploadFile(fileName, fpath, "")
- if err != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败,Err:" + err.Error()
- return
- }
- utils.FileLog.Info(fmt.Sprintf("%s:", time.Now().Format(utils.FormatDateTime)))
- utils.FileLog.Info("end update oss ")
- item := new(models.Resource)
- item.ResourceUrl = resourceUrl
- item.ResourceType = 3
- item.CreateTime = time.Now()
- newId, err := models.AddResource(item)
- if err != nil {
- br.Msg = "音频资源上传失败"
- br.ErrMsg = "音频资源上传失败,Err:" + err.Error()
- return
- }
- resp := models.ResourceResp{
- Id: newId,
- ResourceUrl: resourceUrl,
- ResourceName: uploadFileName,
- PlaySeconds: uint32(playSeconds),
- }
- br.Msg = "上传成功"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- utils.FileLog.Info("end VoiceUpload ")
- utils.FileLog.Info(fmt.Sprintf("%s:", time.Now().Format(utils.FormatDateTime)))
- return
- }
- func (this *ResourceController) UploadImageBase64() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var err error
- ext := ".png"
- uploadDir := "./static"
- randStr := utils.GetRandStringNoSpecialChar(28)
- fileName := randStr + ext
- fpath := uploadDir + "/" + fileName
- image := this.Ctx.Request.FormValue("Image")
- if image == "" {
-
- f, h, e := this.GetFile("Image")
- if e != nil {
- br.Msg = "参数错误"
- br.ErrMsg = "获取资源信息失败,Err:" + e.Error()
- return
- }
- fileData, e := ioutil.ReadAll(f)
- if e != nil {
- br.Msg = "上传失败"
- br.ErrMsg = "读取文件失败, Err: " + e.Error()
- return
- }
- pass := filetype.IsImage(fileData)
- if !pass {
- br.Msg = "文件格式有误"
- br.ErrMsg = "文件格式有误"
- return
- }
- ext = path.Ext(h.Filename)
- dateDir := time.Now().Format("20060102")
- uploadDir = utils.STATIC_DIR + "hongze/" + dateDir
- err = os.MkdirAll(uploadDir, utils.DIR_MOD)
- if err != nil {
- br.Msg = "存储目录创建失败"
- br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
- return
- }
- randStr = utils.GetRandStringNoSpecialChar(28)
- fileName = randStr + ext
- fpath = uploadDir + "/" + fileName
- defer f.Close()
- err = this.SaveToFile("Image", fpath)
- if err != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败,Err:" + err.Error()
- return
- }
- } else {
- b, _ := regexp.MatchString(`^data:\s*image\/(\w+);base64,`, image)
- if !b {
- br.Msg = "图片格式不正确"
- br.ErrMsg = "图片格式不正确"
- return
- }
- re, _ := regexp.Compile(`^data:\s*image\/(\w+);base64,`)
- base64Str := re.ReplaceAllString(image, "")
- base64Str = strings.Replace(base64Str, " ", "", -1)
- err = utils.SaveBase64ToFile(base64Str, fpath)
- if err != nil {
- br.Msg = "图片保存失败"
- br.ErrMsg = "图片保存失败,Err:" + err.Error()
- return
- }
- }
- fmt.Println("end")
- defer os.Remove(fpath)
- hzUploadDir := utils.RESOURCE_DIR + "images/"
- savePath := hzUploadDir + time.Now().Format("200601/20060102/")
- savePath += fileName
-
- resourceUrl := ``
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ossClient := services.NewOssClient()
- if ossClient == nil {
- br.Msg = "上传失败"
- br.ErrMsg = "初始化OSS服务失败"
- return
- }
- resourceUrl, err = ossClient.UploadFile(fileName, fpath, savePath)
- if err != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败,Err:" + err.Error()
- return
- }
- utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
- utils.FileLog.Info("end update oss ")
- item := new(models.Resource)
- item.ResourceUrl = resourceUrl
- item.ResourceType = 1
- item.CreateTime = time.Now()
- newId, err := models.AddResource(item)
- if err != nil {
- br.Msg = "资源上传失败"
- br.ErrMsg = "资源上传失败,Err:" + err.Error()
- return
- }
- resp := new(models.ResourceResp)
- resp.Id = newId
- resp.ResourceUrl = resourceUrl
- br.Msg = "上传成功"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- utils.FileLog.Info("end VideoUpload ")
- utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
- }
- func IsFileExist(filename string) bool {
- _, err := os.Stat(filename)
- if os.IsNotExist(err) {
- return false
- }
- return true
- }
- func (this *ResourceController) UploadV2() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- businessType := this.Ctx.Request.Form.Get("business_type")
-
- fmt.Println("businessType:", businessType)
- fmt.Println(this.Ctx.Request.Form)
- fmt.Println("===========")
- br.Data = businessType
- f, h, err := this.GetFile("file")
- if err != nil {
- br.Msg = "获取资源信息失败"
- br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
- return
- }
- fileData, e := ioutil.ReadAll(f)
- if e != nil {
- br.Msg = "上传失败"
- br.ErrMsg = "读取文件失败, Err: " + e.Error()
- return
- }
- pass := filetype.IsImage(fileData)
- if !pass {
- br.Msg = "文件格式有误"
- br.ErrMsg = "文件格式有误"
- return
- }
- ext := path.Ext(h.Filename)
- dateDir := time.Now().Format("20060102")
- uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
- err = os.MkdirAll(uploadDir, utils.DIR_MOD)
- if err != nil {
- br.Msg = "存储目录创建失败"
- br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
- return
- }
- randStr := utils.GetRandStringNoSpecialChar(28)
- fileName := randStr + ext
- fpath := uploadDir + "/" + fileName
- defer f.Close()
- err = this.SaveToFile("file", fpath)
- if err != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败,Err:" + err.Error()
- return
- }
- resourceUrl := ``
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ossClient := services.NewOssClient()
- if ossClient == nil {
- br.Msg = "上传失败"
- br.ErrMsg = "初始化OSS服务失败"
- return
- }
- resourceUrl, err = ossClient.UploadFile(fileName, fpath, "")
- if err != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败,Err:" + err.Error()
- return
- }
- defer func() {
- os.Remove(fpath)
- }()
- item := new(models.Resource)
- item.ResourceUrl = resourceUrl
- item.ResourceType = 1
- item.CreateTime = time.Now()
- newId, err := models.AddResource(item)
- if err != nil {
- br.Msg = "资源上传失败"
- br.ErrMsg = "资源上传失败,Err:" + err.Error()
- return
- }
- resp := new(models.ResourceResp)
- resp.Id = newId
- resp.ResourceUrl = resourceUrl
- br.Msg = "上传成功"
- br.Ret = 200
- br.Success = true
-
- return
- }
- func (this *ResourceController) OssSTSToken() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- ossClient := services.NewOssClient()
- if ossClient == nil {
- br.Msg = "上传失败"
- br.ErrMsg = "初始化OSS服务失败"
- return
- }
- resp, e := ossClient.GetUploadToken()
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取OSS上传Token失败, Err: " + e.Error()
- return
- }
- br.Data = resp
- br.Msg = "获取成功"
- br.Ret = 200
- br.Success = true
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- func (this *ResourceAuthController) FileDownload() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
-
-
-
-
-
-
- fileEncode := this.GetString("FileUrl")
- fileEncode = strings.TrimSpace(fileEncode)
- if fileEncode == "" {
- br.Msg = "参数有误"
- return
- }
- fileByte, e := base64.StdEncoding.DecodeString(fileEncode)
- if e != nil {
- br.Msg = "下载失败"
- br.ErrMsg = "文件地址解析失败, Err: " + e.Error()
- return
- }
- fileUrl := string(fileByte)
- fileArr := strings.Split(fileUrl, "/")
- if len(fileArr) == 0 {
- br.Msg = "文件地址有误"
- return
- }
- fileName := fileArr[len(fileArr)-1]
-
-
- down, e := http.Get(fileUrl)
- if e != nil {
- br.Msg = "下载失败"
- br.ErrMsg = "文件下载失败, http get: " + e.Error()
- return
- }
- defer down.Body.Close()
- if down.StatusCode != http.StatusOK {
- br.Msg = "下载失败"
- br.ErrMsg = fmt.Sprintf("文件下载失败, http status: %d", down.StatusCode)
- return
- }
-
- localFilePath := fmt.Sprintf("%s%s", utils.GetRandStringNoSpecialChar(6), fileName)
- localFile, e := os.Create(localFilePath)
- if e != nil {
- br.Msg = "下载失败"
- br.ErrMsg = "生成本地文件失败, Err: " + e.Error()
- return
- }
- defer func() {
- if e = localFile.Close(); e != nil {
- fmt.Println("local file close err: ", e.Error())
- }
- if e = os.Remove(localFilePath); e != nil {
- fmt.Println("local file remove err: ", e.Error())
- }
- }()
-
-
- _, e = io.Copy(localFile, down.Body)
- if e != nil {
- br.Msg = "下载失败"
- br.ErrMsg = "复制文件资源失败, Err: " + e.Error()
- return
- }
-
-
-
- br.Ret = 200
- br.Msg = "下载成功"
- br.Success = true
- this.Ctx.Output.Download(localFilePath, fileName)
- }
- func (this *ResourceController) FileReload() {
- fileUrl := `https://hzstatic.hzinsights.com/static/images/202202/20220208/Qr3dulOoVTJgyl8skH1jL1kv36RX.png`
- body, err := rdHttp.Get(fileUrl)
- if err != nil {
- fmt.Println("err: ", err)
- this.Ctx.WriteString("err: " + err.Error() + "")
- return
- }
-
- this.Ctx.ResponseWriter.Header().Set("Content-Type", "image/png")
- this.Ctx.ResponseWriter.Header().Set("Content-Length", strconv.Itoa(len(body)))
-
- _, err = this.Ctx.ResponseWriter.Write(body)
- if err != nil {
- http.Error(this.Ctx.ResponseWriter, "Failed to write response", http.StatusInternalServerError)
- return
- }
- }
|