Browse Source

Added content length in download

Toby Chui 3 years ago
parent
commit
5ae95795f0
1 changed files with 10 additions and 3 deletions
  1. 10 3
      mediaServer.go

+ 10 - 3
mediaServer.go

@@ -5,7 +5,9 @@ import (
 	"log"
 	"log"
 	"net/http"
 	"net/http"
 	"net/url"
 	"net/url"
+	"os"
 	"path/filepath"
 	"path/filepath"
+	"strconv"
 	"strings"
 	"strings"
 
 
 	"imuslab.com/arozos/mod/common"
 	"imuslab.com/arozos/mod/common"
@@ -142,16 +144,21 @@ func serverMedia(w http.ResponseWriter, r *http.Request) {
 	if downloadMode {
 	if downloadMode {
 		userAgent := r.Header.Get("User-Agent")
 		userAgent := r.Header.Get("User-Agent")
 		filename := strings.ReplaceAll(url.QueryEscape(filepath.Base(realFilepath)), "+", "%20")
 		filename := strings.ReplaceAll(url.QueryEscape(filepath.Base(realFilepath)), "+", "%20")
-		log.Println(r.Header.Get("User-Agent"))
+		//log.Println(r.Header.Get("User-Agent"))
 
 
 		if strings.Contains(userAgent, "Safari/") {
 		if strings.Contains(userAgent, "Safari/") {
 			//This is Safari. Use speial header
 			//This is Safari. Use speial header
 			w.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(realFilepath))
 			w.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(realFilepath))
-			w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
 		} else {
 		} else {
 			//Fixing the header issue on Golang url encode lib problems
 			//Fixing the header issue on Golang url encode lib problems
 			w.Header().Set("Content-Disposition", "attachment; filename*=UTF-8''"+filename)
 			w.Header().Set("Content-Disposition", "attachment; filename*=UTF-8''"+filename)
-			w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
+		}
+		w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
+
+		//Serve content length by trying to get filesize
+		fi, err := os.Stat(filepath.Clean(realFilepath))
+		if err == nil {
+			w.Header().Set("Content-Length", strconv.Itoa(int(fi.Size())))
 		}
 		}
 	}
 	}