summaryrefslogtreecommitdiffstats
path: root/app/file.go
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2017-08-18 12:22:29 -0700
committerGitHub <noreply@github.com>2017-08-18 12:22:29 -0700
commit17098fb2a34c3e5ee12bdd64c0f8954532554831 (patch)
tree885ede19dfafa4983c53be051b367ea0a899aa6a /app/file.go
parentb60cc5b28e2845dd7b1bab8da570e377fe16a09b (diff)
downloadchat-17098fb2a34c3e5ee12bdd64c0f8954532554831.tar.gz
chat-17098fb2a34c3e5ee12bdd64c0f8954532554831.tar.bz2
chat-17098fb2a34c3e5ee12bdd64c0f8954532554831.zip
Adding check for file write or minio/s3 bucket before server start. (#7254)
Diffstat (limited to 'app/file.go')
-rw-r--r--app/file.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/app/file.go b/app/file.go
index 23e28c9ce..dc7caff41 100644
--- a/app/file.go
+++ b/app/file.go
@@ -57,6 +57,8 @@ const (
IMAGE_THUMBNAIL_PIXEL_WIDTH = 120
IMAGE_THUMBNAIL_PIXEL_HEIGHT = 100
IMAGE_PREVIEW_PIXEL_WIDTH = 1024
+
+ TEST_FILE_PATH = "/testfile"
)
// Similar to s3.New() but allows initialization of signature v2 or signature v4 client.
@@ -74,6 +76,49 @@ func s3New(endpoint, accessKey, secretKey string, secure bool, signV2 bool, regi
return s3.NewWithCredentials(endpoint, creds, secure, region)
}
+func TestFileConnection() *model.AppError {
+ if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 {
+ endpoint := utils.Cfg.FileSettings.AmazonS3Endpoint
+ accessKey := utils.Cfg.FileSettings.AmazonS3AccessKeyId
+ secretKey := utils.Cfg.FileSettings.AmazonS3SecretAccessKey
+ secure := *utils.Cfg.FileSettings.AmazonS3SSL
+ signV2 := *utils.Cfg.FileSettings.AmazonS3SignV2
+ region := utils.Cfg.FileSettings.AmazonS3Region
+ bucket := utils.Cfg.FileSettings.AmazonS3Bucket
+
+ s3Clnt, err := s3New(endpoint, accessKey, secretKey, secure, signV2, region)
+ if err != nil {
+ return model.NewLocAppError("TestFileConnection", "Bad connection to S3 or minio.", nil, err.Error())
+ }
+
+ exists, err := s3Clnt.BucketExists(bucket)
+ if err != nil {
+ return model.NewLocAppError("TestFileConnection", "Error checking if bucket exists.", nil, err.Error())
+ }
+
+ if !exists {
+ l4g.Warn("Bucket specified does not exist. Attempting to create...")
+ err := s3Clnt.MakeBucket(bucket, region)
+ if err != nil {
+ l4g.Error("Unable to create bucket.")
+ return model.NewAppError("TestFileConnection", "Unable to create bucket", nil, err.Error(), http.StatusInternalServerError)
+ }
+ }
+ l4g.Info("Connection to S3 or minio is good. Bucket exists.")
+ } else if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_LOCAL {
+ f := []byte("testingwrite")
+ if err := writeFileLocally(f, utils.Cfg.FileSettings.Directory+TEST_FILE_PATH); err != nil {
+ return model.NewAppError("TestFileConnection", "Don't have permissions to write to local path specified or other error.", nil, err.Error(), http.StatusInternalServerError)
+ }
+ os.Remove(utils.Cfg.FileSettings.Directory + TEST_FILE_PATH)
+ l4g.Info("Able to write files to local storage.")
+ } else {
+ return model.NewLocAppError("TestFileConnection", "No file driver selected.", nil, "")
+ }
+
+ return nil
+}
+
func ReadFile(path string) ([]byte, *model.AppError) {
if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 {
endpoint := utils.Cfg.FileSettings.AmazonS3Endpoint