mirror of
https://github.com/Jinnrry/PMail.git
synced 2025-02-20 11:43:09 +08:00
51 lines
1.4 KiB
Go
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)
|
|
}
|