github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/os/default/file-directory.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 /* 4 dos abbreviations for "Default Operating System" 5 */ 6 package dos 7 8 import ( 9 goos "os" 10 "sort" 11 "strings" 12 13 "../../file" 14 "../../protocol" 15 ) 16 17 // FileDirectory use to store app needed data from repo like html, css, js, ... 18 type FileDirectory struct { 19 metadata fileDirectoryMetaData 20 parentDirectory *FileDirectory // Not nill if the directory has parent! 21 directories map[string]*FileDirectory // map key is dir name 22 files map[string]*File // map key is file name 23 } 24 25 func (dir *FileDirectory) MetaData() protocol.FileDirectoryMetaData { return &dir.metadata } 26 func (dir *FileDirectory) ParentDirectory() protocol.FileDirectory { return dir.parentDirectory } 27 28 // Mkdir make new directory in relative path from given directory. 29 func (dir *FileDirectory) Directories(offset, limit uint64) (dirs []protocol.FileDirectory) { 30 // TODO::: 31 return 32 } 33 34 // Directory return the directory by its name or make new one if desire name not exist. 35 func (dir *FileDirectory) Directory(name string) (dr protocol.FileDirectory, err protocol.Error) { 36 var goErr error 37 var exist bool 38 var dirPath = dir.metadata.uri.Path() + "/" + name + "/" 39 dr, exist = dir.directories[name] 40 if !exist { 41 goErr = goos.Mkdir(dirPath, 0700) 42 if goErr != nil { 43 // err = 44 return 45 } 46 } 47 if dr == nil { 48 var dir = FileDirectory{ 49 parentDirectory: dir, 50 } 51 err = dir.init(dirPath) 52 if err != nil { 53 return 54 } 55 dir.directories[name] = &dir 56 } 57 return 58 } 59 60 // Files use to get all files in order by name. 61 func (dir *FileDirectory) Files(offset, limit uint64) (files []protocol.File) { 62 var keys = make([]string, 0, len(dir.files)) 63 for k := range dir.files { 64 keys = append(keys, k) 65 } 66 sort.Strings(keys) 67 68 files = make([]protocol.File, len(dir.files)) 69 for i, k := range keys { 70 files[i] = dir.files[k] 71 } 72 return 73 } 74 75 // File use to get a file by its full name with extension 76 // And make new one if desire name not exist. 77 func (dir *FileDirectory) File(name string) (file protocol.File, err protocol.Error) { 78 file, _ = dir.files[name] 79 if file == nil { 80 // make new 81 var fi = File{ 82 parentDirectory: dir, 83 } 84 err = fi.init(name) 85 if err != nil { 86 return 87 } 88 dir.files[name] = &fi 89 } 90 return 91 } 92 93 // FileByPath use to get a file by its path in the directory 94 func (dir *FileDirectory) FileByPath(uriPath string) (file protocol.File, err protocol.Error) { 95 file, err = dir.fileByPath(uriPath) 96 return 97 } 98 99 func (dir *FileDirectory) fileByPath(uriPath string) (file *File, err protocol.Error) { 100 var pathParts = strings.Split(uriPath, "/") 101 var pathPartsLen = len(pathParts) 102 var fullFileName = pathParts[pathPartsLen-1] 103 104 if pathPartsLen < 3 { 105 file = dir.files[fullFileName] 106 return 107 } 108 109 if pathParts[0] == "" { 110 pathParts = pathParts[1:] 111 } 112 var directory *FileDirectory 113 directory, err = dir.directoryByPathParts(pathParts) 114 if directory == nil || err != nil { 115 return 116 } 117 file = directory.files[fullFileName] 118 return 119 } 120 121 // DirectoryByPath use to get a directory by its full path location. 122 // path is the file location in FileSystems include file name 123 func (dir *FileDirectory) DirectoryByPath(pathParts []string) (directory protocol.FileDirectory, err protocol.Error) { 124 directory, err = dir.directoryByPathParts(pathParts) 125 return 126 } 127 128 func (dir *FileDirectory) directoryByPath(uriPath string) (directory *FileDirectory, err protocol.Error) { 129 var pathParts = strings.Split(uriPath, "/") 130 if pathParts[0] == "" { 131 pathParts = pathParts[1:] 132 } 133 directory, err = dir.directoryByPathParts(pathParts) 134 return 135 } 136 137 func (dir *FileDirectory) directoryByPathParts(pathParts []string) (directory *FileDirectory, err protocol.Error) { 138 var pathPartsLen = len(pathParts) 139 directory = dir 140 for i := 0; i < pathPartsLen; i++ { 141 directory, _ = directory.directories[pathParts[i]] 142 if directory == nil { 143 return 144 } 145 } 146 return 147 } 148 149 // FindFiles use to get a file by some part of its name! 150 func (dir *FileDirectory) FindFiles(partName string, num uint) (files []protocol.File) { 151 for fileName, file := range dir.files { 152 if strings.Contains(fileName, partName) { 153 files = append(files, file) 154 if len(files) == int(num) { 155 return 156 } 157 } 158 } 159 return 160 } 161 162 // FindFiles use to get a file by some part of its name! 163 func (dir *FileDirectory) FindFile(partName string) (files protocol.File) { 164 for fileName, file := range dir.files { 165 if strings.Contains(fileName, partName) { 166 return file 167 } 168 } 169 return 170 } 171 172 // FindFileRecursively use to get a file by its ful name with extension in recursively! 173 func (dir *FileDirectory) FindFileRecursively(partName string) (file *File) { 174 file = dir.files[partName] 175 if file != nil { 176 return 177 } 178 if dir.directories != nil { 179 for _, dep := range dir.directories { 180 file = dep.FindFileRecursively(partName) 181 if file != nil { 182 return 183 } 184 } 185 } 186 return nil 187 } 188 189 func (dir *FileDirectory) Rename(oldURIPath, newURIPath string) (err protocol.Error) { 190 // TODO::: 191 return 192 } 193 194 func (dir *FileDirectory) Copy(uriPath, newURIPath string) (err protocol.Error) { 195 // var file = File{ 196 // Directory: f.Directory, 197 // Path: f.Path, 198 // FullName: f.FullName, 199 // Name: f.Name, 200 // Extension: f.Extension, 201 // mediaType: f.mediaType, 202 // State: f.State, 203 // data: f.data, 204 // } 205 return 206 } 207 208 func (dir *FileDirectory) Move(uriPath, newURIPath string) (err protocol.Error) { 209 // var file = File{ 210 // Directory: f.Directory, 211 // Path: f.Path, 212 // FullName: f.FullName, 213 // Name: f.Name, 214 // Extension: f.Extension, 215 // mediaType: f.mediaType, 216 // State: f.State, 217 // data: make([]byte, len(f.data)), 218 // } 219 // copy(file.data, f.data) 220 return 221 } 222 223 func (dir *FileDirectory) Delete(uriPath string) (err protocol.Error) { 224 goos.Remove(uriPath) 225 if file.IsPathDirectory(uriPath) { 226 var dir, _ = dir.directoryByPath(uriPath) 227 delete(dir.parentDirectory.directories, dir.metadata.URI().Name()) 228 } else { 229 var file, _ = dir.fileByPath(uriPath) 230 delete(file.parentDirectory.files, file.metadata.URI().Name()) 231 } 232 return 233 } 234 235 func (dir *FileDirectory) Wipe(uriPath string) (err protocol.Error) { 236 // TODO::: first write null data to uri path 237 err = dir.Delete(uriPath) 238 return 239 } 240 241 // GetDependencyRecursively use to get a dependency by its name in recursively! 242 func (dir *FileDirectory) GetDependencyRecursively(name string) *FileDirectory { 243 var t *FileDirectory 244 var ok bool 245 if dir.directories != nil { 246 t, ok = dir.directories[name] 247 if !ok { 248 for _, dep := range dir.directories { 249 t = dep.GetDependencyRecursively(name) 250 if t != nil { 251 break 252 } 253 } 254 } 255 } 256 return t 257 } 258 259 // init use to read||update Directory from disk. 260 func (dir *FileDirectory) init(path string) (err protocol.Error) { 261 dir.metadata.URI().Init(path) 262 dir.files = make(map[string]*File) 263 dir.directories = make(map[string]*FileDirectory) 264 265 var dirEntry, goErr = goos.ReadDir(dir.metadata.uri.Path()) 266 if goErr != nil { 267 // err = 268 return 269 } 270 for _, file := range dirEntry { 271 var fileName = file.Name() 272 if file.IsDir() { 273 dir.directories[fileName] = nil 274 } else { 275 dir.files[fileName] = nil 276 } 277 } 278 return 279 }