added vfs.GetProviderByName(), using it in for sftpgo portable and for parsing the webadmin form field

This commit is contained in:
Manuel Reithuber 2021-06-07 01:15:07 +02:00 committed by Nicola Murino
parent 81aac15a6c
commit 0ff010cc94
3 changed files with 33 additions and 14 deletions

View file

@ -34,7 +34,7 @@ var (
portableSSHCommands []string
portableAllowedPatterns []string
portableDeniedPatterns []string
portableFsProvider int
portableFsProvider string
portableS3Bucket string
portableS3Region string
portableS3AccessKey string
@ -85,7 +85,7 @@ $ sftpgo portable
Please take a look at the usage below to customize the serving parameters`,
Run: func(cmd *cobra.Command, args []string) {
portableDir := directoryToServe
fsProvider := vfs.FilesystemProvider(portableFsProvider)
fsProvider := vfs.GetProviderByName(portableFsProvider)
if !filepath.IsAbs(portableDir) {
if fsProvider == vfs.LocalFilesystemProvider {
portableDir, _ = filepath.Abs(portableDir)
@ -151,7 +151,7 @@ Please take a look at the usage below to customize the serving parameters`,
HomeDir: portableDir,
Status: 1,
FsConfig: vfs.Filesystem{
Provider: vfs.FilesystemProvider(portableFsProvider),
Provider: vfs.GetProviderByName(portableFsProvider),
S3Config: vfs.S3FsConfig{
Bucket: portableS3Bucket,
Region: portableS3Region,
@ -259,12 +259,12 @@ multicast DNS`)
advertised via multicast DNS, this
flag allows to put username/password
inside the advertised TXT record`)
portableCmd.Flags().IntVarP(&portableFsProvider, "fs-provider", "f", int(vfs.LocalFilesystemProvider), `0 => local filesystem
1 => AWS S3 compatible
2 => Google Cloud Storage
3 => Azure Blob Storage
4 => Encrypted local filesystem
5 => SFTP`)
portableCmd.Flags().StringVarP(&portableFsProvider, "fs-provider", "f", "osfs", `osfs => local filesystem (legacy value: 0)
s3fs => AWS S3 compatible (legacy: 1)
gcsfs => Google Cloud Storage (legacy: 2)
azblobfs => Azure Blob Storage (legacy: 3)
cryptfs => Encrypted local filesystem (legacy: 4)
sftpfs => SFTP (legacy: 5)`)
portableCmd.Flags().StringVar(&portableS3Bucket, "s3-bucket", "", "")
portableCmd.Flags().StringVar(&portableS3Region, "s3-region", "", "")
portableCmd.Flags().StringVar(&portableS3AccessKey, "s3-access-key", "", "")

View file

@ -754,11 +754,7 @@ func getAzureConfig(r *http.Request) (vfs.AzBlobFsConfig, error) {
func getFsConfigFromPostFields(r *http.Request) (vfs.Filesystem, error) {
var fs vfs.Filesystem
provider, err := strconv.Atoi(r.Form.Get("fs_provider"))
if err != nil {
provider = int(vfs.LocalFilesystemProvider)
}
fs.Provider = vfs.FilesystemProvider(provider)
fs.Provider = vfs.GetProviderByName(r.Form.Get("fs_provider"))
switch fs.Provider {
case vfs.S3FilesystemProvider:
config, err := getS3Config(r)

View file

@ -20,6 +20,29 @@ const (
SFTPFilesystemProvider // SFTP
)
// GetProviderByName returns the FilesystemProvider matching a given name
//
// to provide backwards compatibility, numeric strings are accepted as well
func GetProviderByName(name string) FilesystemProvider {
switch name {
case "0", "osfs":
return LocalFilesystemProvider
case "1", "s3fs":
return S3FilesystemProvider
case "2", "gcsfs":
return GCSFilesystemProvider
case "3", "azblobfs":
return AzureBlobFilesystemProvider
case "4", "cryptfs":
return CryptedFilesystemProvider
case "5", "sftpfs":
return SFTPFilesystemProvider
}
// TODO think about returning an error value instead of silently defaulting to LocalFilesystemProvider
return LocalFilesystemProvider
}
// ValidatorHelper implements methods we need for Filesystem.ValidateConfig.
// It is implemented by vfs.Folder and dataprovider.User
type ValidatorHelper interface {