github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/common/file.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package common 20 21 import ( 22 "io" 23 "log" 24 "os" 25 "os/exec" 26 "path/filepath" 27 "unicode/utf8" 28 ) 29 30 const ( 31 dataDir = "data" 32 fileStoragePath = "file_storage" 33 staticPath = "static" 34 depth = 3 35 ) 36 37 // GetFileSize ... 38 func GetFileSize(name string) (int64, error) { 39 file, err := os.Open(name) 40 if err != nil { 41 return 0, err 42 } 43 defer file.Close() 44 45 fi, err := file.Stat() 46 if err != nil { 47 return 0, err 48 } 49 50 return fi.Size(), nil 51 } 52 53 // GetFullPath ... 54 func GetFullPath(name string) string { 55 56 dir := filepath.Join(dataDir, fileStoragePath) 57 58 for i := 0; i < depth; i++ { 59 dir = filepath.Join(dir, name[i*3:(i+1)*3]) 60 } 61 62 return dir 63 } 64 65 // GetLinkPath ... 66 func GetLinkPath(name string) string { 67 68 dir := "/upload" 69 70 count := utf8.RuneCountInString(name) 71 if count < 9 { 72 return filepath.Join(dir, name) 73 } 74 75 for i := 0; i < depth; i++ { 76 dir = filepath.Join(dir, name[i*3:(i+1)*3]) 77 } 78 79 return filepath.Join(dir, name) 80 } 81 82 // StoragePath ... 83 func StoragePath() string { 84 return filepath.Join(dataDir, fileStoragePath) 85 } 86 87 // StaticPath ... 88 func StaticPath() string { 89 return filepath.Join(dataDir, staticPath) 90 } 91 92 // FileExist ... 93 func FileExist(path string) (exist bool) { 94 95 if _, err := os.Stat(path); err != nil { 96 if os.IsNotExist(err) { 97 // file does not exist 98 } else { 99 // other error 100 } 101 return 102 } 103 104 exist = true 105 106 return 107 } 108 109 // CopyFile ... 110 func CopyFile(f, t string) { 111 112 from, err := os.Open(f) 113 if err != nil { 114 log.Fatal(err) 115 } 116 defer from.Close() 117 118 to, err := os.OpenFile(t, os.O_RDWR|os.O_CREATE, 0666) 119 if err != nil { 120 log.Fatal(err) 121 } 122 defer to.Close() 123 124 _, err = io.Copy(to, from) 125 if err != nil { 126 log.Fatal(err) 127 } 128 } 129 130 // FormatSourceCode ... 131 func FormatSourceCode(filename string) { 132 cmd := exec.Command("gofmt", "-w", filename) 133 if err := cmd.Run(); err != nil { 134 log.Fatalf("Error while running gofmt: %s", err.Error()) 135 } 136 }