|
@@ -86,25 +86,69 @@ func NewShareManager(options Options) *Manager {
|
|
|
|
|
|
//Main function for handle share. Must be called with http.HandleFunc (No auth)
|
|
//Main function for handle share. Must be called with http.HandleFunc (No auth)
|
|
func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
|
|
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
|
|
directDownload := false
|
|
directServe := 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
|
|
//Check if id exists
|
|
val, ok := s.urlToFileMap.Load(id)
|
|
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.WriteHeader(http.StatusUnauthorized)
|
|
w.Write([]byte("401 - Unauthorized"))
|
|
w.Write([]byte("401 - Unauthorized"))
|
|
} else {
|
|
} 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
|
|
return
|
|
} else {
|
|
} else {
|
|
@@ -135,7 +179,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
w.Write([]byte("401 - Unauthorized"))
|
|
w.Write([]byte("401 - Unauthorized"))
|
|
} else {
|
|
} 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
|
|
return
|
|
}
|
|
}
|
|
@@ -175,7 +219,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
w.Write([]byte("401 - Unauthorized"))
|
|
w.Write([]byte("401 - Unauthorized"))
|
|
} else {
|
|
} 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
|
|
return
|
|
}
|
|
}
|
|
@@ -201,7 +245,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
w.Write([]byte("401 - Unauthorized"))
|
|
w.Write([]byte("401 - Unauthorized"))
|
|
} else {
|
|
} 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
|
|
return
|
|
}
|
|
}
|
|
@@ -359,7 +403,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
|
|
"size": filesystem.GetFileDisplaySize(fsize, 2),
|
|
"size": filesystem.GetFileDisplaySize(fsize, 2),
|
|
"filecount": strconv.Itoa(fcount),
|
|
"filecount": strconv.Itoa(fcount),
|
|
"modtime": timeString,
|
|
"modtime": timeString,
|
|
- "downloadurl": "./share?id=" + id + "&download=true",
|
|
|
|
|
|
+ "downloadurl": "../../share/download/" + id,
|
|
"filename": filepath.Base(shareOption.FileRealPath),
|
|
"filename": filepath.Base(shareOption.FileRealPath),
|
|
"reqtime": strconv.Itoa(int(time.Now().Unix())),
|
|
"reqtime": strconv.Itoa(int(time.Now().Unix())),
|
|
"treelist": tl,
|
|
"treelist": tl,
|
|
@@ -426,16 +470,23 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
|
|
fmodtime, _ := filesystem.GetModTime(shareOption.FileRealPath)
|
|
fmodtime, _ := filesystem.GetModTime(shareOption.FileRealPath)
|
|
timeString := time.Unix(fmodtime, 0).Format("02-01-2006 15:04:05")
|
|
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), "{{", "}}")
|
|
t := fasttemplate.New(string(content), "{{", "}}")
|
|
s := t.ExecuteString(map[string]interface{}{
|
|
s := t.ExecuteString(map[string]interface{}{
|
|
"hostname": s.options.HostName,
|
|
"hostname": s.options.HostName,
|
|
"reqid": id,
|
|
"reqid": id,
|
|
"mime": mime,
|
|
"mime": mime,
|
|
- "ext": ext,
|
|
|
|
|
|
+ "ext": displayExt,
|
|
"size": filesystem.GetFileDisplaySize(fsize, 2),
|
|
"size": filesystem.GetFileDisplaySize(fsize, 2),
|
|
"modtime": timeString,
|
|
"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),
|
|
"filename": filepath.Base(shareOption.FileRealPath),
|
|
"reqtime": strconv.Itoa(int(time.Now().Unix())),
|
|
"reqtime": strconv.Itoa(int(time.Now().Unix())),
|
|
})
|
|
})
|