s3manager-web/objects/get-handler.go

37 lines
936 B
Go
Raw Normal View History

2017-03-30 22:48:27 +02:00
package objects
import (
"fmt"
"io"
"net/http"
"github.com/gorilla/mux"
2017-04-02 17:10:36 +02:00
"github.com/mastertinner/s3-manager/datasources"
"github.com/mastertinner/s3-manager/utils"
2017-03-30 22:48:27 +02:00
)
// GetHandler downloads an object to the client
2017-04-02 17:10:36 +02:00
func GetHandler(s3 datasources.S3Client) http.Handler {
2017-03-30 22:48:27 +02:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
objectName := vars["objectName"]
object, err := s3.GetObject(vars["bucketName"], objectName)
if err != nil {
msg := "error getting object"
2017-04-02 17:10:36 +02:00
utils.HandleHTTPError(w, msg, err, http.StatusInternalServerError)
2017-03-30 22:48:27 +02:00
return
}
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", objectName))
w.Header().Set("Content-Type", "application/octet-stream")
_, err = io.Copy(w, object)
if err != nil {
msg := "error copying object"
2017-04-02 17:10:36 +02:00
utils.HandleHTTPError(w, msg, err, http.StatusInternalServerError)
2017-03-30 22:48:27 +02:00
return
}
})
}