Bladeren bron

Fixed opg and smbfs bug in share

Toby Chui 3 jaren geleden
bovenliggende
commit
da85013ced
2 gewijzigde bestanden met toevoegingen van 49 en 19 verwijderingen
  1. 4 0
      mod/filesystem/filesystem.go
  2. 45 19
      mod/share/share.go

+ 4 - 0
mod/filesystem/filesystem.go

@@ -234,6 +234,10 @@ func NewFileSystemHandler(option FileSystemOption) (*FileSystemHandler, error) {
 	return nil, errors.New("Not supported file system: " + fstype)
 }
 
+func (fsh *FileSystemHandler) IsNetworkDrive() bool {
+	return arozfs.IsNetworkDrive(fsh.Filesystem)
+}
+
 //Check if a fsh is virtual (e.g. Network or fs Abstractions that cannot be listed with normal fs API)
 /*
 func (fsh *FileSystemHandler) IsVirtual() bool {

+ 45 - 19
mod/share/share.go

@@ -121,11 +121,50 @@ func (s *Manager) HandleOPGServing(w http.ResponseWriter, r *http.Request, share
 	filename := arozfs.Base(shareEntry.FileRealPath)
 	filenameOnly := strings.TrimSuffix(filename, filepath.Ext(filename))
 
-	fs := filesystem.GetFileSize(shareEntry.FileRealPath)
+	//Get the file information from target fsh
+	ownerinfo, err := s.options.UserHandler.GetUserInfoFromUsername(shareEntry.Owner)
+	if err != nil {
+		fmt.Println("[share/opg] " + err.Error())
+		http.NotFound(w, r)
+		return
+	}
+
+	fsh, err := ownerinfo.GetFileSystemHandlerFromVirtualPath(shareEntry.FileVirtualPath)
+	if err != nil {
+		fmt.Println("[share/opg] " + err.Error())
+		http.NotFound(w, r)
+		return
+	}
+
+	fs := fsh.FileSystemAbstraction.GetFileSize(shareEntry.FileRealPath)
 	shareMeta := filepath.Ext(shareEntry.FileRealPath) + " / " + filesystem.GetFileDisplaySize(fs, 2)
-	if isDir(shareEntry.FileRealPath) {
-		fs, fc := filesystem.GetDirctorySize(shareEntry.FileRealPath, false)
-		shareMeta = strconv.Itoa(fc) + " items / " + filesystem.GetFileDisplaySize(fs, 2)
+	if fsh.FileSystemAbstraction.IsDir(shareEntry.FileRealPath) {
+		if fsh.IsNetworkDrive() {
+			fileCount := 0
+			folderCount := 0
+			dirEntries, _ := fsh.FileSystemAbstraction.ReadDir(shareEntry.FileRealPath)
+			for _, di := range dirEntries {
+				if di.IsDir() {
+					folderCount++
+				} else {
+					fileCount++
+				}
+			}
+			shareMeta = strconv.Itoa(fileCount) + " File"
+			if (fileCount) > 1 {
+				shareMeta += "s"
+			}
+			if folderCount > 0 {
+				shareMeta += " / " + strconv.Itoa(folderCount) + " Subfolder"
+				if folderCount > 1 {
+					shareMeta += "s"
+				}
+			}
+		} else {
+			fs, fc := filesystem.GetDirctorySize(shareEntry.FileRealPath, false)
+			shareMeta = strconv.Itoa(fc) + " items / " + filesystem.GetFileDisplaySize(fs, 2)
+		}
+
 	}
 
 	if len([]rune(filename)) > 20 {
@@ -177,24 +216,11 @@ func (s *Manager) HandleOPGServing(w http.ResponseWriter, r *http.Request, share
 	}
 
 	//Get thumbnail
-	ownerinfo, err := s.options.UserHandler.GetUserInfoFromUsername(shareEntry.Owner)
-	if err != nil {
-		fmt.Println("[share/opg] " + err.Error())
-		http.NotFound(w, r)
-		return
-	}
-	fsh, err := ownerinfo.GetFileSystemHandlerFromVirtualPath(shareEntry.FileVirtualPath)
-	if err != nil {
-		fmt.Println("[share/opg] " + err.Error())
-		http.NotFound(w, r)
-		return
-	}
-
 	rpath, _ := fsh.FileSystemAbstraction.VirtualPathToRealPath(shareEntry.FileVirtualPath, shareEntry.Owner)
 	cacheFileImagePath, err := metadata.GetCacheFilePath(fsh, rpath)
 	if err == nil {
 		//We got a thumbnail for this file. Render it as well
-		thumbnailFile, err := os.Open(cacheFileImagePath)
+		thumbnailFile, err := fsh.FileSystemAbstraction.ReadStream(cacheFileImagePath)
 		if err != nil {
 			fmt.Println("[share/opg] " + err.Error())
 			http.NotFound(w, r)
@@ -514,7 +540,7 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 					//Validate the absolute path to prevent path escape
 					reqPath := filepath.ToSlash(filepath.Clean(targetFilepath))
 					rootPath, _ := targetFshAbs.VirtualPathToRealPath(shareOption.FileVirtualPath, shareOption.Owner)
-					if !strings.HasPrefix(reqPath, rootPath) {
+					if !strings.HasPrefix(arozfs.ToSlash(reqPath), arozfs.ToSlash(rootPath)) {
 						//Directory escape detected
 						w.WriteHeader(http.StatusBadRequest)
 						w.Write([]byte("400 - Bad Request: Invalid relative path"))