github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/protocol/storage-file.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package protocol 4 5 // FileDirectory is the descriptor interface that must implement by any to be an file! 6 // File owner is one app so it must handle concurrent protection internally not by file it self! 7 type FileDirectory interface { 8 Metadata() FileDirectoryMetadata 9 ParentDirectory() (dir FileDirectory) 10 11 Directories(offset, limit uint64) (dirs []FileDirectory) 12 Directory(name string) (dir FileDirectory, err Error) // make if not exist before 13 14 Files(offset, limit uint64) (files []File) 15 File(name string) (file File, err Error) // make if not exist before 16 FileByPath(uriPath string) (file File, err Error) 17 18 FindFiles(partName string, num uint) (files []File) 19 FindFile(partName string) (file File) // return first match file. It will prevent unneeded slice allocation. 20 21 Rename(oldURIPath, newURIPath string) (err Error) 22 Copy(uriPath, newURIPath string) (err Error) 23 Move(uriPath, newURIPath string) (err Error) 24 Delete(uriPath string) (err Error) // make invisible just by remove from primary index 25 Erase(uriPath string) (err Error) // make invisible by remove from primary index & write zero data to all file locations 26 } 27 28 // FileDirectoryMetadata is the interface that must implement by any file and directory! 29 type FileDirectoryMetadata interface { 30 DirNum() uint // return number of directory save in this directory 31 FileNum() uint // return number of file save in this directory 32 33 FileMetadata 34 } 35 36 // File is the descriptor interface that must implement by any to be an file! 37 // File owner is one app so it must handle concurrent protection internally not by file it self! 38 type File interface { 39 Metadata() FileMetadata 40 Data() FileData 41 ParentDirectory() (dir FileDirectory) 42 43 Rename(newName string) 44 } 45 46 // FileMetadata is the interface that must implement by any file and directory! 47 type FileMetadata interface { 48 URI() FileURI 49 Size() uint64 // in Byte 50 Created() Time 51 Accessed() Time 52 Modified() Time 53 } 54 55 // https://datatracker.ietf.org/doc/html/rfc8089 56 // https://en.wikipedia.org/wiki/File_URI_scheme 57 type FileURI interface { 58 URI 59 // URI() string // always return full file uri as "{{file}}://{{sabz.city}}/{{path/to/{{{{the file}}.{{html}}}}}}" 60 // Scheme() string // always return "file" 61 Domain() string 62 Path() string // File location from root directory include file name. 63 Name() string // Full name with extension if exist 64 Extension() string 65 NameWithoutExtension() string 66 67 // Helper functions 68 IsDirectory() bool // Path end with "/" if it is a file directory 69 } 70 71 type FileData interface { 72 // Depend on OS, file data can be cache on ram until Save() called! 73 Save() (err Error) 74 75 Prepend(data []byte) 76 Append(data []byte) 77 Replace(old, new []byte, n int) 78 Codec 79 }