Files
PMail/server/controllers/attachments.go
木木的木头 f4689d073c v2.2.0
2023-09-08 02:11:00 +08:00

51 lines
1.3 KiB
Go

package controllers
import (
"fmt"
"github.com/spf13/cast"
"net/http"
"pmail/dto/response"
"pmail/services/attachments"
"pmail/utils/context"
"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)
}