github.com/keys-pub/mattermost-server@v4.10.10+incompatible/utils/file_backend_local.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package utils 5 6 import ( 7 "io/ioutil" 8 "net/http" 9 "os" 10 "path/filepath" 11 12 "github.com/mattermost/mattermost-server/mlog" 13 "github.com/mattermost/mattermost-server/model" 14 ) 15 16 const ( 17 TEST_FILE_PATH = "/testfile" 18 ) 19 20 type LocalFileBackend struct { 21 directory string 22 } 23 24 func (b *LocalFileBackend) TestConnection() *model.AppError { 25 f := []byte("testingwrite") 26 if err := writeFileLocally(f, filepath.Join(b.directory, TEST_FILE_PATH)); err != nil { 27 return model.NewAppError("TestFileConnection", "Don't have permissions to write to local path specified or other error.", nil, err.Error(), http.StatusInternalServerError) 28 } 29 os.Remove(filepath.Join(b.directory, TEST_FILE_PATH)) 30 mlog.Info("Able to write files to local storage.") 31 return nil 32 } 33 34 func (b *LocalFileBackend) ReadFile(path string) ([]byte, *model.AppError) { 35 if f, err := ioutil.ReadFile(filepath.Join(b.directory, path)); err != nil { 36 return nil, model.NewAppError("ReadFile", "api.file.read_file.reading_local.app_error", nil, err.Error(), http.StatusInternalServerError) 37 } else { 38 return f, nil 39 } 40 } 41 42 func (b *LocalFileBackend) CopyFile(oldPath, newPath string) *model.AppError { 43 if err := CopyFile(filepath.Join(b.directory, oldPath), filepath.Join(b.directory, newPath)); err != nil { 44 return model.NewAppError("copyFile", "api.file.move_file.rename.app_error", nil, err.Error(), http.StatusInternalServerError) 45 } 46 return nil 47 } 48 49 func (b *LocalFileBackend) MoveFile(oldPath, newPath string) *model.AppError { 50 if err := os.MkdirAll(filepath.Dir(filepath.Join(b.directory, newPath)), 0774); err != nil { 51 return model.NewAppError("moveFile", "api.file.move_file.rename.app_error", nil, err.Error(), http.StatusInternalServerError) 52 } 53 54 if err := os.Rename(filepath.Join(b.directory, oldPath), filepath.Join(b.directory, newPath)); err != nil { 55 return model.NewAppError("moveFile", "api.file.move_file.rename.app_error", nil, err.Error(), http.StatusInternalServerError) 56 } 57 58 return nil 59 } 60 61 func (b *LocalFileBackend) WriteFile(f []byte, path string) *model.AppError { 62 return writeFileLocally(f, filepath.Join(b.directory, path)) 63 } 64 65 func writeFileLocally(f []byte, path string) *model.AppError { 66 if err := os.MkdirAll(filepath.Dir(path), 0774); err != nil { 67 directory, _ := filepath.Abs(filepath.Dir(path)) 68 return model.NewAppError("WriteFile", "api.file.write_file_locally.create_dir.app_error", nil, "directory="+directory+", err="+err.Error(), http.StatusInternalServerError) 69 } 70 71 if err := ioutil.WriteFile(path, f, 0644); err != nil { 72 return model.NewAppError("WriteFile", "api.file.write_file_locally.writing.app_error", nil, err.Error(), http.StatusInternalServerError) 73 } 74 75 return nil 76 } 77 78 func (b *LocalFileBackend) RemoveFile(path string) *model.AppError { 79 if err := os.Remove(filepath.Join(b.directory, path)); err != nil { 80 return model.NewAppError("RemoveFile", "utils.file.remove_file.local.app_error", nil, err.Error(), http.StatusInternalServerError) 81 } 82 return nil 83 } 84 85 func (b *LocalFileBackend) ListDirectory(path string) (*[]string, *model.AppError) { 86 var paths []string 87 if fileInfos, err := ioutil.ReadDir(filepath.Join(b.directory, path)); err != nil { 88 return nil, model.NewAppError("ListDirectory", "utils.file.list_directory.local.app_error", nil, err.Error(), http.StatusInternalServerError) 89 } else { 90 for _, fileInfo := range fileInfos { 91 if fileInfo.IsDir() { 92 paths = append(paths, filepath.Join(path, fileInfo.Name())) 93 } 94 } 95 } 96 return &paths, nil 97 } 98 99 func (b *LocalFileBackend) RemoveDirectory(path string) *model.AppError { 100 if err := os.RemoveAll(filepath.Join(b.directory, path)); err != nil { 101 return model.NewAppError("RemoveDirectory", "utils.file.remove_directory.local.app_error", nil, err.Error(), http.StatusInternalServerError) 102 } 103 return nil 104 }