package action import ( "fmt" "log" "strings" "yunpanWeb/service/config" "app.yhyue.com/moapp/jybase/go-xweb/xweb" "github.com/gogf/gf/v2/util/gconv" // "github.com/studio-b12/gowebdav" "app.yhyue.com/moapp/jybase/encrypt" ) type UploadAction struct { *xweb.Action upload xweb.Mapper `xweb:"/upload/(\\w+)/(.*)"` } func init() { xweb.AddAction(&UploadAction{}) } func (this *UploadAction) Upload(stype, sid string) { log.Println(stype, sid) id := encrypt.DecodeArticleId2ByCheck(sid)[0] table := config.Sysconfig.UrlMap[stype] if id == "" || table == "" { this.Write("未找到附件") return } data := &map[string]interface{}{} for _, v := range config.Sysconfig.TableMap[table] { if data != nil && len(*data) > 0 { continue } data, _ = config.MQFW.FindById(v, id, `{"file_path":1}`) } if data != nil && len(*data) > 0 { file_path := gconv.String((*data)["file_path"]) fileBytes, err := config.Client.Read(file_path) if err != nil { return } // 设置 HTTP 响应头 this.SetHeader("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, extractNameFromPath(file_path))) this.SetHeader("Content-Type", "application/octet-stream") this.SetHeader("Content-Length", fmt.Sprintf("%d", len(fileBytes))) this.WriteBytes(fileBytes) return } this.Write("未找到附件") } // func extractNameFromPath(path string) string { // 使用 strings.LastIndex 寻找最后一个斜杠的索引 index := strings.LastIndex(path, "/") if index == -1 { // 如果没有斜杠,则返回整个路径 return path } // 提取最后一个斜杠后面的名称 if index+1 < len(path) { name := path[index+1:] return name } // 如果索引超出范围,则返回空字符串 return "" }