Browse Source

Fixed ContentLength not serving issue when gzip is enabled

Toby Chui 3 years ago
parent
commit
ad175e9783

+ 7 - 8
mediaServer.go

@@ -39,6 +39,8 @@ func mediaServer_init() {
 		http.HandleFunc("/media/getMime/", serveMediaMime)
 	}
 
+	//Download API always bypass gzip no matter if gzip mode is enabled
+	http.HandleFunc("/media/download/", serverMedia)
 }
 
 //This function validate the incoming media request and return the real path for the targed file
@@ -138,6 +140,11 @@ func serverMedia(w http.ResponseWriter, r *http.Request) {
 		downloadMode = true
 	}
 
+	//New download implementations, allow /download to be used instead of &download=true
+	if strings.Contains(r.RequestURI, "media/download/?file=") {
+		downloadMode = true
+	}
+
 	//Serve the file
 	if downloadMode {
 		userAgent := r.Header.Get("User-Agent")
@@ -152,14 +159,6 @@ func serverMedia(w http.ResponseWriter, r *http.Request) {
 			w.Header().Set("Content-Disposition", "attachment; filename*=UTF-8''"+filename)
 		}
 		//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())))
-			}
-		*/
 	}
 
 	http.ServeFile(w, r, realFilepath)

+ 1 - 1
web/Code Studio/index.html

@@ -1416,7 +1416,7 @@
                     return;
                 }
 
-                window.open("../../../media?file=" + currentFileData.filepath+ "&download=true");
+                window.open("../../../media/download/?file=" + currentFileData.filepath);
             }
 
             function getCurrentFocusedFileData(){

+ 8 - 2
web/Music/index.html

@@ -929,8 +929,14 @@
 	}
 
 	function generateDownloadElement(filepath, filename){
-	    var link = document.createElement('a');
-        link.href = filepath + "&download=true";
+		var link = document.createElement('a');
+
+		//Clean the filepath
+		filepath = filepath.split("//").join("/");
+		//Convert the filepath to download path
+        link.href = filepath.replace("/media?file=", "/media/download/?file=");
+
+		//Generate the download element
         link.setAttribute('download', filename);
         document.getElementsByTagName("body")[0].appendChild(link);
         // Firefox

+ 2 - 2
web/SystemAO/file_system/file_explorer.html

@@ -3197,7 +3197,7 @@
                 var fileList = [];
                 if ($(".fileObject.selected").length == 1 && $(".fileObject.selected").attr("type") == "file"){
                     //One file. Download directly.
-                    var downloadURL = "../../media?file=" + encodeURIComponent($(".fileObject.selected").attr("filepath")) + "&download=true";
+                    var downloadURL = "../../media/download?file=" + encodeURIComponent($(".fileObject.selected").attr("filepath"));
                     var filename = $(".fileObject.selected").attr("filename");
                     generateDownloadFromURL(downloadURL,escape(filename));
                 }else if ($(".fileObject.selected").length > 1 || ($(".fileObject.selected").length == 1 && $(".fileObject.selected").attr("type") == "folder")){
@@ -3238,7 +3238,7 @@
                                 targetTaskDisplayObject.find(".progress").removeClass("preparing").removeClass("primary").addClass("positive");
 
                                 //Open the zip file
-                                window.open("../../media?file=" + data + "&download=true");
+                                window.open("../../media/download?file=" + data);
                             }
 
                            

+ 1 - 1
web/SystemAO/file_system/file_versions.html

@@ -141,7 +141,7 @@
                 var dirname = targetFile.split("/");
                 dirname.pop();
                 var accessPath = dirname.join("/") + "/" + relpath;
-                window.open("../../media?file=" + accessPath + "&download=true")
+                window.open("../../media/download?file=" + accessPath)
             }
 
             function createSnapshot(){

+ 1 - 1
web/Video/embedded.html

@@ -72,7 +72,7 @@
                     contextmenu: [
                         {
                             text: 'Download',
-                            link: '../media?file=' + encodeURIComponent(playbackFile.filepath) + "&download=true",
+                            link: '../media/download?file=' + encodeURIComponent(playbackFile.filepath),
                         }
                     ],
                 });

+ 2 - 2
web/desktop.system

@@ -4848,7 +4848,7 @@
                 var filedata = JSON.parse(decodeURIComponent(rawfiledata));
                 var filepath = filedata.Filepath;
                 //Resolve the link to media download link
-                var downloadLink = "media?file=" + filepath + "&download=true";
+                var downloadLink = "media/download?file=" + filepath;
                 window.open(downloadLink)
 
             }else if ($(".launchIconWrapper.selected").length > 1){
@@ -4869,7 +4869,7 @@
                             alert(data.error);
                         }else{
                             //Zip completed.
-                            window.open("media?file=" + data + "&download=true");
+                            window.open("media/download?file=" + data);
                         }
                     }
                 });