|
| 1 | +package template |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/Xhofe/alist/conf" |
| 5 | + "github.com/Xhofe/alist/drivers/base" |
| 6 | + "github.com/Xhofe/alist/model" |
| 7 | + "github.com/Xhofe/alist/utils" |
| 8 | + "io" |
| 9 | + "path" |
| 10 | + "path/filepath" |
| 11 | +) |
| 12 | + |
| 13 | +type SFTP struct { |
| 14 | +} |
| 15 | + |
| 16 | +func (driver SFTP) Config() base.DriverConfig { |
| 17 | + return base.DriverConfig{ |
| 18 | + Name: "SFTP", |
| 19 | + OnlyProxy: true, |
| 20 | + OnlyLocal: true, |
| 21 | + LocalSort: true, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func (driver SFTP) Items() []base.Item { |
| 26 | + // TODO fill need info |
| 27 | + return []base.Item{ |
| 28 | + { |
| 29 | + Name: "site_url", |
| 30 | + Label: "ip/host", |
| 31 | + Type: base.TypeString, |
| 32 | + Required: true, |
| 33 | + }, |
| 34 | + { |
| 35 | + Name: "limit", |
| 36 | + Label: "port", |
| 37 | + Type: base.TypeNumber, |
| 38 | + Required: true, |
| 39 | + Default: "22", |
| 40 | + }, |
| 41 | + { |
| 42 | + Name: "username", |
| 43 | + Label: "username", |
| 44 | + Type: base.TypeString, |
| 45 | + Required: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + Name: "password", |
| 49 | + Label: "password", |
| 50 | + Type: base.TypeString, |
| 51 | + Required: true, |
| 52 | + }, |
| 53 | + { |
| 54 | + Name: "root_folder", |
| 55 | + Label: "root folder path", |
| 56 | + Type: base.TypeString, |
| 57 | + Default: "/", |
| 58 | + Required: true, |
| 59 | + }, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func (driver SFTP) Save(account *model.Account, old *model.Account) error { |
| 64 | + if old != nil { |
| 65 | + clientsMap.Lock() |
| 66 | + defer clientsMap.Unlock() |
| 67 | + delete(clientsMap.clients, old.Name) |
| 68 | + } |
| 69 | + if account == nil { |
| 70 | + return nil |
| 71 | + } |
| 72 | + _, err := GetClient(account) |
| 73 | + if err != nil { |
| 74 | + account.Status = err.Error() |
| 75 | + } else { |
| 76 | + account.Status = "work" |
| 77 | + } |
| 78 | + _ = model.SaveAccount(account) |
| 79 | + return err |
| 80 | +} |
| 81 | + |
| 82 | +func (driver SFTP) File(path string, account *model.Account) (*model.File, error) { |
| 83 | + path = utils.ParsePath(path) |
| 84 | + if path == "/" { |
| 85 | + return &model.File{ |
| 86 | + Id: account.RootFolder, |
| 87 | + Name: account.Name, |
| 88 | + Size: 0, |
| 89 | + Type: conf.FOLDER, |
| 90 | + Driver: driver.Config().Name, |
| 91 | + UpdatedAt: account.UpdatedAt, |
| 92 | + }, nil |
| 93 | + } |
| 94 | + dir, name := filepath.Split(path) |
| 95 | + files, err := driver.Files(dir, account) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + for _, file := range files { |
| 100 | + if file.Name == name { |
| 101 | + return &file, nil |
| 102 | + } |
| 103 | + } |
| 104 | + return nil, base.ErrPathNotFound |
| 105 | +} |
| 106 | + |
| 107 | +func (driver SFTP) Files(path string, account *model.Account) ([]model.File, error) { |
| 108 | + path = utils.ParsePath(path) |
| 109 | + remotePath := utils.Join(account.RootFolder, path) |
| 110 | + cache, err := base.GetCache(path, account) |
| 111 | + if err == nil { |
| 112 | + files, _ := cache.([]model.File) |
| 113 | + return files, nil |
| 114 | + } |
| 115 | + client, err := GetClient(account) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + var files []model.File |
| 120 | + rawFiles, err := client.Files(remotePath) |
| 121 | + if err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + for i := 0; i < len(rawFiles); i++ { |
| 125 | + files = append(files, driver.formatFile(rawFiles[i])) |
| 126 | + } |
| 127 | + if len(files) > 0 { |
| 128 | + _ = base.SetCache(path, files, account) |
| 129 | + } |
| 130 | + return files, nil |
| 131 | +} |
| 132 | + |
| 133 | +func (driver SFTP) Link(args base.Args, account *model.Account) (*base.Link, error) { |
| 134 | + client, err := GetClient(account) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + remoteFileName := utils.Join(account.RootFolder, args.Path) |
| 139 | + remoteFile, err := client.Open(remoteFileName) |
| 140 | + if err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + return &base.Link{ |
| 144 | + Data: remoteFile, |
| 145 | + }, nil |
| 146 | +} |
| 147 | + |
| 148 | +func (driver SFTP) Path(path string, account *model.Account) (*model.File, []model.File, error) { |
| 149 | + path = utils.ParsePath(path) |
| 150 | + file, err := driver.File(path, account) |
| 151 | + if err != nil { |
| 152 | + return nil, nil, err |
| 153 | + } |
| 154 | + if !file.IsDir() { |
| 155 | + return file, nil, nil |
| 156 | + } |
| 157 | + files, err := driver.Files(path, account) |
| 158 | + if err != nil { |
| 159 | + return nil, nil, err |
| 160 | + } |
| 161 | + return nil, files, nil |
| 162 | +} |
| 163 | + |
| 164 | +func (driver SFTP) Preview(path string, account *model.Account) (interface{}, error) { |
| 165 | + //TODO preview interface if driver support |
| 166 | + return nil, base.ErrNotImplement |
| 167 | +} |
| 168 | + |
| 169 | +func (driver SFTP) MakeDir(path string, account *model.Account) error { |
| 170 | + client, err := GetClient(account) |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + return client.MkdirAll(utils.Join(account.RootFolder, path)) |
| 175 | +} |
| 176 | + |
| 177 | +func (driver SFTP) Move(src string, dst string, account *model.Account) error { |
| 178 | + return driver.Rename(src, dst, account) |
| 179 | +} |
| 180 | + |
| 181 | +func (driver SFTP) Rename(src string, dst string, account *model.Account) error { |
| 182 | + client, err := GetClient(account) |
| 183 | + if err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + return client.Rename(utils.Join(account.RootFolder, src), utils.Join(account.RootFolder, dst)) |
| 187 | +} |
| 188 | + |
| 189 | +func (driver SFTP) Copy(src string, dst string, account *model.Account) error { |
| 190 | + return base.ErrNotSupport |
| 191 | +} |
| 192 | + |
| 193 | +func (driver SFTP) Delete(path string, account *model.Account) error { |
| 194 | + client, err := GetClient(account) |
| 195 | + if err != nil { |
| 196 | + return err |
| 197 | + } |
| 198 | + return client.Remove(utils.Join(account.RootFolder, path)) |
| 199 | +} |
| 200 | + |
| 201 | +func (driver SFTP) Upload(file *model.FileStream, account *model.Account) error { |
| 202 | + if file == nil { |
| 203 | + return base.ErrEmptyFile |
| 204 | + } |
| 205 | + client, err := GetClient(account) |
| 206 | + if err != nil { |
| 207 | + return err |
| 208 | + } |
| 209 | + dstFile, err := client.Create(path.Join(account.RootFolder, file.ParentPath, file.Name)) |
| 210 | + if err != nil { |
| 211 | + return err |
| 212 | + } |
| 213 | + defer func() { |
| 214 | + _ = dstFile.Close() |
| 215 | + }() |
| 216 | + _, err = io.Copy(dstFile, file) |
| 217 | + return err |
| 218 | +} |
| 219 | + |
| 220 | +var _ base.Driver = (*SFTP)(nil) |
0 commit comments