Commit 387f824c authored by Eric's avatar Eric Committed by Eric_Lee

[update] support sftp home dir

parent b9394148
...@@ -116,7 +116,6 @@ func (fs *sftpHandler) Filelist(r *sftp.Request) (sftp.ListerAt, error) { ...@@ -116,7 +116,6 @@ func (fs *sftpHandler) Filelist(r *sftp.Request) (sftp.ListerAt, error) {
if !ok { if !ok {
return nil, sftp.ErrSshFxNoSuchFile return nil, sftp.ErrSshFxNoSuchFile
} }
realPath = sysUserDir.ParsePath(r.Filepath)
if !fs.validatePermission(hostDir.asset.ID, sysUserDir.systemUser.ID, model.ConnectAction) { if !fs.validatePermission(hostDir.asset.ID, sysUserDir.systemUser.ID, model.ConnectAction) {
return nil, sftp.ErrSshFxPermissionDenied return nil, sftp.ErrSshFxPermissionDenied
...@@ -127,10 +126,15 @@ func (fs *sftpHandler) Filelist(r *sftp.Request) (sftp.ListerAt, error) { ...@@ -127,10 +126,15 @@ func (fs *sftpHandler) Filelist(r *sftp.Request) (sftp.ListerAt, error) {
if err != nil { if err != nil {
return nil, sftp.ErrSshFxPermissionDenied return nil, sftp.ErrSshFxPermissionDenied
} }
sysUserDir.homeDirpath, err = client.Getwd()
if err != nil {
return nil, err
}
sysUserDir.client = client sysUserDir.client = client
sysUserDir.conn = conn sysUserDir.conn = conn
} }
realPath = sysUserDir.ParsePath(r.Filepath)
switch r.Method { switch r.Method {
case "List": case "List":
logger.Debug("List method") logger.Debug("List method")
...@@ -189,6 +193,10 @@ func (fs *sftpHandler) Filecmd(r *sftp.Request) (err error) { ...@@ -189,6 +193,10 @@ func (fs *sftpHandler) Filecmd(r *sftp.Request) (err error) {
if err != nil { if err != nil {
return sftp.ErrSshFxPermissionDenied return sftp.ErrSshFxPermissionDenied
} }
suDir.homeDirpath, err = client.Getwd()
if err != nil {
return err
}
suDir.client = client suDir.client = client
suDir.conn = conn suDir.conn = conn
} }
...@@ -267,6 +275,10 @@ func (fs *sftpHandler) Filewrite(r *sftp.Request) (io.WriterAt, error) { ...@@ -267,6 +275,10 @@ func (fs *sftpHandler) Filewrite(r *sftp.Request) (io.WriterAt, error) {
if err != nil { if err != nil {
return nil, sftp.ErrSshFxPermissionDenied return nil, sftp.ErrSshFxPermissionDenied
} }
suDir.homeDirpath, err = client.Getwd()
if err != nil {
return nil, err
}
suDir.client = client suDir.client = client
suDir.conn = conn suDir.conn = conn
} }
...@@ -324,6 +336,10 @@ func (fs *sftpHandler) Fileread(r *sftp.Request) (io.ReaderAt, error) { ...@@ -324,6 +336,10 @@ func (fs *sftpHandler) Fileread(r *sftp.Request) (io.ReaderAt, error) {
if err != nil { if err != nil {
return nil, sftp.ErrSshFxPermissionDenied return nil, sftp.ErrSshFxPermissionDenied
} }
suDir.homeDirpath, err = ftpClient.Getwd()
if err != nil {
return nil, err
}
suDir.client = ftpClient suDir.client = ftpClient
suDir.conn = client suDir.conn = client
} }
...@@ -416,13 +432,14 @@ func (h *HostNameDir) Sys() interface{} { ...@@ -416,13 +432,14 @@ func (h *HostNameDir) Sys() interface{} {
} }
type SysUserDir struct { type SysUserDir struct {
ID string ID string
prefix string prefix string
rootPath string rootPath string
systemUser *model.SystemUser systemUser *model.SystemUser
time time.Time time time.Time
client *sftp.Client homeDirpath string
conn *gossh.Client client *sftp.Client
conn *gossh.Client
} }
func (su *SysUserDir) Name() string { return su.systemUser.Name } func (su *SysUserDir) Name() string { return su.systemUser.Name }
...@@ -443,10 +460,15 @@ func (su *SysUserDir) Sys() interface{} { ...@@ -443,10 +460,15 @@ func (su *SysUserDir) Sys() interface{} {
func (su *SysUserDir) ParsePath(path string) string { func (su *SysUserDir) ParsePath(path string) string {
var realPath string var realPath string
realPath = strings.ReplaceAll(path, su.prefix, su.rootPath) fmt.Println("root path", su.rootPath)
switch strings.ToLower(su.rootPath) {
case "home", "~", "":
realPath = strings.ReplaceAll(path, su.prefix, su.homeDirpath)
default:
realPath = strings.ReplaceAll(path, su.prefix, su.rootPath)
}
logger.Debug("real path: ", realPath) logger.Debug("real path: ", realPath)
return realPath return realPath
} }
type FakeFile struct { type FakeFile struct {
......
...@@ -121,16 +121,21 @@ func (u *UserVolume) Info(path string) (elfinder.FileDir, error) { ...@@ -121,16 +121,21 @@ func (u *UserVolume) Info(path string) (elfinder.FileDir, error) {
return rest, os.ErrPermission return rest, os.ErrPermission
} }
realPath := sysUserVol.ParsePath(path)
if sysUserVol.client == nil { if sysUserVol.client == nil {
sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser) sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser)
if err != nil { if err != nil {
return rest, os.ErrPermission return rest, os.ErrPermission
} }
sysUserVol.homeDirpath, err = sftClient.Getwd()
if err != nil {
return rest, err
}
sysUserVol.client = sftClient sysUserVol.client = sftClient
sysUserVol.conn = conn sysUserVol.conn = conn
} }
realPath := sysUserVol.ParsePath(path)
dirname := filepath.Dir(path) dirname := filepath.Dir(path)
fileInfos, err := sysUserVol.client.Stat(realPath) fileInfos, err := sysUserVol.client.Stat(realPath)
if err != nil { if err != nil {
...@@ -194,16 +199,21 @@ func (u *UserVolume) List(path string) []elfinder.FileDir { ...@@ -194,16 +199,21 @@ func (u *UserVolume) List(path string) []elfinder.FileDir {
if !ok { if !ok {
return dirs return dirs
} }
realPath := sysUserVol.ParsePath(path)
if sysUserVol.client == nil { if sysUserVol.client == nil {
sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser) sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser)
if err != nil { if err != nil {
return dirs return dirs
} }
sysUserVol.homeDirpath, err = sftClient.Getwd()
if err != nil {
return dirs
}
sysUserVol.client = sftClient sysUserVol.client = sftClient
sysUserVol.conn = conn sysUserVol.conn = conn
} }
realPath := sysUserVol.ParsePath(path)
subFiles, err := sysUserVol.client.ReadDir(realPath) subFiles, err := sysUserVol.client.ReadDir(realPath)
if err != nil { if err != nil {
return dirs return dirs
...@@ -269,16 +279,21 @@ func (u *UserVolume) GetFile(path string) (reader io.ReadCloser, err error) { ...@@ -269,16 +279,21 @@ func (u *UserVolume) GetFile(path string) (reader io.ReadCloser, err error) {
return nil, os.ErrPermission return nil, os.ErrPermission
} }
realPath := sysUserVol.ParsePath(path)
if sysUserVol.client == nil { if sysUserVol.client == nil {
sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser) sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser)
if err != nil { if err != nil {
return nil, os.ErrPermission return nil, os.ErrPermission
} }
sysUserVol.homeDirpath, err = sftClient.Getwd()
if err != nil {
return nil, err
}
sysUserVol.client = sftClient sysUserVol.client = sftClient
sysUserVol.conn = conn sysUserVol.conn = conn
} }
realPath := sysUserVol.ParsePath(path)
logData := &model.FTPLog{ logData := &model.FTPLog{
User: fmt.Sprintf("%s (%s)", u.user.Name, u.user.Username), User: fmt.Sprintf("%s (%s)", u.user.Name, u.user.Username),
Hostname: hostVol.asset.Hostname, Hostname: hostVol.asset.Hostname,
...@@ -334,16 +349,22 @@ func (u *UserVolume) UploadFile(dir, filename string, reader io.Reader) (elfinde ...@@ -334,16 +349,22 @@ func (u *UserVolume) UploadFile(dir, filename string, reader io.Reader) (elfinde
if !ok { if !ok {
return rest, os.ErrNotExist return rest, os.ErrNotExist
} }
realPath := sysUserVol.ParsePath(dir)
if sysUserVol.client == nil { if sysUserVol.client == nil {
sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser) sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser)
if err != nil { if err != nil {
return rest, os.ErrPermission return rest, os.ErrPermission
} }
sysUserVol.homeDirpath, err = sftClient.Getwd()
if err != nil {
return rest, err
}
sysUserVol.client = sftClient sysUserVol.client = sftClient
sysUserVol.conn = conn sysUserVol.conn = conn
} }
realPath := sysUserVol.ParsePath(dir)
realFilenamePath := filepath.Join(realPath, filename) realFilenamePath := filepath.Join(realPath, filename)
if !u.validatePermission(hostVol.asset.ID, sysUserVol.systemUser.ID, model.UploadAction) { if !u.validatePermission(hostVol.asset.ID, sysUserVol.systemUser.ID, model.UploadAction) {
return rest, os.ErrPermission return rest, os.ErrPermission
...@@ -436,16 +457,21 @@ func (u *UserVolume) MergeChunk(cid, total int, dirPath, filename string) (elfin ...@@ -436,16 +457,21 @@ func (u *UserVolume) MergeChunk(cid, total int, dirPath, filename string) (elfin
return rest, os.ErrPermission return rest, os.ErrPermission
} }
realDirPath := sysUserVol.ParsePath(dirPath)
if sysUserVol.client == nil { if sysUserVol.client == nil {
sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser) sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser)
if err != nil { if err != nil {
return rest, os.ErrPermission return rest, os.ErrPermission
} }
sysUserVol.homeDirpath, err = sftClient.Getwd()
if err != nil {
return rest, err
}
sysUserVol.client = sftClient sysUserVol.client = sftClient
sysUserVol.conn = conn sysUserVol.conn = conn
} }
realDirPath := sysUserVol.ParsePath(dirPath)
filenamePath := filepath.Join(realDirPath, filename) filenamePath := filepath.Join(realDirPath, filename)
logData := &model.FTPLog{ logData := &model.FTPLog{
User: fmt.Sprintf("%s (%s)", u.user.Name, u.user.Username), User: fmt.Sprintf("%s (%s)", u.user.Name, u.user.Username),
...@@ -536,15 +562,20 @@ func (u *UserVolume) MakeDir(dir, newDirname string) (elfinder.FileDir, error) { ...@@ -536,15 +562,20 @@ func (u *UserVolume) MakeDir(dir, newDirname string) (elfinder.FileDir, error) {
return rest, os.ErrPermission return rest, os.ErrPermission
} }
realPath := sysUserVol.ParsePath(dir)
if sysUserVol.client == nil { if sysUserVol.client == nil {
sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser) sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser)
if err != nil { if err != nil {
return rest, os.ErrPermission return rest, os.ErrPermission
} }
sysUserVol.homeDirpath, err = sftClient.Getwd()
if err != nil {
return rest, err
}
sysUserVol.client = sftClient sysUserVol.client = sftClient
sysUserVol.conn = conn sysUserVol.conn = conn
} }
realPath := sysUserVol.ParsePath(dir)
realDirPath := filepath.Join(realPath, newDirname) realDirPath := filepath.Join(realPath, newDirname)
err := sysUserVol.client.MkdirAll(realDirPath) err := sysUserVol.client.MkdirAll(realDirPath)
logData := &model.FTPLog{ logData := &model.FTPLog{
...@@ -604,16 +635,21 @@ func (u *UserVolume) MakeFile(dir, newFilename string) (elfinder.FileDir, error) ...@@ -604,16 +635,21 @@ func (u *UserVolume) MakeFile(dir, newFilename string) (elfinder.FileDir, error)
return rest, os.ErrPermission return rest, os.ErrPermission
} }
realPath := sysUserVol.ParsePath(dir)
if sysUserVol.client == nil { if sysUserVol.client == nil {
sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser) sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser)
if err != nil { if err != nil {
return rest, os.ErrPermission return rest, os.ErrPermission
} }
sysUserVol.homeDirpath, err = sftClient.Getwd()
if err != nil {
return rest, err
}
sysUserVol.client = sftClient sysUserVol.client = sftClient
sysUserVol.conn = conn sysUserVol.conn = conn
} }
realPath := sysUserVol.ParsePath(dir)
realFilePath := filepath.Join(realPath, newFilename) realFilePath := filepath.Join(realPath, newFilename)
_, err := sysUserVol.client.Create(realFilePath) _, err := sysUserVol.client.Create(realFilePath)
logData := &model.FTPLog{ logData := &model.FTPLog{
...@@ -667,16 +703,21 @@ func (u *UserVolume) Rename(oldNamePath, newName string) (elfinder.FileDir, erro ...@@ -667,16 +703,21 @@ func (u *UserVolume) Rename(oldNamePath, newName string) (elfinder.FileDir, erro
return rest, os.ErrPermission return rest, os.ErrPermission
} }
realPath := sysUserVol.ParsePath(oldNamePath)
if sysUserVol.client == nil { if sysUserVol.client == nil {
sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser) sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser)
if err != nil { if err != nil {
return rest, os.ErrPermission return rest, os.ErrPermission
} }
sysUserVol.homeDirpath, err = sftClient.Getwd()
if err != nil {
return rest, err
}
sysUserVol.client = sftClient sysUserVol.client = sftClient
sysUserVol.conn = conn sysUserVol.conn = conn
} }
realPath := sysUserVol.ParsePath(oldNamePath)
dirpath := filepath.Dir(realPath) dirpath := filepath.Dir(realPath)
newFilePath := filepath.Join(dirpath, newName) newFilePath := filepath.Join(dirpath, newName)
...@@ -737,16 +778,21 @@ func (u *UserVolume) Remove(path string) error { ...@@ -737,16 +778,21 @@ func (u *UserVolume) Remove(path string) error {
return os.ErrPermission return os.ErrPermission
} }
realPath := sysUserVol.ParsePath(path)
if sysUserVol.client == nil { if sysUserVol.client == nil {
sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser) sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser)
if err != nil { if err != nil {
return os.ErrPermission return os.ErrPermission
} }
sysUserVol.homeDirpath, err = sftClient.Getwd()
if err != nil {
return err
}
sysUserVol.client = sftClient sysUserVol.client = sftClient
sysUserVol.conn = conn sysUserVol.conn = conn
} }
realPath := sysUserVol.ParsePath(path)
logData := &model.FTPLog{ logData := &model.FTPLog{
User: fmt.Sprintf("%s (%s)", u.user.Name, u.user.Username), User: fmt.Sprintf("%s (%s)", u.user.Name, u.user.Username),
Hostname: hostVol.asset.Hostname, Hostname: hostVol.asset.Hostname,
...@@ -803,16 +849,20 @@ func (u *UserVolume) Paste(dir, filename, suffix string, reader io.ReadCloser) ( ...@@ -803,16 +849,20 @@ func (u *UserVolume) Paste(dir, filename, suffix string, reader io.ReadCloser) (
return rest, os.ErrPermission return rest, os.ErrPermission
} }
realPath := sysUserVol.ParsePath(dir)
if sysUserVol.client == nil { if sysUserVol.client == nil {
sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser) sftClient, conn, err := u.GetSftpClient(hostVol.asset, sysUserVol.systemUser)
if err != nil { if err != nil {
return rest, os.ErrPermission return rest, os.ErrPermission
} }
sysUserVol.homeDirpath, err = sftClient.Getwd()
if err != nil {
return rest, err
}
sysUserVol.client = sftClient sysUserVol.client = sftClient
sysUserVol.conn = conn sysUserVol.conn = conn
} }
realPath := sysUserVol.ParsePath(dir)
realFilePath := filepath.Join(realPath, filename) realFilePath := filepath.Join(realPath, filename)
_, err := sysUserVol.client.Stat(realFilePath) _, err := sysUserVol.client.Stat(realFilePath)
if err != nil { if err != nil {
...@@ -923,8 +973,9 @@ type sysUserVolume struct { ...@@ -923,8 +973,9 @@ type sysUserVolume struct {
rootPath string rootPath string
systemUser *model.SystemUser systemUser *model.SystemUser
client *sftp.Client homeDirpath string
conn *gossh.Client client *sftp.Client
conn *gossh.Client
} }
func (su *sysUserVolume) info() elfinder.FileDir { func (su *sysUserVolume) info() elfinder.FileDir {
...@@ -940,7 +991,13 @@ func (su *sysUserVolume) info() elfinder.FileDir { ...@@ -940,7 +991,13 @@ func (su *sysUserVolume) info() elfinder.FileDir {
} }
func (su *sysUserVolume) ParsePath(path string) string { func (su *sysUserVolume) ParsePath(path string) string {
realPath := strings.ReplaceAll(path, su.suPath, su.rootPath) var realPath string
switch strings.ToLower(su.rootPath) {
case "home", "~", "":
realPath = strings.ReplaceAll(path, su.suPath, su.homeDirpath)
default:
realPath = strings.ReplaceAll(path, su.suPath, su.rootPath)
}
logger.Debug("real path: ", realPath) logger.Debug("real path: ", realPath)
return realPath return realPath
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment