2015-02-26 15:59:07 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2015-12-15 14:38:58 +08:00
|
|
|
"bytes"
|
2024-06-15 02:40:34 +08:00
|
|
|
"context"
|
2020-02-26 13:50:12 +08:00
|
|
|
"encoding/json"
|
2019-06-06 15:29:02 +08:00
|
|
|
"errors"
|
2019-10-24 13:11:42 +08:00
|
|
|
"fmt"
|
2015-02-26 15:59:07 +08:00
|
|
|
"io"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
2016-06-14 06:07:17 +08:00
|
|
|
"net/url"
|
2020-03-09 06:42:44 +08:00
|
|
|
"path/filepath"
|
2015-02-26 15:59:07 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-08-09 14:25:16 +08:00
|
|
|
"sync/atomic"
|
2015-02-26 15:59:07 +08:00
|
|
|
"time"
|
|
|
|
|
2024-01-30 02:35:52 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
2023-01-07 01:28:07 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/mem"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/images"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
2023-10-30 22:38:03 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2015-02-26 15:59:07 +08:00
|
|
|
)
|
|
|
|
|
2021-02-18 14:05:28 +08:00
|
|
|
var fileNameEscaper = strings.NewReplacer(`\`, `\\`, `"`, `\"`)
|
2015-02-26 15:59:07 +08:00
|
|
|
|
2023-10-30 22:38:03 +08:00
|
|
|
func NotFound(w http.ResponseWriter) {
|
2024-01-11 02:05:27 +08:00
|
|
|
stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorGetNotFound).Inc()
|
2023-10-30 22:38:03 +08:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func InternalError(w http.ResponseWriter) {
|
2024-01-11 02:05:27 +08:00
|
|
|
stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorGetInternal).Inc()
|
2023-10-30 22:38:03 +08:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
2015-02-26 15:59:07 +08:00
|
|
|
func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) {
|
2019-04-19 12:43:36 +08:00
|
|
|
n := new(needle.Needle)
|
2015-02-26 15:59:07 +08:00
|
|
|
vid, fid, filename, ext, _ := parseURLPath(r.URL.Path)
|
2019-06-06 15:29:02 +08:00
|
|
|
|
|
|
|
if !vs.maybeCheckJwtAuthorization(r, vid, fid, false) {
|
|
|
|
writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-19 12:43:36 +08:00
|
|
|
volumeId, err := needle.NewVolumeId(vid)
|
2015-02-26 15:59:07 +08:00
|
|
|
if err != nil {
|
2020-04-22 07:13:13 +08:00
|
|
|
glog.V(2).Infof("parsing vid %s: %v", r.URL.Path, err)
|
2015-02-26 15:59:07 +08:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = n.ParsePath(fid)
|
|
|
|
if err != nil {
|
2020-04-22 07:13:13 +08:00
|
|
|
glog.V(2).Infof("parsing fid %s: %v", r.URL.Path, err)
|
2015-02-26 15:59:07 +08:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-25 09:26:03 +08:00
|
|
|
// glog.V(4).Infoln("volume", volumeId, "reading", n)
|
2019-05-27 16:29:46 +08:00
|
|
|
hasVolume := vs.store.HasVolume(volumeId)
|
2019-05-28 12:40:51 +08:00
|
|
|
_, hasEcVolume := vs.store.FindEcVolume(volumeId)
|
|
|
|
if !hasVolume && !hasEcVolume {
|
2021-06-30 17:28:37 +08:00
|
|
|
if vs.ReadMode == "local" {
|
|
|
|
glog.V(0).Infoln("volume is not local:", err, r.URL.Path)
|
2023-10-30 22:38:03 +08:00
|
|
|
NotFound(w)
|
2015-08-04 05:43:15 +08:00
|
|
|
return
|
|
|
|
}
|
2021-08-13 11:33:00 +08:00
|
|
|
lookupResult, err := operation.LookupVolumeId(vs.GetMaster, vs.grpcDialOption, volumeId.String())
|
2015-02-26 15:59:07 +08:00
|
|
|
glog.V(2).Infoln("volume", volumeId, "found on", lookupResult, "error", err)
|
2021-07-01 16:21:14 +08:00
|
|
|
if err != nil || len(lookupResult.Locations) <= 0 {
|
2021-06-30 17:28:37 +08:00
|
|
|
glog.V(0).Infoln("lookup error:", err, r.URL.Path)
|
2023-10-30 22:38:03 +08:00
|
|
|
NotFound(w)
|
2021-06-30 17:28:37 +08:00
|
|
|
return
|
|
|
|
}
|
2021-06-30 18:33:18 +08:00
|
|
|
if vs.ReadMode == "proxy" {
|
2021-06-30 17:28:37 +08:00
|
|
|
// proxy client request to target server
|
|
|
|
u, _ := url.Parse(util.NormalizeUrl(lookupResult.Locations[0].Url))
|
|
|
|
r.URL.Host = u.Host
|
|
|
|
r.URL.Scheme = u.Scheme
|
|
|
|
request, err := http.NewRequest("GET", r.URL.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("failed to instance http request of url %s: %v", r.URL.String(), err)
|
2023-10-30 22:38:03 +08:00
|
|
|
InternalError(w)
|
2021-06-30 17:28:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for k, vv := range r.Header {
|
|
|
|
for _, v := range vv {
|
|
|
|
request.Header.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("request remote url %s: %v", r.URL.String(), err)
|
2023-10-30 22:38:03 +08:00
|
|
|
InternalError(w)
|
2021-06-30 17:28:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer util.CloseResponse(response)
|
|
|
|
// proxy target response to client
|
|
|
|
for k, vv := range response.Header {
|
|
|
|
for _, v := range vv {
|
|
|
|
w.Header().Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.WriteHeader(response.StatusCode)
|
2022-03-03 12:15:28 +08:00
|
|
|
buf := mem.Allocate(128 * 1024)
|
|
|
|
defer mem.Free(buf)
|
|
|
|
io.CopyBuffer(w, response.Body, buf)
|
2021-06-30 17:28:37 +08:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// redirect
|
2016-02-27 17:42:41 +08:00
|
|
|
u, _ := url.Parse(util.NormalizeUrl(lookupResult.Locations[0].PublicUrl))
|
2019-10-24 13:11:42 +08:00
|
|
|
u.Path = fmt.Sprintf("%s/%s,%s", u.Path, vid, fid)
|
2016-02-27 17:42:41 +08:00
|
|
|
arg := url.Values{}
|
|
|
|
if c := r.FormValue("collection"); c != "" {
|
|
|
|
arg.Set("collection", c)
|
|
|
|
}
|
|
|
|
u.RawQuery = arg.Encode()
|
|
|
|
http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
|
2021-06-30 17:28:37 +08:00
|
|
|
return
|
2015-02-26 15:59:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cookie := n.Cookie
|
2020-08-19 08:37:26 +08:00
|
|
|
|
|
|
|
readOption := &storage.ReadOption{
|
2022-09-16 15:30:40 +08:00
|
|
|
ReadDeleted: r.FormValue("readDeleted") == "true",
|
|
|
|
HasSlowRead: vs.hasSlowRead,
|
2022-09-18 01:50:06 +08:00
|
|
|
ReadBufferSize: vs.readBufferSizeMB * 1024 * 1024,
|
2020-08-19 08:37:26 +08:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:29:46 +08:00
|
|
|
var count int
|
2022-09-14 15:06:54 +08:00
|
|
|
var memoryCost types.Size
|
2022-06-06 02:54:04 +08:00
|
|
|
readOption.AttemptMetaOnly, readOption.MustMetaOnly = shouldAttemptStreamWrite(hasVolume, ext, r)
|
2021-08-09 14:25:16 +08:00
|
|
|
onReadSizeFn := func(size types.Size) {
|
2022-09-14 15:06:54 +08:00
|
|
|
memoryCost = size
|
|
|
|
atomic.AddInt64(&vs.inFlightDownloadDataSize, int64(memoryCost))
|
2021-08-09 14:25:16 +08:00
|
|
|
}
|
2019-05-27 16:29:46 +08:00
|
|
|
if hasVolume {
|
2021-08-09 14:25:16 +08:00
|
|
|
count, err = vs.store.ReadVolumeNeedle(volumeId, n, readOption, onReadSizeFn)
|
2019-05-28 12:40:51 +08:00
|
|
|
} else if hasEcVolume {
|
2021-08-09 14:25:16 +08:00
|
|
|
count, err = vs.store.ReadEcShardNeedle(volumeId, n, onReadSizeFn)
|
2019-05-27 16:29:46 +08:00
|
|
|
}
|
2021-08-09 14:25:16 +08:00
|
|
|
defer func() {
|
2022-09-14 15:06:54 +08:00
|
|
|
atomic.AddInt64(&vs.inFlightDownloadDataSize, -int64(memoryCost))
|
2021-08-09 14:25:16 +08:00
|
|
|
vs.inFlightDownloadDataLimitCond.Signal()
|
|
|
|
}()
|
|
|
|
|
2022-06-05 10:11:06 +08:00
|
|
|
if err != nil && err != storage.ErrorDeleted && hasVolume {
|
2020-10-09 09:40:38 +08:00
|
|
|
glog.V(4).Infof("read needle: %v", err)
|
2020-10-09 09:33:06 +08:00
|
|
|
// start to fix it from other replicas, if not deleted and hasVolume and is not a replicated request
|
|
|
|
}
|
2020-01-25 09:26:03 +08:00
|
|
|
// glog.V(4).Infoln("read bytes", count, "error", err)
|
2019-05-27 16:29:46 +08:00
|
|
|
if err != nil || count < 0 {
|
2020-09-09 18:49:26 +08:00
|
|
|
glog.V(3).Infof("read %s isNormalVolume %v error: %v", r.URL.Path, hasVolume, err)
|
2022-08-19 18:36:15 +08:00
|
|
|
if err == storage.ErrorNotFound || err == storage.ErrorDeleted {
|
2023-10-30 22:38:03 +08:00
|
|
|
NotFound(w)
|
2022-08-19 18:36:15 +08:00
|
|
|
} else {
|
2023-10-30 22:38:03 +08:00
|
|
|
InternalError(w)
|
2022-08-19 18:36:15 +08:00
|
|
|
}
|
2015-02-26 15:59:07 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if n.Cookie != cookie {
|
2018-11-22 16:42:56 +08:00
|
|
|
glog.V(0).Infof("request %s with cookie:%x expected:%x from %s agent %s", r.URL.Path, cookie, n.Cookie, r.RemoteAddr, r.UserAgent())
|
2023-10-30 22:38:03 +08:00
|
|
|
NotFound(w)
|
2015-02-26 15:59:07 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if n.LastModified != 0 {
|
|
|
|
w.Header().Set("Last-Modified", time.Unix(int64(n.LastModified), 0).UTC().Format(http.TimeFormat))
|
|
|
|
if r.Header.Get("If-Modified-Since") != "" {
|
|
|
|
if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
|
|
|
|
if t.Unix() >= int64(n.LastModified) {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 07:25:43 +08:00
|
|
|
if inm := r.Header.Get("If-None-Match"); inm == "\""+n.Etag()+"\"" {
|
2015-02-26 15:59:07 +08:00
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
2024-06-25 08:14:52 +08:00
|
|
|
SetEtag(w, n.Etag())
|
2015-12-01 20:23:50 +08:00
|
|
|
|
2017-01-08 09:16:29 +08:00
|
|
|
if n.HasPairs() {
|
|
|
|
pairMap := make(map[string]string)
|
|
|
|
err = json.Unmarshal(n.Pairs, &pairMap)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infoln("Unmarshal pairs error:", err)
|
|
|
|
}
|
|
|
|
for k, v := range pairMap {
|
|
|
|
w.Header().Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 12:23:15 +08:00
|
|
|
if vs.tryHandleChunkedFile(n, filename, ext, w, r) {
|
2015-12-01 20:23:50 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-26 15:59:07 +08:00
|
|
|
if n.NameSize > 0 && filename == "" {
|
|
|
|
filename = string(n.Name)
|
2015-12-15 11:26:16 +08:00
|
|
|
if ext == "" {
|
2020-03-09 06:42:44 +08:00
|
|
|
ext = filepath.Ext(filename)
|
2015-02-26 15:59:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mtype := ""
|
|
|
|
if n.MimeSize > 0 {
|
|
|
|
mt := string(n.Mime)
|
2015-05-16 18:55:58 +08:00
|
|
|
if !strings.HasPrefix(mt, "application/octet-stream") {
|
2015-02-26 15:59:07 +08:00
|
|
|
mtype = mt
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 11:26:16 +08:00
|
|
|
|
2020-08-02 02:17:19 +08:00
|
|
|
if n.IsCompressed() {
|
2023-01-07 01:28:07 +08:00
|
|
|
_, _, _, shouldResize := shouldResizeImages(ext, r)
|
|
|
|
_, _, _, _, shouldCrop := shouldCropImages(ext, r)
|
|
|
|
if shouldResize || shouldCrop {
|
2020-08-02 02:17:19 +08:00
|
|
|
if n.Data, err = util.DecompressData(n.Data); err != nil {
|
|
|
|
glog.V(0).Infoln("ungzip error:", err, r.URL.Path)
|
|
|
|
}
|
2020-11-27 03:22:30 +08:00
|
|
|
// } else if strings.Contains(r.Header.Get("Accept-Encoding"), "zstd") && util.IsZstdContent(n.Data) {
|
|
|
|
// w.Header().Set("Content-Encoding", "zstd")
|
2020-08-02 02:17:19 +08:00
|
|
|
} else if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && util.IsGzippedContent(n.Data) {
|
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
} else {
|
|
|
|
if n.Data, err = util.DecompressData(n.Data); err != nil {
|
|
|
|
glog.V(0).Infoln("uncompress error:", err, r.URL.Path)
|
2015-02-26 15:59:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-24 15:58:52 +08:00
|
|
|
|
2022-06-06 02:54:04 +08:00
|
|
|
if !readOption.IsMetaOnly {
|
2023-01-07 01:28:07 +08:00
|
|
|
rs := conditionallyCropImages(bytes.NewReader(n.Data), ext, r)
|
|
|
|
rs = conditionallyResizeImages(rs, ext, r)
|
2022-06-06 02:54:04 +08:00
|
|
|
if e := writeResponseContent(filename, mtype, rs, w, r); e != nil {
|
|
|
|
glog.V(2).Infoln("response write error:", e)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vs.streamWriteResponseContent(filename, mtype, volumeId, n, w, r, readOption)
|
|
|
|
}
|
|
|
|
}
|
2015-02-26 15:59:07 +08:00
|
|
|
|
2022-06-06 02:54:04 +08:00
|
|
|
func shouldAttemptStreamWrite(hasLocalVolume bool, ext string, r *http.Request) (shouldAttempt bool, mustMetaOnly bool) {
|
|
|
|
if !hasLocalVolume {
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
if len(ext) > 0 {
|
|
|
|
ext = strings.ToLower(ext)
|
|
|
|
}
|
|
|
|
if r.Method == "HEAD" {
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
_, _, _, shouldResize := shouldResizeImages(ext, r)
|
2023-01-07 01:28:07 +08:00
|
|
|
_, _, _, _, shouldCrop := shouldCropImages(ext, r)
|
|
|
|
if shouldResize || shouldCrop {
|
2022-06-06 02:54:04 +08:00
|
|
|
return false, false
|
2015-02-26 15:59:07 +08:00
|
|
|
}
|
2022-06-06 02:54:04 +08:00
|
|
|
return true, false
|
2015-02-26 15:59:07 +08:00
|
|
|
}
|
2015-12-01 20:23:50 +08:00
|
|
|
|
2020-04-03 12:23:15 +08:00
|
|
|
func (vs *VolumeServer) tryHandleChunkedFile(n *needle.Needle, fileName string, ext string, w http.ResponseWriter, r *http.Request) (processed bool) {
|
2017-03-13 09:40:24 +08:00
|
|
|
if !n.IsChunkedManifest() || r.URL.Query().Get("cm") == "false" {
|
2015-12-01 20:23:50 +08:00
|
|
|
return false
|
|
|
|
}
|
2015-12-02 16:35:16 +08:00
|
|
|
|
2020-06-20 13:45:27 +08:00
|
|
|
chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsCompressed())
|
2015-12-01 20:23:50 +08:00
|
|
|
if e != nil {
|
2015-12-15 14:38:58 +08:00
|
|
|
glog.V(0).Infof("load chunked manifest (%s) error: %v", r.URL.Path, e)
|
2015-12-01 20:23:50 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if fileName == "" && chunkManifest.Name != "" {
|
|
|
|
fileName = chunkManifest.Name
|
|
|
|
}
|
2018-07-05 10:34:17 +08:00
|
|
|
|
2020-04-03 12:23:15 +08:00
|
|
|
if ext == "" {
|
|
|
|
ext = filepath.Ext(fileName)
|
|
|
|
}
|
2018-07-05 10:34:17 +08:00
|
|
|
|
2015-12-15 11:26:16 +08:00
|
|
|
mType := ""
|
2015-12-01 20:23:50 +08:00
|
|
|
if chunkManifest.Mime != "" {
|
|
|
|
mt := chunkManifest.Mime
|
|
|
|
if !strings.HasPrefix(mt, "application/octet-stream") {
|
2015-12-15 11:26:16 +08:00
|
|
|
mType = mt
|
2015-12-01 20:23:50 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 11:26:16 +08:00
|
|
|
|
2015-12-01 20:23:50 +08:00
|
|
|
w.Header().Set("X-File-Store", "chunked")
|
|
|
|
|
2024-06-15 02:40:34 +08:00
|
|
|
chunkedFileReader := operation.NewChunkedFileReader(chunkManifest.Chunks, vs.GetMaster(context.Background()), vs.grpcDialOption)
|
2015-12-01 20:23:50 +08:00
|
|
|
defer chunkedFileReader.Close()
|
2018-07-24 15:58:52 +08:00
|
|
|
|
2023-01-07 01:28:07 +08:00
|
|
|
rs := conditionallyCropImages(chunkedFileReader, ext, r)
|
|
|
|
rs = conditionallyResizeImages(rs, ext, r)
|
2018-07-24 15:58:52 +08:00
|
|
|
|
|
|
|
if e := writeResponseContent(fileName, mType, rs, w, r); e != nil {
|
|
|
|
glog.V(2).Infoln("response write error:", e)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func conditionallyResizeImages(originalDataReaderSeeker io.ReadSeeker, ext string, r *http.Request) io.ReadSeeker {
|
|
|
|
rs := originalDataReaderSeeker
|
2020-07-17 19:57:35 +08:00
|
|
|
if len(ext) > 0 {
|
|
|
|
ext = strings.ToLower(ext)
|
|
|
|
}
|
2020-03-21 11:31:11 +08:00
|
|
|
width, height, mode, shouldResize := shouldResizeImages(ext, r)
|
|
|
|
if shouldResize {
|
|
|
|
rs, _, _ = images.Resized(ext, originalDataReaderSeeker, width, height, mode)
|
|
|
|
}
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
|
|
|
|
func shouldResizeImages(ext string, r *http.Request) (width, height int, mode string, shouldResize bool) {
|
2021-07-24 13:32:31 +08:00
|
|
|
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".webp" {
|
2018-07-05 10:34:17 +08:00
|
|
|
if r.FormValue("width") != "" {
|
|
|
|
width, _ = strconv.Atoi(r.FormValue("width"))
|
|
|
|
}
|
|
|
|
if r.FormValue("height") != "" {
|
|
|
|
height, _ = strconv.Atoi(r.FormValue("height"))
|
|
|
|
}
|
|
|
|
}
|
2020-03-21 11:31:11 +08:00
|
|
|
mode = r.FormValue("mode")
|
|
|
|
shouldResize = width > 0 || height > 0
|
|
|
|
return
|
2015-12-15 11:26:16 +08:00
|
|
|
}
|
|
|
|
|
2023-01-07 01:28:07 +08:00
|
|
|
func conditionallyCropImages(originalDataReaderSeeker io.ReadSeeker, ext string, r *http.Request) io.ReadSeeker {
|
|
|
|
rs := originalDataReaderSeeker
|
|
|
|
if len(ext) > 0 {
|
|
|
|
ext = strings.ToLower(ext)
|
|
|
|
}
|
|
|
|
x1, y1, x2, y2, shouldCrop := shouldCropImages(ext, r)
|
|
|
|
if shouldCrop {
|
|
|
|
var err error
|
|
|
|
rs, err = images.Cropped(ext, rs, x1, y1, x2, y2)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Cropping images error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
|
|
|
|
func shouldCropImages(ext string, r *http.Request) (x1, y1, x2, y2 int, shouldCrop bool) {
|
|
|
|
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
|
|
|
|
if r.FormValue("crop_x1") != "" {
|
|
|
|
x1, _ = strconv.Atoi(r.FormValue("crop_x1"))
|
|
|
|
}
|
|
|
|
if r.FormValue("crop_y1") != "" {
|
|
|
|
y1, _ = strconv.Atoi(r.FormValue("crop_y1"))
|
|
|
|
}
|
|
|
|
if r.FormValue("crop_x2") != "" {
|
|
|
|
x2, _ = strconv.Atoi(r.FormValue("crop_x2"))
|
|
|
|
}
|
|
|
|
if r.FormValue("crop_y2") != "" {
|
|
|
|
y2, _ = strconv.Atoi(r.FormValue("crop_y2"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
shouldCrop = x1 >= 0 && y1 >= 0 && x2 > x1 && y2 > y1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-15 11:26:16 +08:00
|
|
|
func writeResponseContent(filename, mimeType string, rs io.ReadSeeker, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
totalSize, e := rs.Seek(0, 2)
|
|
|
|
if mimeType == "" {
|
2020-03-09 06:42:44 +08:00
|
|
|
if ext := filepath.Ext(filename); ext != "" {
|
2015-12-15 11:26:16 +08:00
|
|
|
mimeType = mime.TypeByExtension(ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mimeType != "" {
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
|
|
|
}
|
|
|
|
w.Header().Set("Accept-Ranges", "bytes")
|
2020-03-09 06:42:44 +08:00
|
|
|
|
2024-06-25 08:14:52 +08:00
|
|
|
AdjustPassthroughHeaders(w, r, filename)
|
2020-07-26 11:06:40 +08:00
|
|
|
|
2015-12-15 11:26:16 +08:00
|
|
|
if r.Method == "HEAD" {
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-01 20:23:50 +08:00
|
|
|
|
2024-06-25 08:14:52 +08:00
|
|
|
return ProcessRangeRequest(r, w, totalSize, mimeType, func(offset int64, size int64) (filer.DoStreamContent, error) {
|
2024-01-30 02:35:52 +08:00
|
|
|
return func(writer io.Writer) error {
|
|
|
|
if _, e = rs.Seek(offset, 0); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
_, e = io.CopyN(writer, rs, size)
|
2015-12-15 11:26:16 +08:00
|
|
|
return e
|
2024-01-30 02:35:52 +08:00
|
|
|
}, nil
|
2020-03-08 10:06:48 +08:00
|
|
|
})
|
2015-12-01 20:23:50 +08:00
|
|
|
}
|
2022-06-06 02:54:04 +08:00
|
|
|
|
|
|
|
func (vs *VolumeServer) streamWriteResponseContent(filename string, mimeType string, volumeId needle.VolumeId, n *needle.Needle, w http.ResponseWriter, r *http.Request, readOption *storage.ReadOption) {
|
|
|
|
totalSize := int64(n.DataSize)
|
|
|
|
if mimeType == "" {
|
|
|
|
if ext := filepath.Ext(filename); ext != "" {
|
|
|
|
mimeType = mime.TypeByExtension(ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mimeType != "" {
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
|
|
|
}
|
|
|
|
w.Header().Set("Accept-Ranges", "bytes")
|
2024-06-25 08:14:52 +08:00
|
|
|
AdjustPassthroughHeaders(w, r, filename)
|
2022-06-06 02:54:04 +08:00
|
|
|
|
|
|
|
if r.Method == "HEAD" {
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-25 08:14:52 +08:00
|
|
|
ProcessRangeRequest(r, w, totalSize, mimeType, func(offset int64, size int64) (filer.DoStreamContent, error) {
|
2024-01-30 02:35:52 +08:00
|
|
|
return func(writer io.Writer) error {
|
|
|
|
return vs.store.ReadVolumeNeedleDataInto(volumeId, n, readOption, writer, offset, size)
|
|
|
|
}, nil
|
2022-06-06 02:54:04 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|