feat: use new file abstraction and use http.ServeContent
This commit is contained in:
parent
2f9b684953
commit
bf25d7bfda
|
@ -19,6 +19,7 @@ import (
|
||||||
"git.lumeweb.com/LumeWeb/portal/db/models"
|
"git.lumeweb.com/LumeWeb/portal/db/models"
|
||||||
"git.lumeweb.com/LumeWeb/portal/interfaces"
|
"git.lumeweb.com/LumeWeb/portal/interfaces"
|
||||||
"git.lumeweb.com/LumeWeb/portal/protocols"
|
"git.lumeweb.com/LumeWeb/portal/protocols"
|
||||||
|
"git.lumeweb.com/LumeWeb/portal/storage"
|
||||||
emailverifier "github.com/AfterShip/email-verifier"
|
emailverifier "github.com/AfterShip/email-verifier"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
"github.com/vmihailenco/msgpack/v5"
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
|
@ -1302,10 +1303,13 @@ func (h *HttpHandler) DownloadFile(jc jape.Context) {
|
||||||
|
|
||||||
hashBytes = cidDecoded.Hash.HashBytes()
|
hashBytes = cidDecoded.Hash.HashBytes()
|
||||||
|
|
||||||
file, fileSize, err := h.portal.Storage().GetFile(hashBytes)
|
file := storage.NewFile(hashBytes, h.portal.Storage())
|
||||||
if jc.Check("error getting file", err) != nil {
|
|
||||||
|
if !file.Exists() {
|
||||||
|
jc.ResponseWriter.WriteHeader(http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func(file io.ReadCloser) {
|
defer func(file io.ReadCloser) {
|
||||||
err := file.Close()
|
err := file.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1313,21 +1317,7 @@ func (h *HttpHandler) DownloadFile(jc jape.Context) {
|
||||||
}
|
}
|
||||||
}(file)
|
}(file)
|
||||||
|
|
||||||
mimeBuffer := make([]byte, 512)
|
http.ServeContent(jc.ResponseWriter, jc.Request, "", file.Modtime(), file)
|
||||||
if _, err := file.Read(mimeBuffer); err != nil {
|
|
||||||
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
contentType := http.DetectContentType(mimeBuffer)
|
|
||||||
|
|
||||||
jc.ResponseWriter.Header().Set("Content-Type", contentType)
|
|
||||||
jc.ResponseWriter.Header().Set("Content-Length", strconv.FormatUint(fileSize, 10))
|
|
||||||
jc.ResponseWriter.Header().Set("Cache-Control", "public, max-age=31536000")
|
|
||||||
|
|
||||||
jc.ResponseWriter.WriteHeader(http.StatusOK)
|
|
||||||
_, _ = jc.ResponseWriter.Write(mimeBuffer)
|
|
||||||
_, _ = io.Copy(jc.ResponseWriter, file)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setAuthCookie(jwt string, jc jape.Context) {
|
func setAuthCookie(jwt string, jc jape.Context) {
|
||||||
|
|
Loading…
Reference in New Issue