mirror of
https://github.com/Forceu/Gokapi.git
synced 2026-01-09 18:39:45 -06:00
Ensure headers are set when proxying S3 file #199
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
||||
"github.com/forceu/gokapi/internal/storage/filesystem/s3filesystem/aws"
|
||||
"github.com/forceu/gokapi/internal/storage/processingstatus"
|
||||
"github.com/forceu/gokapi/internal/webserver/downloadstatus"
|
||||
"github.com/forceu/gokapi/internal/webserver/headers"
|
||||
"github.com/jinzhu/copier"
|
||||
"io"
|
||||
"log"
|
||||
@@ -543,7 +544,7 @@ func ServeFile(file models.File, w http.ResponseWriter, r *http.Request, forceDo
|
||||
}
|
||||
}
|
||||
statusId := downloadstatus.SetDownload(file)
|
||||
writeDownloadHeaders(file, w, forceDownload)
|
||||
headers.WriteDownloadHeaders(file, w, forceDownload)
|
||||
if file.Encryption.IsEncrypted && !file.RequiresClientDecryption() {
|
||||
err := encryption.DecryptReader(file.Encryption, fileData, w)
|
||||
if err != nil {
|
||||
@@ -558,22 +559,6 @@ func ServeFile(file models.File, w http.ResponseWriter, r *http.Request, forceDo
|
||||
downloadstatus.SetComplete(statusId)
|
||||
}
|
||||
|
||||
// writeDownloadHeaders sets headers to either display the file inline or to force download, the content type
|
||||
// and if the file is encrypted, the creation timestamp to now
|
||||
func writeDownloadHeaders(file models.File, w http.ResponseWriter, forceDownload bool) {
|
||||
if forceDownload {
|
||||
w.Header().Set("Content-Disposition", "attachment; filename=\""+file.Name+"\"")
|
||||
} else {
|
||||
w.Header().Set("Content-Disposition", "inline; filename=\""+file.Name+"\"")
|
||||
}
|
||||
w.Header().Set("Content-Type", file.ContentType)
|
||||
|
||||
if file.Encryption.IsEncrypted {
|
||||
w.Header().Set("Accept-Ranges", "bytes")
|
||||
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
|
||||
}
|
||||
}
|
||||
|
||||
func getFileHandler(file models.File, dataDir string) (*os.File, int64) {
|
||||
storageData, err := os.OpenFile(dataDir+"/"+file.SHA1, os.O_RDONLY, 0644)
|
||||
helper.Check(err)
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
||||
"github.com/forceu/gokapi/internal/models"
|
||||
"github.com/forceu/gokapi/internal/webserver/headers"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -172,6 +173,7 @@ func proxyDownload(w http.ResponseWriter, file models.File, forceDownload bool)
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
headers.WriteDownloadHeaders(file, w, forceDownload)
|
||||
_, _ = io.Copy(w, resp.Body)
|
||||
return nil
|
||||
}
|
||||
|
||||
23
internal/webserver/headers/Headers.go
Normal file
23
internal/webserver/headers/Headers.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package headers
|
||||
|
||||
import (
|
||||
"github.com/forceu/gokapi/internal/models"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// WriteDownloadHeaders sets headers to either display the file inline or to force download, the content type
|
||||
// and if the file is encrypted, the creation timestamp to now
|
||||
func WriteDownloadHeaders(file models.File, w http.ResponseWriter, forceDownload bool) {
|
||||
if forceDownload {
|
||||
w.Header().Set("Content-Disposition", "attachment; filename=\""+file.Name+"\"")
|
||||
} else {
|
||||
w.Header().Set("Content-Disposition", "inline; filename=\""+file.Name+"\"")
|
||||
}
|
||||
w.Header().Set("Content-Type", file.ContentType)
|
||||
|
||||
if file.Encryption.IsEncrypted {
|
||||
w.Header().Set("Accept-Ranges", "bytes")
|
||||
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user