Documentation ¶
Index ¶
- Variables
- func Destroy(ctx context.Context, destroyer Destroyer, f File, fileDeleter *Deleter, ...) error
- type BaseFile
- func (f *BaseFile) Base() *BaseFile
- func (f *BaseFile) Info(fs FS) (fs.FileInfo, error)
- func (f *BaseFile) Open(fs FS) (io.ReadCloser, error)
- func (f *BaseFile) Serve(fs FS, w http.ResponseWriter, r *http.Request) error
- func (f *BaseFile) SetFingerprint(fp Fingerprint)
- func (f *BaseFile) SetFingerprints(fp Fingerprints)
- type CleanHandler
- type CleanOptions
- type Cleaner
- type Counter
- type Creator
- type Decorator
- type Deleter
- type Destroyer
- type DirEntry
- type DirMakerStatRenamer
- type FS
- type File
- type Filter
- type FilterFunc
- type FilteredDecorator
- type FilteredHandler
- type Finder
- type Fingerprint
- type FingerprintCalculator
- type Fingerprints
- func (f Fingerprints) AppendUnique(o Fingerprint) Fingerprints
- func (f Fingerprints) ContentsChanged(other Fingerprints) bool
- func (f Fingerprints) Equals(other Fingerprints) bool
- func (f Fingerprints) For(type_ string) *Fingerprint
- func (f Fingerprints) Get(type_ string) interface{}
- func (f Fingerprints) GetInt64(type_ string) int64
- func (f Fingerprints) GetString(type_ string) string
- func (f *Fingerprints) Remove(type_ string)
- type Folder
- type FolderCounter
- type FolderCreator
- type FolderDestroyer
- type FolderFinder
- type FolderFinderCreator
- type FolderGetter
- type FolderGetterDestroyer
- type FolderID
- type FolderPathFinder
- type FolderStore
- type FolderUpdater
- type Getter
- type GetterDestroyer
- type GetterUpdater
- type Handler
- type ID
- type ImageFile
- type Mover
- type Opener
- type OsFS
- func (f *OsFS) Create(name string) (*os.File, error)
- func (f *OsFS) IsPathCaseSensitive(path string) (bool, error)
- func (f *OsFS) Lstat(name string) (fs.FileInfo, error)
- func (f *OsFS) MkdirAll(path string, perm fs.FileMode) error
- func (f *OsFS) Open(name string) (fs.ReadDirFile, error)
- func (f *OsFS) OpenZip(name string) (*ZipFS, error)
- func (f *OsFS) Remove(name string) error
- func (f *OsFS) RemoveAll(path string) error
- func (f *OsFS) Rename(oldpath, newpath string) error
- func (f *OsFS) Stat(name string) (fs.FileInfo, error)
- type PathFilter
- type PathFilterFunc
- type ProgressReporter
- type Renamer
- type RenamerRemover
- type Repository
- type ScanOptions
- type Scanner
- type Statter
- type Store
- type Updater
- type VideoFile
- type ZipDestroyer
- type ZipFS
- func (f *ZipFS) Close() error
- func (f *ZipFS) IsPathCaseSensitive(path string) (bool, error)
- func (f *ZipFS) Lstat(name string) (fs.FileInfo, error)
- func (f *ZipFS) Open(name string) (fs.ReadDirFile, error)
- func (f *ZipFS) OpenOnly(name string) (io.ReadCloser, error)
- func (f *ZipFS) OpenZip(name string) (*ZipFS, error)
- func (f *ZipFS) Stat(name string) (fs.FileInfo, error)
Constants ¶
This section is empty.
Variables ¶
var ( FingerprintTypeOshash = "oshash" FingerprintTypeMD5 = "md5" FingerprintTypePhash = "phash" )
Functions ¶
Types ¶
type BaseFile ¶ added in v0.17.0
type BaseFile struct { ID ID `json:"id"` DirEntry // resolved from parent folder and basename only - not stored in DB Path string `json:"path"` Basename string `json:"basename"` ParentFolderID FolderID `json:"parent_folder_id"` Fingerprints Fingerprints `json:"fingerprints"` Size int64 `json:"size"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` }
BaseFile represents a file in the file system.
func (*BaseFile) SetFingerprint ¶ added in v0.17.0
func (f *BaseFile) SetFingerprint(fp Fingerprint)
SetFingerprint sets the fingerprint of the file. If a fingerprint of the same type already exists, it is overwritten.
func (*BaseFile) SetFingerprints ¶ added in v0.17.0
func (f *BaseFile) SetFingerprints(fp Fingerprints)
SetFingerprints sets the fingerprints of the file. If a fingerprint of the same type already exists, it is overwritten.
type CleanHandler ¶ added in v0.17.0
type CleanHandler interface { HandleFile(ctx context.Context, fileDeleter *Deleter, fileID ID) error HandleFolder(ctx context.Context, fileDeleter *Deleter, folderID FolderID) error }
CleanHandler provides a handler for cleaning Files and Folders.
type CleanOptions ¶ added in v0.17.0
type CleanOptions struct { Paths []string // Do a dry run. Don't delete any files DryRun bool // PathFilter are used to determine if a file should be included. // Excluded files are marked for cleaning. PathFilter PathFilter }
ScanOptions provides options for scanning files.
type Cleaner ¶ added in v0.17.0
type Cleaner struct { FS FS Repository Repository Handlers []CleanHandler }
Cleaner scans through stored file and folder instances and removes those that are no longer present on disk.
type Decorator ¶ added in v0.17.0
type Decorator interface { Decorate(ctx context.Context, fs FS, f File) (File, error) IsMissingMetadata(ctx context.Context, fs FS, f File) bool }
Decorator wraps the Decorate method to add additional functionality while scanning files.
type Deleter ¶ added in v0.12.0
type Deleter struct { RenamerRemover RenamerRemover // contains filtered or unexported fields }
Deleter is used to safely delete files and directories from the filesystem. During a transaction, files and directories are marked for deletion using the Files and Dirs methods. This will rename the files/directories to be deleted. If the transaction is rolled back, then the files/directories can be restored to their original state with the Abort method. If the transaction is committed, the marked files are then deleted from the filesystem using the Complete method.
func NewDeleter ¶ added in v0.12.0
func NewDeleter() *Deleter
func (*Deleter) Commit ¶ added in v0.12.0
func (d *Deleter) Commit()
Commit deletes all files marked for deletion and clears the marked list. Any errors encountered are logged. All files will be attempted, regardless of the errors encountered.
func (*Deleter) Dirs ¶ added in v0.12.0
Dirs designates directories to be deleted. Each directory marked will be renamed to add a `.delete` suffix. An error is returned if a directory could not be renamed. Note that if an error is returned, then some directories may be left renamed. Abort should be called to restore marked files/directories if this function returns an error.
func (*Deleter) Files ¶ added in v0.12.0
Files designates files to be deleted. Each file marked will be renamed to add a `.delete` suffix. An error is returned if a file could not be renamed. Note that if an error is returned, then some files may be left renamed. Abort should be called to restore marked files if this function returns an error.
func (*Deleter) RegisterHooks ¶ added in v0.17.0
RegisterHooks registers post-commit and post-rollback hooks.
type DirEntry ¶ added in v0.17.0
type DirEntry struct { ZipFileID *ID `json:"zip_file_id"` // transient - not persisted // only guaranteed to have id, path and basename set ZipFile File ModTime time.Time `json:"mod_time"` }
DirEntry represents a file or directory in the file system.
type DirMakerStatRenamer ¶ added in v0.20.0
type FS ¶ added in v0.17.0
type FS interface { Stat(name string) (fs.FileInfo, error) Lstat(name string) (fs.FileInfo, error) Open(name string) (fs.ReadDirFile, error) OpenZip(name string) (*ZipFS, error) IsPathCaseSensitive(path string) (bool, error) }
FS represents a file system.
type File ¶ added in v0.17.0
type File interface { Base() *BaseFile SetFingerprints(fp Fingerprints) Open(fs FS) (io.ReadCloser, error) }
File represents a file in the file system.
type FilterFunc ¶ added in v0.17.0
type FilteredDecorator ¶ added in v0.17.0
func (*FilteredDecorator) Decorate ¶ added in v0.17.0
Decorate runs the decorator if the filter accepts the file.
func (*FilteredDecorator) IsMissingMetadata ¶ added in v0.17.0
type FilteredHandler ¶ added in v0.17.0
FilteredHandler is a Handler runs only if the filter accepts the file.
type Fingerprint ¶ added in v0.17.0
type Fingerprint struct { Type string Fingerprint interface{} }
Fingerprint represents a fingerprint of a file.
type FingerprintCalculator ¶ added in v0.17.0
type FingerprintCalculator interface {
CalculateFingerprints(f *BaseFile, o Opener, useExisting bool) ([]Fingerprint, error)
}
FingerprintCalculator calculates a fingerprint for the provided file.
type Fingerprints ¶ added in v0.17.0
type Fingerprints []Fingerprint
func (Fingerprints) AppendUnique ¶ added in v0.17.0
func (f Fingerprints) AppendUnique(o Fingerprint) Fingerprints
AppendUnique appends a fingerprint to the list if a Fingerprint of the same type does not already exist in the list. If one does, then it is updated with o's Fingerprint value.
func (Fingerprints) ContentsChanged ¶ added in v0.17.0
func (f Fingerprints) ContentsChanged(other Fingerprints) bool
ContentsChanged returns true if this Fingerprints slice contains any Fingerprints that different Fingerprint values for the matching type in other, or if this slice contains any Fingerprint types that are not in other.
func (Fingerprints) Equals ¶ added in v0.17.0
func (f Fingerprints) Equals(other Fingerprints) bool
Equals returns true if the contents of this slice are equal to those in the other slice.
func (Fingerprints) For ¶ added in v0.17.0
func (f Fingerprints) For(type_ string) *Fingerprint
For returns a pointer to the first Fingerprint element matching the provided type.
func (Fingerprints) Get ¶ added in v0.17.0
func (f Fingerprints) Get(type_ string) interface{}
func (Fingerprints) GetInt64 ¶ added in v0.17.0
func (f Fingerprints) GetInt64(type_ string) int64
func (Fingerprints) GetString ¶ added in v0.17.0
func (f Fingerprints) GetString(type_ string) string
func (*Fingerprints) Remove ¶ added in v0.17.0
func (f *Fingerprints) Remove(type_ string)
type Folder ¶ added in v0.17.0
type Folder struct { ID FolderID `json:"id"` DirEntry Path string `json:"path"` ParentFolderID *FolderID `json:"parent_folder_id"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` }
Folder represents a folder in the file system.
func GetOrCreateFolderHierarchy ¶ added in v0.20.0
func GetOrCreateFolderHierarchy(ctx context.Context, fc FolderFinderCreator, path string) (*Folder, error)
GetOrCreateFolderHierarchy gets the folder for the given path, or creates a folder hierarchy for the given path if one if no existing folder is found. Does not create any folders in the file system
type FolderCounter ¶ added in v0.17.0
type FolderCreator ¶ added in v0.17.0
FolderCreator provides methods to create Folders.
type FolderDestroyer ¶ added in v0.17.0
type FolderFinder ¶ added in v0.20.0
type FolderFinderCreator ¶ added in v0.20.0
type FolderFinderCreator interface { FolderPathFinder FolderCreator }
type FolderGetter ¶ added in v0.17.0
type FolderGetter interface { FolderFinder FolderPathFinder FindByZipFileID(ctx context.Context, zipFileID ID) ([]*Folder, error) FindAllInPaths(ctx context.Context, p []string, limit, offset int) ([]*Folder, error) FindByParentFolderID(ctx context.Context, parentFolderID FolderID) ([]*Folder, error) }
FolderGetter provides methods to find Folders.
type FolderGetterDestroyer ¶ added in v0.17.0
type FolderGetterDestroyer interface { FolderGetter FolderDestroyer }
type FolderPathFinder ¶ added in v0.20.0
FolderPathFinder finds Folders by their path.
type FolderStore ¶ added in v0.17.0
type FolderStore interface { FolderGetter FolderCounter FolderCreator FolderUpdater FolderDestroyer }
FolderStore provides methods to find, create and update Folders.
type FolderUpdater ¶ added in v0.17.0
FolderUpdater provides methods to update Folders.
type Getter ¶ added in v0.17.0
type Getter interface { Finder FindByPath(ctx context.Context, path string) (File, error) FindAllByPath(ctx context.Context, path string) ([]File, error) FindByFingerprint(ctx context.Context, fp Fingerprint) ([]File, error) FindByZipFileID(ctx context.Context, zipFileID ID) ([]File, error) FindAllInPaths(ctx context.Context, p []string, limit, offset int) ([]File, error) }
Getter provides methods to find Files.
type GetterDestroyer ¶ added in v0.17.0
type GetterUpdater ¶ added in v0.20.0
type ImageFile ¶ added in v0.17.0
type ImageFile struct { *BaseFile Format string `json:"format"` Width int `json:"width"` Height int `json:"height"` }
ImageFile is an extension of BaseFile to represent image files.
type Mover ¶ added in v0.20.0
type Mover struct { Renamer DirMakerStatRenamer Files GetterUpdater Folders FolderStore // contains filtered or unexported fields }
func NewMover ¶ added in v0.20.0
func NewMover(fileStore GetterUpdater, folderStore FolderStore) *Mover
func (*Mover) CreateFolderHierarchy ¶ added in v0.20.0
type Opener ¶ added in v0.17.0
type Opener interface {
Open() (io.ReadCloser, error)
}
Opener provides an interface to open a file.
type OsFS ¶ added in v0.17.0
type OsFS struct{}
OsFS is a file system backed by the OS.
func (*OsFS) IsPathCaseSensitive ¶ added in v0.17.2
type PathFilter ¶ added in v0.17.0
PathFilter provides a filter function for paths.
type PathFilterFunc ¶ added in v0.17.0
func (PathFilterFunc) Accept ¶ added in v0.17.0
func (pff PathFilterFunc) Accept(path string) bool
type ProgressReporter ¶ added in v0.17.0
type ProgressReporter interface { AddTotal(total int) Increment() Definite() ExecuteTask(description string, fn func()) }
ProgressReporter is used to report progress of the scan.
type RenamerRemover ¶ added in v0.12.0
type RenamerRemover interface { Renamer Remove(name string) error RemoveAll(path string) error Statter }
RenamerRemover provides access to the Rename and Remove functions.
type Repository ¶ added in v0.17.0
type Repository struct { txn.Manager txn.DatabaseProvider Store FolderStore FolderStore }
Repository provides access to storage methods for files and folders.
type ScanOptions ¶ added in v0.17.0
type ScanOptions struct { Paths []string // ZipFileExtensions is a list of file extensions that are considered zip files. // Extension does not include the . character. ZipFileExtensions []string // ScanFilters are used to determine if a file should be scanned. ScanFilters []PathFilter // HandlerRequiredFilters are used to determine if an unchanged file needs to be handled HandlerRequiredFilters []Filter ParallelTasks int }
ScanOptions provides options for scanning files.
type Scanner ¶
type Scanner struct { FS FS Repository Repository FingerprintCalculator FingerprintCalculator // FileDecorators are applied to files as they are scanned. FileDecorators []Decorator }
Scanner scans files into the database.
The scan process works using two goroutines. The first walks through the provided paths in the filesystem. It runs each directory entry through the provided ScanFilters. If none of the filter Accept methods return true, then the file/directory is ignored. Any folders found are handled immediately. Files inside zip files are also handled immediately. All other files encountered are sent to the second goroutine queue.
Folders are handled by checking if the folder exists in the database, by its full path. If a folder entry already exists, then its mod time is updated (if applicable). If the folder does not exist in the database, then a new folder entry its created.
Files are handled by first querying for the file by its path. If the file entry exists in the database, then the mod time is compared to the value in the database. If the mod time is different then file is marked as updated - it recalculates any fingerprints and fires decorators, then the file entry is updated and any applicable handlers are fired.
If the file entry does not exist in the database, then fingerprints are calculated for the file. It then determines if the file is a rename of an existing file by querying for file entries with the same fingerprint. If any are found, it checks each to see if any are missing in the file system. If one is, then the file is treated as renamed and its path is updated. If none are missing, or many are, then the file is treated as a new file.
If the file is not a renamed file, then the decorators are fired and the file is created, then the applicable handlers are fired.
func (*Scanner) Scan ¶ added in v0.17.0
func (s *Scanner) Scan(ctx context.Context, handlers []Handler, options ScanOptions, progressReporter ProgressReporter)
Scan starts the scanning process.
type Store ¶ added in v0.17.0
type Store interface { Getter Counter Creator Updater Destroyer IsPrimary(ctx context.Context, fileID ID) (bool, error) }
Store provides methods to find, create and update Files.
type VideoFile ¶ added in v0.17.0
type VideoFile struct { *BaseFile Format string `json:"format"` Width int `json:"width"` Height int `json:"height"` Duration float64 `json:"duration"` VideoCodec string `json:"video_codec"` AudioCodec string `json:"audio_codec"` FrameRate float64 `json:"frame_rate"` BitRate int64 `json:"bitrate"` Interactive bool `json:"interactive"` InteractiveSpeed *int `json:"interactive_speed"` }
VideoFile is an extension of BaseFile to represent video files.
func (VideoFile) GetMinResolution ¶ added in v0.17.0
type ZipDestroyer ¶ added in v0.17.0
type ZipDestroyer struct { FileDestroyer GetterDestroyer FolderDestroyer FolderGetterDestroyer }
func (*ZipDestroyer) DestroyZip ¶ added in v0.17.0
type ZipFS ¶ added in v0.17.0
ZipFS is a file system backed by a zip file.