From 0ff010cc946cb637e9a99f9993fbc99699e4fa61 Mon Sep 17 00:00:00 2001 From: Manuel Reithuber Date: Mon, 7 Jun 2021 01:15:07 +0200 Subject: [PATCH] added vfs.GetProviderByName(), using it in for sftpgo portable and for parsing the webadmin form field --- cmd/portable.go | 18 +++++++++--------- httpd/webadmin.go | 6 +----- vfs/filesystem.go | 23 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/cmd/portable.go b/cmd/portable.go index c5d2c614..50762395 100644 --- a/cmd/portable.go +++ b/cmd/portable.go @@ -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", "", "") diff --git a/httpd/webadmin.go b/httpd/webadmin.go index cc8deb62..7bff2f42 100644 --- a/httpd/webadmin.go +++ b/httpd/webadmin.go @@ -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) diff --git a/vfs/filesystem.go b/vfs/filesystem.go index 020338ba..cfcea058 100644 --- a/vfs/filesystem.go +++ b/vfs/filesystem.go @@ -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 {