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