2017-10-08 02:53:00 +00:00
|
|
|
package contented
|
|
|
|
|
|
|
|
import (
|
2017-10-08 03:01:41 +00:00
|
|
|
"log"
|
2017-10-08 02:53:00 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2017-10-08 03:01:41 +00:00
|
|
|
func (this *Server) handleView(w http.ResponseWriter, r *http.Request, fileID string) {
|
|
|
|
err := this.handleViewInternal(w, r, r.URL.Path[len(downloadUrlPrefix):])
|
|
|
|
if err != nil {
|
2017-10-15 06:11:13 +00:00
|
|
|
log.Printf("%s View failed: %s\n", this.remoteIP(r), err.Error())
|
2017-10-08 03:01:41 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
http.Error(w, "File not found", 404)
|
|
|
|
} else {
|
|
|
|
http.Error(w, "Couldn't provide content", 500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Server) handleViewInternal(w http.ResponseWriter, r *http.Request, fileID string) error {
|
2017-10-08 02:53:00 +00:00
|
|
|
|
|
|
|
// Load metadata
|
|
|
|
m, err := this.Metadata(fileID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load file
|
2023-05-19 07:13:55 +00:00
|
|
|
f, err := this.ReadFile(r.Context(), m.FileHash)
|
2017-10-08 02:53:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// ServeContent only uses the filename to get the mime type, which we can
|
|
|
|
// set accurately (including blacklist)
|
2018-09-09 06:33:33 +00:00
|
|
|
|
|
|
|
switch m.MimeType {
|
|
|
|
case `text/plain`:
|
|
|
|
w.Header().Set(`Content-Type`, `text/plain; charset=UTF-8`)
|
|
|
|
|
|
|
|
case `application/octet-stream`:
|
|
|
|
w.Header().Set(`Content-Type`, m.MimeType)
|
2017-10-08 02:53:00 +00:00
|
|
|
w.Header().Set(`Content-Disposition`, `attachment; filename="`+m.Filename+`"`)
|
2018-09-09 06:33:33 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
w.Header().Set(`Content-Type`, m.MimeType)
|
2017-10-08 02:53:00 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 07:13:55 +00:00
|
|
|
/*
|
|
|
|
if _, ok := f.(io.ReadSeeker); ! ok {
|
|
|
|
// Stream directly, no support for bytes/etag
|
|
|
|
w.Header().Set(`Content-Length`, fmt.Sprintf("%d", m.FileSize))
|
|
|
|
_, err := io.Copy(w, f)
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
2017-10-08 02:53:00 +00:00
|
|
|
|
2023-05-19 07:13:55 +00:00
|
|
|
// Allow range requests, if-modified-since, and so on
|
|
|
|
http.ServeContent(w, r, "", m.UploadTime, f)
|
2017-10-08 02:53:00 +00:00
|
|
|
return nil
|
2023-05-19 07:13:55 +00:00
|
|
|
|
2017-10-08 02:53:00 +00:00
|
|
|
}
|