PMail/server/controllers/attachments.go
Jinnrry 054336fe9e
v2.6.1 (#169)
1、新增垃圾邮件过滤插件
2、使用使用github.com/dlclark/regexp2替换go原生的正则包
3、修复空数据导致的邮件插入失败
2024-07-20 10:39:17 +08:00

51 lines
1.4 KiB
Go

package controllers
import (
"fmt"
"github.com/Jinnrry/pmail/dto/response"
"github.com/Jinnrry/pmail/services/attachments"
"github.com/Jinnrry/pmail/utils/context"
"github.com/spf13/cast"
"net/http"
"strings"
)
func GetAttachments(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
urlInfos := strings.Split(req.RequestURI, "/")
if len(urlInfos) != 4 {
response.NewErrorResponse(response.ParamsError, "", "").FPrint(w)
return
}
emailId := cast.ToInt(urlInfos[2])
cid := urlInfos[3]
contentType, content := attachments.GetAttachments(ctx, emailId, cid)
if len(content) == 0 {
response.NewErrorResponse(response.ParamsError, "", "").FPrint(w)
return
}
w.Header().Set("Content-Type", contentType)
w.Write(content)
}
func Download(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
urlInfos := strings.Split(req.RequestURI, "/")
if len(urlInfos) != 5 {
response.NewErrorResponse(response.ParamsError, "", "").FPrint(w)
return
}
emailId := cast.ToInt(urlInfos[3])
index := cast.ToInt(urlInfos[4])
fileName, content := attachments.GetAttachmentsByIndex(ctx, emailId, index)
if len(content) == 0 {
response.NewErrorResponse(response.ParamsError, "", "").FPrint(w)
return
}
w.Header().Set("ContentType", "application/octet-stream")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=%s", fileName))
w.Write(content)
}