Explorar o código

Work in progress adding folder travel function in folder sharing page

tobychui %!s(int64=4) %!d(string=hai) anos
pai
achega
77419f09ba
Modificáronse 2 ficheiros con 74 adicións e 17 borrados
  1. 45 15
      mod/share/share.go
  2. 29 2
      system/share/downloadPageFolder.html

+ 45 - 15
mod/share/share.go

@@ -26,7 +26,7 @@ import (
 	uuid "github.com/satori/go.uuid"
 	"imuslab.com/arozos/mod/auth"
 	"imuslab.com/arozos/mod/database"
-	fs "imuslab.com/arozos/mod/filesystem"
+	filesystem "imuslab.com/arozos/mod/filesystem"
 	"imuslab.com/arozos/mod/user"
 )
 
@@ -252,7 +252,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 				targetZipFilename := filepath.Join(tmpFolder, filepath.Base(shareOption.FileRealPath)) + ".zip"
 
 				//Build a filelist
-				err := fs.ArozZipFile([]string{shareOption.FileRealPath}, targetZipFilename, false)
+				err := filesystem.ArozZipFile([]string{shareOption.FileRealPath}, targetZipFilename, false)
 				if err != nil {
 					//Failed to create zip file
 					w.WriteHeader(http.StatusInternalServerError)
@@ -275,33 +275,62 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 				}
 
 				//Get file size
-				fsize, fcount := fs.GetDirctorySize(shareOption.FileRealPath, false)
+				fsize, fcount := filesystem.GetDirctorySize(shareOption.FileRealPath, false)
 
 				//Build the filelist of the root folder
-				rawFilelist, _ := fs.WGlob(filepath.Clean(shareOption.FileRealPath) + "/*")
+				rawFilelist, _ := filesystem.WGlob(filepath.Clean(shareOption.FileRealPath) + "/*")
 
 				rootVisableFiles := []File{}
 				for _, file := range rawFilelist {
 					if filepath.Base(file)[:1] != "." {
 						//Not hidden folder.
-						fileSize := fs.GetFileSize(file)
-						if fs.IsDir(file) {
-							fileSize, _ = fs.GetDirctorySize(file, false)
+						fileSize := filesystem.GetFileSize(file)
+						if filesystem.IsDir(file) {
+							fileSize, _ = filesystem.GetDirctorySize(file, false)
 						}
 
 						rootVisableFiles = append(rootVisableFiles, File{
 							Filename: filepath.Base(file),
 							RelPath:  "/",
-							Filesize: fs.GetFileDisplaySize(fileSize, 2),
-							IsDir:    fs.IsDir(file),
+							Filesize: filesystem.GetFileDisplaySize(fileSize, 2),
+							IsDir:    filesystem.IsDir(file),
 						})
 					}
 				}
 
+				//Build the tree list of the folder
+				treeList := []File{}
+				err = filepath.Walk(filepath.Clean(shareOption.FileRealPath), func(file string, info os.FileInfo, err error) error {
+					if err != nil {
+						//If error skip this
+						return nil
+					}
+					if filepath.Base(file)[:1] != "." {
+						fileSize := filesystem.GetFileSize(file)
+						if filesystem.IsDir(file) {
+							fileSize, _ = filesystem.GetDirctorySize(file, false)
+						}
+
+						relPath, err := filepath.Rel(shareOption.FileRealPath, file)
+						if err != nil {
+							relPath = "/"
+						}
+
+						treeList = append(treeList, File{
+							Filename: filepath.Base(file),
+							RelPath:  filepath.ToSlash(relPath),
+							Filesize: filesystem.GetFileDisplaySize(fileSize, 2),
+							IsDir:    filesystem.IsDir(file),
+						})
+					}
+					return nil
+				})
+
 				js, _ := json.Marshal(rootVisableFiles)
+				tl, _ := json.Marshal(treeList)
 
 				//Get modification time
-				fmodtime, _ := fs.GetModTime(shareOption.FileRealPath)
+				fmodtime, _ := filesystem.GetModTime(shareOption.FileRealPath)
 				timeString := time.Unix(fmodtime, 0).Format("02-01-2006 15:04:05")
 
 				t := fasttemplate.New(string(content), "{{", "}}")
@@ -309,13 +338,14 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 					"hostname":    s.options.HostName,
 					"reqid":       id,
 					"mime":        "application/x-directory",
-					"size":        fs.GetFileDisplaySize(fsize, 2),
+					"size":        filesystem.GetFileDisplaySize(fsize, 2),
 					"filecount":   strconv.Itoa(fcount),
 					"modtime":     timeString,
 					"downloadurl": "/share?id=" + id + "&download=true",
 					"filename":    filepath.Base(shareOption.FileRealPath),
 					"reqtime":     strconv.Itoa(int(time.Now().Unix())),
 					"filelist":    js,
+					"treelist":    tl,
 				})
 
 				w.Write([]byte(s))
@@ -340,7 +370,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 				}
 
 				//Get file mime type
-				mime, ext, err := fs.GetMime(shareOption.FileRealPath)
+				mime, ext, err := filesystem.GetMime(shareOption.FileRealPath)
 				if err != nil {
 					mime = "Unknown"
 				}
@@ -370,10 +400,10 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 				content = []byte(strings.ReplaceAll(string(content), "{{previewer}}", string(tp)))
 
 				//Get file size
-				fsize := fs.GetFileSize(shareOption.FileRealPath)
+				fsize := filesystem.GetFileSize(shareOption.FileRealPath)
 
 				//Get modification time
-				fmodtime, _ := fs.GetModTime(shareOption.FileRealPath)
+				fmodtime, _ := filesystem.GetModTime(shareOption.FileRealPath)
 				timeString := time.Unix(fmodtime, 0).Format("02-01-2006 15:04:05")
 
 				t := fasttemplate.New(string(content), "{{", "}}")
@@ -382,7 +412,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 					"reqid":       id,
 					"mime":        mime,
 					"ext":         ext,
-					"size":        fs.GetFileDisplaySize(fsize, 2),
+					"size":        filesystem.GetFileDisplaySize(fsize, 2),
 					"modtime":     timeString,
 					"downloadurl": "/share?id=" + id + "&download=true",
 					"preview_url": "/share?id=" + id + "&serve=true",

+ 29 - 2
system/share/downloadPageFolder.html

@@ -28,6 +28,13 @@
             padding: 20px;
             color: white;
         }
+
+        .fileobject{
+          cursor: pointer;
+        }
+        .fileobject:hover{
+          background-color: #f5f5f5;
+        }
     </style>
     </head>
     <body>
@@ -93,23 +100,43 @@
         </div>
     <script>
       var rootFileList = {{filelist}};
+      var treeFileList = {{treelist}};
       renderFileList(rootFileList);
 
       function renderFileList(filelist){
         $("#folderList").html("");
         filelist.forEach(file => {
           var filetype = "File";
+          var displayName = "";
           if (file.IsDir == true){
             filetype = "Folder";
+            displayName = "📁 " + file.Filename;
+          }else{
+            displayName = "📄 " + file.Filename;
           }
-          $("#folderList").append(`<tr>
-              <td>${file.Filename}</td>
+          $("#folderList").append(`<tr class="fileobject" filename="${file.Filename}" relpath="${file.RelPath}" type="${filetype.toLocaleLowerCase()}" ondblclick="event.preventDefault(); openThis(this);">
+              <td style="padding-left: 8px;">${displayName}</td>
               <td>${filetype}</td>
               <td>${file.Filesize}</td>
             </tr>`);
           });
       }
 
+      function openThis(object){
+        var targetFilename = $(object).attr("filename");
+        var targetType = $(object).attr("type");
+        var targetRelPath = $(object).attr("relpath");
+        var fileRelPathToShareRoot = targetRelPath + targetFilename;
+        
+        alert("WIP");
+        if (targetType == "folder"){
+          //Folder. Travel it
+         
+        }else{
+          //File. Download it
+        }
+      }
+
       resizeDOMElement();
       function resizeDOMElement(){
         $("#filelistWrapper").css({