tobychui 3 лет назад
Родитель
Сommit
271383983f

+ 2 - 1
file_system.go

@@ -184,7 +184,8 @@ func FileSystemInit() {
 	router.HandleFunc("/system/file_system/share/checkShared", shareManager.HandleShareCheck)
 
 	//Handle the main share function
-	http.HandleFunc("/share", shareManager.HandleShareAccess)
+	//Share function is now routed by the main router
+	//http.HandleFunc("/share", shareManager.HandleShareAccess)
 
 	/*
 		Nighly Tasks

+ 3 - 0
main.router.go

@@ -62,7 +62,10 @@ func mrouter(h http.Handler) http.Handler {
 			h.ServeHTTP(w, r)
 
 		} else if len(r.URL.Path) >= len("/webdav") && r.URL.Path[:7] == "/webdav" {
+			//WebDAV special handler
 			WebDavHandler.HandleRequest(w, r)
+		} else if len(r.URL.Path) >= len("/share") && r.URL.Path[:6] == "/share" {
+			shareManager.HandleShareAccess(w, r)
 		} else if r.URL.Path == "/" && authAgent.CheckAuth(r) {
 			//Use logged in and request the index. Serve the user's interface module
 			w.Header().Set("Cache-Control", "no-cache, no-store, no-transform, must-revalidate, private, max-age=0")

+ 73 - 22
mod/share/share.go

@@ -86,25 +86,69 @@ func NewShareManager(options Options) *Manager {
 
 //Main function for handle share. Must be called with http.HandleFunc (No auth)
 func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
-	id, err := mv(r, "id", false)
-	if err != nil {
-		http.NotFound(w, r)
-		return
-	}
+	//New download method variables
+	subpathElements := []string{}
 
 	directDownload := false
 	directServe := false
-	download, _ := mv(r, "download", false)
-	if download == "true" {
-		directDownload = true
-	}
+	relpath := ""
 
-	serve, _ := mv(r, "serve", false)
-	if serve == "true" {
-		directServe = true
-	}
+	id, err := mv(r, "id", false)
+	if err != nil {
+		//ID is not defined in the URL paramter. New ID defination is based on the subpath content
+		requestURI := filepath.ToSlash(filepath.Clean(r.URL.Path))
+		subpathElements = strings.Split(requestURI[1:], "/")
+		if len(subpathElements) == 2 {
+			//E.g. /share/{id} => Show the download page
+			id = subpathElements[1]
+
+			//Check if there is missing / at the end. Redirect if true
+			if r.URL.Path[len(r.URL.Path)-1:] != "/" {
+				http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
+				return
+			}
+
+		} else if len(subpathElements) >= 3 {
+			//E.g. /share/{id}/myfile.txt => Enter download mode
+			id = subpathElements[2]
+			if subpathElements[1] == "download" {
+				directDownload = true
+
+				//Check if this contain a subpath
+				if len(subpathElements) > 3 {
+					relpath = strings.Join(subpathElements[3:], "/")
+				}
+			} else if subpathElements[1] == "preview" {
+				directServe = true
+			}
 
-	relpath, _ := mv(r, "rel", false)
+			log.Println(len(subpathElements[3:]), subpathElements[3:], relpath)
+
+		} else {
+			http.NotFound(w, r)
+			return
+		}
+	} else {
+
+		//Parse and redirect to new share path
+		download, _ := mv(r, "download", false)
+		if download == "true" {
+			directDownload = true
+		}
+
+		serve, _ := mv(r, "serve", false)
+		if serve == "true" {
+			directServe = true
+		}
+
+		relpath, _ = mv(r, "rel", false)
+
+		redirectURL := "./" + id + "/"
+		if directDownload == true {
+			redirectURL = "./download/" + id + "/"
+		}
+		http.Redirect(w, r, redirectURL, http.StatusTemporaryRedirect)
+	}
 
 	//Check if id exists
 	val, ok := s.urlToFileMap.Load(id)
@@ -122,7 +166,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 					w.WriteHeader(http.StatusUnauthorized)
 					w.Write([]byte("401 - Unauthorized"))
 				} else {
-					http.Redirect(w, r, common.ConstructRelativePathFromRequestURL(r.RequestURI, "login.system")+"?redirect=/share?id="+id, 307)
+					http.Redirect(w, r, common.ConstructRelativePathFromRequestURL(r.RequestURI, "login.system")+"?redirect=/share/preview/?id="+id, 307)
 				}
 				return
 			} else {
@@ -135,7 +179,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 					w.WriteHeader(http.StatusUnauthorized)
 					w.Write([]byte("401 - Unauthorized"))
 				} else {
-					http.Redirect(w, r, common.ConstructRelativePathFromRequestURL(r.RequestURI, "login.system")+"?redirect=/share?id="+id, 307)
+					http.Redirect(w, r, common.ConstructRelativePathFromRequestURL(r.RequestURI, "login.system")+"?redirect=/share/preview/?id="+id, 307)
 				}
 				return
 			}
@@ -175,7 +219,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 					w.WriteHeader(http.StatusUnauthorized)
 					w.Write([]byte("401 - Unauthorized"))
 				} else {
-					http.Redirect(w, r, common.ConstructRelativePathFromRequestURL(r.RequestURI, "login.system")+"?redirect=/share?id="+id, 307)
+					http.Redirect(w, r, common.ConstructRelativePathFromRequestURL(r.RequestURI, "login.system")+"?redirect=/share/"+id, 307)
 				}
 				return
 			}
@@ -201,7 +245,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 					w.WriteHeader(http.StatusUnauthorized)
 					w.Write([]byte("401 - Unauthorized"))
 				} else {
-					http.Redirect(w, r, common.ConstructRelativePathFromRequestURL(r.RequestURI, "login.system")+"?redirect=/share?id="+id, 307)
+					http.Redirect(w, r, common.ConstructRelativePathFromRequestURL(r.RequestURI, "login.system")+"?redirect=/share/"+id, 307)
 				}
 				return
 			}
@@ -359,7 +403,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 					"size":         filesystem.GetFileDisplaySize(fsize, 2),
 					"filecount":    strconv.Itoa(fcount),
 					"modtime":      timeString,
-					"downloadurl":  "./share?id=" + id + "&download=true",
+					"downloadurl":  "../../share/download/" + id,
 					"filename":     filepath.Base(shareOption.FileRealPath),
 					"reqtime":      strconv.Itoa(int(time.Now().Unix())),
 					"treelist":     tl,
@@ -426,16 +470,23 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 				fmodtime, _ := filesystem.GetModTime(shareOption.FileRealPath)
 				timeString := time.Unix(fmodtime, 0).Format("02-01-2006 15:04:05")
 
+				downloadURI := "../../share/download/" + id + "/" + filepath.Base(shareOption.FileRealPath)
+
+				//Check if ext match with filepath ext
+				displayExt := ext
+				if ext != filepath.Ext(shareOption.FileRealPath) {
+					displayExt = filepath.Ext(shareOption.FileRealPath) + " (" + ext + ")"
+				}
 				t := fasttemplate.New(string(content), "{{", "}}")
 				s := t.ExecuteString(map[string]interface{}{
 					"hostname":    s.options.HostName,
 					"reqid":       id,
 					"mime":        mime,
-					"ext":         ext,
+					"ext":         displayExt,
 					"size":        filesystem.GetFileDisplaySize(fsize, 2),
 					"modtime":     timeString,
-					"downloadurl": "/share?id=" + id + "&download=true",
-					"preview_url": "/share?id=" + id + "&serve=true",
+					"downloadurl": downloadURI,
+					"preview_url": "/share/preview/?id=" + id + "&serve=true",
 					"filename":    filepath.Base(shareOption.FileRealPath),
 					"reqtime":     strconv.Itoa(int(time.Now().Unix())),
 				})

+ 4 - 4
system/share/downloadPage.html

@@ -4,10 +4,10 @@
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
     <title>{{hostname}} File Share</title>
-    <link rel="stylesheet" href="script/skeleton/offline.css">
-    <link rel="stylesheet" href="script/skeleton/normalize.css">
-    <link rel="stylesheet" href="script/skeleton/skeleton.css">
-    <script type="application/javascript" src="script/jquery.min.js"></script>
+    <link rel="stylesheet" href="../../script/skeleton/offline.css">
+    <link rel="stylesheet" href="../../script/skeleton/normalize.css">
+    <link rel="stylesheet" href="../../script/skeleton/skeleton.css">
+    <script type="application/javascript" src="../../script/jquery.min.js"></script>
     <style>
 
         body{

+ 11 - 7
system/share/downloadPageFolder.html

@@ -4,10 +4,10 @@
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
     <title>{{hostname}} File Share</title>
-    <link rel="stylesheet" href="script/skeleton/offline.css">
-    <link rel="stylesheet" href="script/skeleton/normalize.css">
-    <link rel="stylesheet" href="script/skeleton/skeleton.css">
-    <script type="application/javascript" src="script/jquery.min.js"></script>
+    <link rel="stylesheet" href="../../script/skeleton/offline.css">
+    <link rel="stylesheet" href="../../script/skeleton/normalize.css">
+    <link rel="stylesheet" href="../../script/skeleton/skeleton.css">
+    <script type="application/javascript" src="../../script/jquery.min.js"></script>
     <style>
 
         body{
@@ -177,10 +177,14 @@
             }
 
             displayName =  icon + " " + file.Filename;
-            
+          }
+
+          var filenameLinker = `<a href="../../share/download/${downloadUUID}/${file.RelPath}">${displayName}</a>`;
+          if (file.IsDir == true){
+            filenameLinker = `${displayName}`;
           }
           $("#folderList").append(`<tr class="fileobject noselect" onclick="highlightThis(this);" filename="${file.Filename}" relpath="${file.RelPath}" type="${filetype.toLocaleLowerCase()}" ondblclick="event.preventDefault(); openThis(this);">
-              <td style="padding-left: 8px;">${displayName}</td>
+              <td style="padding-left: 8px;">${filenameLinker}</td>
               <td>${filetype}</td>
               <td>${file.Filesize}</td>
             </tr>`);
@@ -231,7 +235,7 @@
           
         }else{
           //File. Download it
-          window.open("./share?id=" + downloadUUID + "&download=true&rel=" + targetRelPath)
+          window.open("../../share/download/" + downloadUUID + "/" + targetRelPath)
         }
       }
 

+ 4 - 4
system/share/notfound.html

@@ -4,10 +4,10 @@
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
     <title>{{hostname}} File Share</title>
-    <link rel="stylesheet" href="script/skeleton/offline.css">
-    <link rel="stylesheet" href="script/skeleton/normalize.css">
-    <link rel="stylesheet" href="script/skeleton/skeleton.css">
-    <script type="application/javascript" src="script/jquery.min.js"></script>
+    <link rel="stylesheet" href="../../script/skeleton/offline.css">
+    <link rel="stylesheet" href="../../script/skeleton/normalize.css">
+    <link rel="stylesheet" href="../../script/skeleton/skeleton.css">
+    <script type="application/javascript" src="../../script/jquery.min.js"></script>
     <style>
         .bar{
             height: 12px;

+ 4 - 4
system/share/permissionDenied.html

@@ -4,10 +4,10 @@
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
     <title>Permission Denied</title>
-    <link rel="stylesheet" href="script/skeleton/offline.css">
-    <link rel="stylesheet" href="script/skeleton/normalize.css">
-    <link rel="stylesheet" href="script/skeleton/skeleton.css">
-    <script type="application/javascript" src="script/jquery.min.js"></script>
+    <link rel="stylesheet" href="../../script/skeleton/offline.css">
+    <link rel="stylesheet" href="../../script/skeleton/normalize.css">
+    <link rel="stylesheet" href="../../script/skeleton/skeleton.css">
+    <script type="application/javascript" src="../../script/jquery.min.js"></script>
     <style>
         .bar{
             height: 12px;

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

@@ -3708,7 +3708,7 @@
 
                             //Continue to render QR Code and UI contents
                             $("#qrcode").html("");
-                            var shareURL = protocol + window.location.hostname + ":" + window.location.port + "/share?id=" + data.UUID;
+                            var shareURL = protocol + window.location.hostname + ":" + window.location.port + "/share/" + data.UUID;
                             shareEditingUUID = data.UUID;
                             new QRCode(document.getElementById("qrcode"), shareURL);
                             $("#sharelink").text(shareURL);

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

@@ -265,7 +265,7 @@
                 if (location.protocol !== 'https:') {
                     protocol = "http://";
                 }
-                var shareURL = protocol + window.location.hostname + ":" + window.location.port + "/share?id=" + uuid;
+                var shareURL = protocol + window.location.hostname + ":" + window.location.port + "/share/" + uuid;
                 shareEditingUUID = uuid;
                 fileSharingURL = shareURL;
                 new QRCode(document.getElementById("qrcode"), shareURL);

+ 1 - 1
web/desktop.system

@@ -4744,7 +4744,7 @@
             if (location.protocol !== 'https:') {
                 protocol = "http://";
             }
-            var shareURL = protocol + window.location.hostname + ":" + window.location.port + "/share?id=" + payload.uuid;
+            var shareURL = protocol + window.location.hostname + ":" + window.location.port + "/share/" + payload.uuid;
 
             //Copy the text to clipboard
             const area = document.createElement('textarea');