Documentation
¶
Overview ¶
Package file provides file management functionality for the Weblens system.
Index ¶
- Constants
- Variables
- func GenerateContentID(ctx context.Context, f *WeblensFileImpl) (string, error)
- func GetFileOwnerName(ctx context.Context, file *WeblensFileImpl) (string, error)
- func GetFileOwnerNameFromPath(_ context.Context, portable fs.Filepath) (string, error)
- func IsFileInTrash(f *WeblensFileImpl) bool
- type NewFileOptions
- type Service
- type WeblensFile
- type WeblensFileImpl
- func (f *WeblensFileImpl) AddChild(child *WeblensFileImpl) error
- func (f *WeblensFileImpl) Append(data []byte) error
- func (f *WeblensFileImpl) BubbleMap(fn func(*WeblensFileImpl) error) error
- func (f *WeblensFileImpl) ChildrenLoaded() bool
- func (f *WeblensFileImpl) Close() error
- func (f *WeblensFileImpl) CreateSelf() error
- func (f *WeblensFileImpl) Exists() bool
- func (f *WeblensFileImpl) Freeze() *WeblensFileImpl
- func (f *WeblensFileImpl) GetChild(childName string) (*WeblensFileImpl, error)
- func (f *WeblensFileImpl) GetChildren() []*WeblensFileImpl
- func (f *WeblensFileImpl) GetContentID() string
- func (f *WeblensFileImpl) GetParent() *WeblensFileImpl
- func (f *WeblensFileImpl) GetPortablePath() file_system.Filepath
- func (f *WeblensFileImpl) ID() string
- func (f *WeblensFileImpl) InitChildren()
- func (f *WeblensFileImpl) IsDir() bool
- func (f *WeblensFileImpl) IsParentOf(child *WeblensFileImpl) bool
- func (f *WeblensFileImpl) IsPastFile() bool
- func (f *WeblensFileImpl) IsReadOnly() bool
- func (f *WeblensFileImpl) LeafMap(fn func(*WeblensFileImpl) error) error
- func (f *WeblensFileImpl) LoadStat() (newSize int64, err error)
- func (f *WeblensFileImpl) MarshalJSON() ([]byte, error)
- func (f *WeblensFileImpl) ModTime() (t time.Time)
- func (f *WeblensFileImpl) Mode() os.FileMode
- func (f *WeblensFileImpl) Name() string
- func (f *WeblensFileImpl) Read(p []byte) (n int, err error)
- func (f *WeblensFileImpl) ReadAll() ([]byte, error)
- func (f *WeblensFileImpl) Readable() (io.Reader, error)
- func (f *WeblensFileImpl) Readdir(count int) ([]fs.FileInfo, error)
- func (f *WeblensFileImpl) RecursiveMap(fn func(*WeblensFileImpl) error) error
- func (f *WeblensFileImpl) Remove() error
- func (f *WeblensFileImpl) RemoveChild(child string) error
- func (f *WeblensFileImpl) ReplaceRoot(newRoot string)
- func (f *WeblensFileImpl) Seek(offset int64, whence int) (int64, error)
- func (f *WeblensFileImpl) SetContentID(newContentID string)
- func (f *WeblensFileImpl) SetID(id string)
- func (f *WeblensFileImpl) SetMemOnly(memOnly bool)
- func (f *WeblensFileImpl) SetModifiedTime(ts time.Time)
- func (f *WeblensFileImpl) SetParent(p *WeblensFileImpl) error
- func (f *WeblensFileImpl) SetPastFile(isPastFile bool)
- func (f *WeblensFileImpl) SetPortablePath(path file_system.Filepath)
- func (f *WeblensFileImpl) SetSize(newSize int64)
- func (f *WeblensFileImpl) SetWatching() error
- func (f *WeblensFileImpl) Size() int64
- func (f *WeblensFileImpl) Stat() (fs.FileInfo, error)
- func (f *WeblensFileImpl) Sys() any
- func (f *WeblensFileImpl) UnmarshalJSON(bs []byte) error
- func (f *WeblensFileImpl) WithLock(fn func() error) error
- func (f *WeblensFileImpl) Write(data []byte) (int, error)
- func (f *WeblensFileImpl) WriteAt(data []byte, seekLoc int64) error
- func (f *WeblensFileImpl) Writer() (io.WriteCloser, error)
Constants ¶
const ( // UsersTreeKey identifies the root of the users file tree. UsersTreeKey = "USERS" // RestoreTreeKey identifies the root of the restore file tree. RestoreTreeKey = "RESTORE" // CachesTreeKey identifies the root of the caches file tree. CachesTreeKey = "CACHES" // BackupTreeKey identifies the root of the backup file tree. BackupTreeKey = "BACKUP" // UserTrashDirName is the directory name for user trash folders. UserTrashDirName = ".user_trash" // ThumbsDirName is the directory name for thumbnail storage. ThumbsDirName = "thumbs/" // ZipsDirName is the directory name for zip file storage. ZipsDirName = "zips/" )
Variables ¶
var BackupRootPath = fs.Filepath{RootAlias: BackupTreeKey}
BackupRootPath is the filepath to the backup file tree root.
var CacheRootPath = fs.Filepath{RootAlias: CachesTreeKey}
CacheRootPath is the filepath to the cache file tree root.
var ErrDirectoryAlreadyExists = wlerrors.New("directory already exists")
ErrDirectoryAlreadyExists is returned when attempting to create a directory that already exists.
var ErrDirectoryNotAllowed = wlerrors.New("directory not allowed")
ErrDirectoryNotAllowed is returned when a directory is provided but not allowed for the operation.
var ErrDirectoryRequired = wlerrors.New("directory required")
ErrDirectoryRequired is returned when an operation requires a directory but a regular file was provided.
var ErrEmptyFile = wlerrors.New("file is empty")
ErrEmptyFile is returned when an operation cannot be performed on an empty file.
var ErrFileAlreadyExists = wlerrors.New("file already exists")
ErrFileAlreadyExists is returned when attempting to create a file that already exists.
var ErrFileAlreadyHasTask = wlerrors.New("file already has task")
ErrFileAlreadyHasTask is returned when attempting to assign a task to a file that already has one.
var ErrFileNoTask = wlerrors.New("file has no task")
ErrFileNoTask is returned when a file operation requires a task but none is present.
var ErrFileNotFound = wlerrors.New("file not found")
ErrFileNotFound is returned when a requested file cannot be found.
var ErrFileTreeNotFound = wlerrors.New("file tree not found")
ErrFileTreeNotFound is returned when a requested file tree cannot be found.
var ErrNilFile = wlerrors.New("file is nil")
ErrNilFile is returned when a file pointer is unexpectedly nil.
var ErrNoChildren = wlerrors.New("directory has no children")
ErrNoChildren is returned when a directory operation requires children but the directory has none.
var ErrNoContentID = wlerrors.New("file has no content id")
ErrNoContentID is returned when a file operation requires a content ID but none is present.
var ErrNoFileID = wlerrors.New("file has no id")
ErrNoFileID is returned when a file operation requires an ID but none is present.
var ErrNoParent = wlerrors.New("file has no parent")
ErrNoParent is returned when a file operation requires a parent but none is present.
var ErrNotChild = wlerrors.New("file is not a child of given directory")
ErrNotChild is returned when a file is not a child of the specified directory.
var RestoreDirPath = fs.Filepath{RootAlias: RestoreTreeKey}
RestoreDirPath is the filepath to the restore directory.
var ThumbsDirPath = fs.Filepath{RootAlias: CachesTreeKey, RelPath: ThumbsDirName}
ThumbsDirPath is the filepath to the thumbnails storage directory.
var UsersRootPath = fs.Filepath{RootAlias: UsersTreeKey}
UsersRootPath is the filepath to the users file tree root.
var ZipsDirPath = fs.Filepath{RootAlias: CachesTreeKey, RelPath: ZipsDirName}
ZipsDirPath is the filepath to the zips storage directory.
Functions ¶
func GenerateContentID ¶
func GenerateContentID(ctx context.Context, f *WeblensFileImpl) (string, error)
GenerateContentID computes and returns a content hash for the file.
func GetFileOwnerName ¶
func GetFileOwnerName(ctx context.Context, file *WeblensFileImpl) (string, error)
GetFileOwnerName retrieves the username of the file owner from a file instance.
func GetFileOwnerNameFromPath ¶
GetFileOwnerNameFromPath extracts the username of the file owner from a portable filepath.
func IsFileInTrash ¶
func IsFileInTrash(f *WeblensFileImpl) bool
IsFileInTrash checks whether a file is located in the user trash directory.
Types ¶
type NewFileOptions ¶
type NewFileOptions struct {
Path file_system.Filepath
FileID string
ContentID string
IsPastFile bool
Size int64
MemOnly bool
// CreateNow will create the file if it doesn't exist
CreateNow bool
// GenerateID will generate a new ID for the file
GenerateID bool
ModifiedDate option.Option[time.Time]
}
NewFileOptions represents configuration options for creating a new WeblensFile.
type Service ¶
type Service interface {
AddFile(context context.Context, file ...*WeblensFileImpl) error
// Size returns the size of the specified file tree
Size(treeAlias string) int64
// GetFileByID retrieves a file by its ID
GetFileByID(ctx context.Context, fileID string) (*WeblensFileImpl, error)
// GetFileByFilepath retrieves a file by its filepath
GetFileByFilepath(ctx context.Context, path fs.Filepath, dontLoadNew ...bool) (*WeblensFileImpl, error)
// CreateFile creates a new file
CreateFile(ctx context.Context, parent *WeblensFileImpl, filename string, data ...[]byte) (*WeblensFileImpl, error)
// CreateFolder creates a new folder
CreateFolder(ctx context.Context, parent *WeblensFileImpl, folderName string) (*WeblensFileImpl, error)
// GetChildren retrieves children of a folder
GetChildren(ctx context.Context, folder *WeblensFileImpl) ([]*WeblensFileImpl, error)
// GetChildrenByPath loads children of a folder recursively
RecursiveEnsureChildrenLoaded(ctx context.Context, folder *WeblensFileImpl) error
// CreateUserHome creates a home directory for a user
CreateUserHome(ctx context.Context, user *user_model.User) error
// NewBackupRestoreFile creates a new file for backup restoration from a remote tower
NewBackupRestoreFile(ctx context.Context, contentID, remoteTowerID string) (*WeblensFileImpl, error)
// InitBackupDirectory initializes the backup directory for a tower
InitBackupDirectory(ctx context.Context, tower tower_model.Instance) (*WeblensFileImpl, error)
// MoveFiles moves files to a new location
MoveFiles(ctx context.Context, files []*WeblensFileImpl, destFolder *WeblensFileImpl) error
// RenameFile renames a file
RenameFile(ctx context.Context, file *WeblensFileImpl, newName string) error
// ReturnFilesFromTrash restores files from the trash
ReturnFilesFromTrash(ctx context.Context, trashFiles []*WeblensFileImpl) error
// DeleteFiles permanently deletes files
DeleteFiles(ctx context.Context, files ...*WeblensFileImpl) error
// RestoreFiles restores files from history
RestoreFiles(ctx context.Context, ids []string, newParent *WeblensFileImpl, restoreTime time.Time) error
// GetMediaCacheByFilename retrieves media cache by filename
GetMediaCacheByFilename(ctx context.Context, filename string) (*WeblensFileImpl, error)
// GetFileByContentID retrieves a file by its content ID
GetFileByContentID(ctx context.Context, contentID string) (*WeblensFileImpl, error)
// NewCacheFile creates a new cache file for media
NewCacheFile(mediaID string, quality string, pageNum int) (*WeblensFileImpl, error)
// DeleteCacheFile deletes a cache file
DeleteCacheFile(file *WeblensFileImpl) error
// NewZip creates a new zip file
NewZip(ctx context.Context, zipName string, owner *user_model.User) (*WeblensFileImpl, error)
// GetZip retrieves a zip file by ID
GetZip(ctx context.Context, id string) (*WeblensFileImpl, error)
}
Service provides operations for managing files and folders in the Weblens system. It handles file creation, modification, deletion, and retrieval, as well as media caching and backup operations.
type WeblensFile ¶
type WeblensFile interface {
ID() string
Write(data []byte) (int, error)
ReadAll() ([]byte, error)
}
WeblensFile is an interface for file operations.
type WeblensFileImpl ¶
type WeblensFileImpl struct {
// contains filtered or unexported fields
}
WeblensFileImpl represents a file in the Weblens system with metadata and operations.
func NewWeblensFile ¶
func NewWeblensFile(params NewFileOptions) *WeblensFileImpl
NewWeblensFile creates and initializes a new WeblensFileImpl with the provided options.
func (*WeblensFileImpl) AddChild ¶
func (f *WeblensFileImpl) AddChild(child *WeblensFileImpl) error
AddChild adds a child file to the directory.
func (*WeblensFileImpl) Append ¶
func (f *WeblensFileImpl) Append(data []byte) error
Append appends data to the end of the file.
func (*WeblensFileImpl) BubbleMap ¶
func (f *WeblensFileImpl) BubbleMap(fn func(*WeblensFileImpl) error) error
BubbleMap performs fn on f and all parents of f, ignoring the mediaService root or other static directories. Files are acted on in the order of their index number below, starting with the caller, children are never accessed.
f3 <- Parent of f2 | f2 <- Parent of f1 | f1 <- Root caller
func (*WeblensFileImpl) ChildrenLoaded ¶
func (f *WeblensFileImpl) ChildrenLoaded() bool
ChildrenLoaded returns true if the directory's children have been loaded into memory.
func (*WeblensFileImpl) CreateSelf ¶
func (f *WeblensFileImpl) CreateSelf() error
CreateSelf creates the file or directory on the filesystem.
func (*WeblensFileImpl) Exists ¶
func (f *WeblensFileImpl) Exists() bool
Exists check if the file exists on the real filesystem below.
func (*WeblensFileImpl) Freeze ¶
func (f *WeblensFileImpl) Freeze() *WeblensFileImpl
Freeze returns a shallow copy of the file descriptor.
func (*WeblensFileImpl) GetChild ¶
func (f *WeblensFileImpl) GetChild(childName string) (*WeblensFileImpl, error)
GetChild returns the child file with the specified name.
func (*WeblensFileImpl) GetChildren ¶
func (f *WeblensFileImpl) GetChildren() []*WeblensFileImpl
GetChildren returns all child files in the directory.
func (*WeblensFileImpl) GetContentID ¶
func (f *WeblensFileImpl) GetContentID() string
GetContentID returns the content hash of the file.
func (*WeblensFileImpl) GetParent ¶
func (f *WeblensFileImpl) GetParent() *WeblensFileImpl
GetParent returns the parent directory of the file.
func (*WeblensFileImpl) GetPortablePath ¶
func (f *WeblensFileImpl) GetPortablePath() file_system.Filepath
GetPortablePath returns the portable filepath that can be safely translated between systems.
func (*WeblensFileImpl) ID ¶
func (f *WeblensFileImpl) ID() string
ID returns the unique identifier the file, which is the portable path of the file.
func (*WeblensFileImpl) InitChildren ¶
func (f *WeblensFileImpl) InitChildren()
InitChildren initializes the children map for a directory.
func (*WeblensFileImpl) IsDir ¶
func (f *WeblensFileImpl) IsDir() bool
IsDir returns true if the file is a directory.
func (*WeblensFileImpl) IsParentOf ¶
func (f *WeblensFileImpl) IsParentOf(child *WeblensFileImpl) bool
IsParentOf returns true if f is an ancestor of child.
func (*WeblensFileImpl) IsPastFile ¶
func (f *WeblensFileImpl) IsPastFile() bool
IsPastFile returns true if the file is a past version.
func (*WeblensFileImpl) IsReadOnly ¶
func (f *WeblensFileImpl) IsReadOnly() bool
IsReadOnly returns true if the file is read-only.
func (*WeblensFileImpl) LeafMap ¶
func (f *WeblensFileImpl) LeafMap(fn func(*WeblensFileImpl) error) error
LeafMap recursively perform fn on leaves, first, and work back up the tree. This takes an inverted "Depth first" approach. Note this behaves very differently than RecursiveMap. See below.
Files are acted on in the order of their index number here, starting with the leftmost leaf
fx.LeafMap(fn) | f5 / \ f3 f4 / \ f1 f2
func (*WeblensFileImpl) LoadStat ¶
func (f *WeblensFileImpl) LoadStat() (newSize int64, err error)
LoadStat will recompute the size and modify date of the file using os.Stat. If the size of the file changes, LoadStat will return the newSize. If the size does not change, LoadStat will return -1 for the newSize. To get the current size of the file, use Size() instead.
func (*WeblensFileImpl) MarshalJSON ¶
func (f *WeblensFileImpl) MarshalJSON() ([]byte, error)
MarshalJSON serializes the WeblensFileImpl into JSON format.
func (*WeblensFileImpl) ModTime ¶
func (f *WeblensFileImpl) ModTime() (t time.Time)
ModTime returns the last modification time of the file.
func (*WeblensFileImpl) Mode ¶
func (f *WeblensFileImpl) Mode() os.FileMode
Mode returns the file mode bits for the file.
func (*WeblensFileImpl) Name ¶
func (f *WeblensFileImpl) Name() string
Name returns the filename of the file.
func (*WeblensFileImpl) ReadAll ¶
func (f *WeblensFileImpl) ReadAll() ([]byte, error)
ReadAll reads and returns the entire contents of the file.
func (*WeblensFileImpl) Readable ¶
func (f *WeblensFileImpl) Readable() (io.Reader, error)
Readable returns a reader for the file contents.
func (*WeblensFileImpl) Readdir ¶
func (f *WeblensFileImpl) Readdir(count int) ([]fs.FileInfo, error)
Readdir reads and returns up to count directory entries.
func (*WeblensFileImpl) RecursiveMap ¶
func (f *WeblensFileImpl) RecursiveMap(fn func(*WeblensFileImpl) error) error
RecursiveMap applies function fn to every file recursively.
func (*WeblensFileImpl) Remove ¶
func (f *WeblensFileImpl) Remove() error
Remove deletes the file or directory from the filesystem.
func (*WeblensFileImpl) RemoveChild ¶
func (f *WeblensFileImpl) RemoveChild(child string) error
RemoveChild removes a child file from the directory by name.
func (*WeblensFileImpl) ReplaceRoot ¶
func (f *WeblensFileImpl) ReplaceRoot(newRoot string)
ReplaceRoot changes the root alias of the file's path.
func (*WeblensFileImpl) Seek ¶
func (f *WeblensFileImpl) Seek(offset int64, whence int) (int64, error)
Seek sets the offset for the next read or write operation.
func (*WeblensFileImpl) SetContentID ¶
func (f *WeblensFileImpl) SetContentID(newContentID string)
SetContentID sets the content hash of the file.
func (*WeblensFileImpl) SetID ¶
func (f *WeblensFileImpl) SetID(id string)
SetID sets the unique identifier for the file.
func (*WeblensFileImpl) SetMemOnly ¶
func (f *WeblensFileImpl) SetMemOnly(memOnly bool)
SetMemOnly sets whether the file should only be stored in memory.
func (*WeblensFileImpl) SetModifiedTime ¶
func (f *WeblensFileImpl) SetModifiedTime(ts time.Time)
SetModifiedTime sets the last modification time of the file.
func (*WeblensFileImpl) SetParent ¶
func (f *WeblensFileImpl) SetParent(p *WeblensFileImpl) error
SetParent sets the parent directory for the file.
func (*WeblensFileImpl) SetPastFile ¶
func (f *WeblensFileImpl) SetPastFile(isPastFile bool)
SetPastFile marks the file as a past version.
func (*WeblensFileImpl) SetPortablePath ¶
func (f *WeblensFileImpl) SetPortablePath(path file_system.Filepath)
SetPortablePath sets the portable filepath for the file.
func (*WeblensFileImpl) SetSize ¶
func (f *WeblensFileImpl) SetSize(newSize int64)
SetSize sets the size of the file in bytes.
func (*WeblensFileImpl) SetWatching ¶
func (f *WeblensFileImpl) SetWatching() error
SetWatching marks the file as being watched for changes.
func (*WeblensFileImpl) Size ¶
func (f *WeblensFileImpl) Size() int64
Size returns the size of the file in bytes.
func (*WeblensFileImpl) Stat ¶
func (f *WeblensFileImpl) Stat() (fs.FileInfo, error)
Stat returns file information for the file.
func (*WeblensFileImpl) Sys ¶
func (f *WeblensFileImpl) Sys() any
Sys returns the underlying system-specific data for the file.
func (*WeblensFileImpl) UnmarshalJSON ¶
func (f *WeblensFileImpl) UnmarshalJSON(bs []byte) error
UnmarshalJSON deserializes JSON data into the WeblensFileImpl structure.
func (*WeblensFileImpl) WithLock ¶
func (f *WeblensFileImpl) WithLock(fn func() error) error
WithLock is a quick way to ensure locks on files are lifted if the function using the file is to panic.
func (*WeblensFileImpl) WriteAt ¶
func (f *WeblensFileImpl) WriteAt(data []byte, seekLoc int64) error
WriteAt writes data to the file at the specified offset.
func (*WeblensFileImpl) Writer ¶
func (f *WeblensFileImpl) Writer() (io.WriteCloser, error)
Writer returns a writer for the file contents.