36 lines
813 B
Go
36 lines
813 B
Go
package controller
|
|
|
|
import (
|
|
"blog/internal/service"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/kataras/iris/v12"
|
|
"github.com/kataras/iris/v12/mvc"
|
|
"github.com/kataras/iris/v12/sessions"
|
|
)
|
|
|
|
type FileController struct {
|
|
Ctx iris.Context
|
|
Session *sessions.Session
|
|
}
|
|
|
|
func (ctrl *FileController) ViewFile() {
|
|
id := ctrl.Ctx.Params().Get("id")
|
|
file, err := service.FileService.GetFile(id)
|
|
if err != nil {
|
|
return
|
|
}
|
|
ctrl.Ctx.Header("Content-Disposition", "attachment;filename="+url.QueryEscape(file.FileName))
|
|
ctrl.Ctx.Write(file.Data)
|
|
}
|
|
|
|
func (ctrl *FileController) BeforeActivation(activation mvc.BeforeActivation) {
|
|
// log.Println("before")
|
|
activation.Handle(http.MethodGet, "{id}", "ViewFile")
|
|
}
|
|
|
|
func (ctrl *FileController) AfterActivation(activation mvc.AfterActivation) {
|
|
// log.Println("after")
|
|
}
|