Browse Source

Added multi-thread download for share

Toby Chui 3 years ago
parent
commit
e4209c91bb
2 changed files with 22 additions and 7 deletions
  1. 7 1
      mod/network/gzipmiddleware/gzipmiddleware.go
  2. 15 6
      mod/share/share.go

+ 7 - 1
mod/network/gzipmiddleware/gzipmiddleware.go

@@ -29,7 +29,7 @@ type gzipResponseWriter struct {
 }
 
 func (w *gzipResponseWriter) WriteHeader(status int) {
-	w.Header().Del("Content-Length")
+	//w.Header().Del("Content-Length")
 	w.ResponseWriter.WriteHeader(status)
 }
 
@@ -48,6 +48,12 @@ func Compress(h http.Handler) http.Handler {
 			return
 		}
 
+		//Handle very special case where it is /share/download
+		if strings.HasPrefix(r.URL.RequestURI(), "/share/download/") {
+			h.ServeHTTP(w, r)
+			return
+		}
+
 		//Check if this is websocket request. Skip this if true
 		if r.Header["Upgrade"] != nil && r.Header["Upgrade"][0] == "websocket" {
 			//WebSocket request. Do not gzip it

+ 15 - 6
mod/share/share.go

@@ -20,6 +20,7 @@ import (
 	"io/ioutil"
 	"log"
 	"math"
+	"mime"
 	"net/http"
 	"net/url"
 	"os"
@@ -681,17 +682,25 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 			}
 		} else {
 			//This share is a file
+			contentType := mime.TypeByExtension(filepath.Ext(fileRuntimeAbsPath))
 			if directDownload {
 				//Serve the file directly
 				w.Header().Set("Content-Disposition", "attachment; filename=\""+filepath.Base(shareOption.FileVirtualPath)+"\"")
-				w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
-				f, _ := targetFshAbs.ReadStream(fileRuntimeAbsPath)
-				io.Copy(w, f)
-				f.Close()
+				w.Header().Set("Content-Type", contentType)
+				w.Header().Set("Content-Length", strconv.Itoa(int(targetFshAbs.GetFileSize(fileRuntimeAbsPath))))
+
+				if targetFsh.RequireBuffer {
+					f, _ := targetFshAbs.ReadStream(fileRuntimeAbsPath)
+					io.Copy(w, f)
+					f.Close()
+				} else {
+					http.ServeFile(w, r, fileRuntimeAbsPath)
+				}
+
 			} else if directServe {
 				w.Header().Set("Access-Control-Allow-Origin", "*")
 				w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
-				w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
+				w.Header().Set("Content-Type", contentType)
 				f, _ := targetFshAbs.ReadStream(fileRuntimeAbsPath)
 				io.Copy(w, f)
 				f.Close()
@@ -755,7 +764,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 					"ext":         displayExt,
 					"size":        filesystem.GetFileDisplaySize(fsize, 2),
 					"modtime":     timeString,
-					"downloadurl": "../../share/download/" + id + "/" + filepath.Base(fileRuntimeAbsPath),
+					"downloadurl": "/share/download/" + id + "/" + filepath.Base(fileRuntimeAbsPath),
 					"preview_url": "/share/preview/" + id + "/",
 					"filename":    filepath.Base(fileRuntimeAbsPath),
 					"opg_image":   "/share/opg/" + strconv.Itoa(int(time.Now().Unix())) + "/" + id,