fileutils

package
v0.0.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CoverImageExtensions = []string{".jpg", ".jpeg", ".png", ".webp", ".gif", ".bmp"}

CoverImageExtensions contains all supported image extensions for cover files.

Functions

func CleanupEmptyDirectory

func CleanupEmptyDirectory(dirPath string, ignoredPatterns ...string) (bool, error)

CleanupEmptyDirectory removes a directory if it's empty or only contains ignored files. ignoredPatterns can include glob patterns like ".*" (dotfiles), ".DS_Store", "Thumbs.db", etc. Returns true if the directory was removed, false if it wasn't empty or didn't exist.

func CleanupEmptyParentDirectories

func CleanupEmptyParentDirectories(startPath, stopAt string, ignoredPatterns ...string) error

CleanupEmptyParentDirectories removes empty parent directories starting from startPath up to (but not including) stopAt. ignoredPatterns are passed to CleanupEmptyDirectory.

func ComputeNewCoverFilename added in v0.0.8

func ComputeNewCoverFilename(oldCoverFilename, newFilePath string) string

ComputeNewCoverFilename computes the new cover filename after a file has been renamed. It preserves the cover's extension while updating the base filename to match the new file path. Returns just the filename (e.g., "book.epub.cover.jpg"), not a full path. Returns empty string if oldCoverFilename is empty.

func CoverExistsWithBaseName

func CoverExistsWithBaseName(dir, baseName string) string

CoverExistsWithBaseName checks if any cover file exists with the given base name, regardless of image extension. This allows users to provide custom covers that won't be overwritten even if the book would extract a different format.

Parameters:

  • dir: the directory to check
  • baseName: the cover base name without extension (e.g., "mybook.epub.cover")

Returns the path to the existing cover file if found, or empty string if no cover exists.

func ExtractSeriesFromTitle

func ExtractSeriesFromTitle(title string, fileType string) (seriesName string, volumeNumber *float64, ok bool)

ExtractSeriesFromTitle extracts series name and volume number from a normalized title. Only applies to CBZ files with volume indicators in the "v{number}" format. Returns the base title (series name), volume number, and whether extraction succeeded.

func GenerateOrganizedFileName

func GenerateOrganizedFileName(opts OrganizedNameOptions, originalFilepath string) string

GenerateOrganizedFileName creates a standardized filename: Title.ext. For M4B files, includes narrator in braces: Title {Narrator}.m4b. Author names are NOT included since files are already inside author-prefixed folders.

func GenerateOrganizedFolderName

func GenerateOrganizedFolderName(opts OrganizedNameOptions) string

GenerateOrganizedFolderName creates a standardized folder name: [Author] Title #Volume.

func GenerateUniqueFilepathIfExists added in v0.0.8

func GenerateUniqueFilepathIfExists(path string) string

GenerateUniqueFilepathIfExists returns a unique filepath if the path exists, otherwise returns the original.

func IsOrganizedName

func IsOrganizedName(name string) bool

IsOrganizedName checks if a filename/foldername follows the organized naming pattern.

func MimeTypeFromExtension

func MimeTypeFromExtension(ext string) string

MimeTypeFromExtension returns the MIME type for a given file extension. Returns empty string if the extension is not recognized.

func MoveFile added in v0.0.8

func MoveFile(src, dst string) error

MoveFile safely moves a file from source to destination. Returns error if move fails.

func MoveFileWithAssociatedFiles added in v0.0.8

func MoveFileWithAssociatedFiles(originalFilePath, newFilePath string) (int, error)

MoveFileWithAssociatedFiles moves a file and its associated files (covers, file sidecar). This does NOT move book sidecars - only file-specific associated files. Returns the number of associated files moved.

func NormalizeImage

func NormalizeImage(data []byte, mimeType string) ([]byte, string, error)

NormalizeImage decodes and re-encodes an image to strip problematic metadata (like gAMA chunks without sRGB in PNG) that cause color rendering issues in browsers. Returns the normalized image data and the new MIME type. If the input is a JPEG, it stays as JPEG to preserve quality. Otherwise, it becomes PNG.

func NormalizeVolumeInTitle

func NormalizeVolumeInTitle(title string, fileType string) (string, bool)

NormalizeVolumeInTitle normalizes volume indicators in titles to the standard v{number} format. Only applies to CBZ files. Returns the normalized title and whether a volume was found.

func RenameOrganizedFile

func RenameOrganizedFile(currentPath string, opts OrganizedNameOptions) (string, error)

RenameOrganizedFile renames an already organized file with new metadata. Also renames associated cover images, file sidecar, AND the book sidecar. Use this for BOOK-level changes (title, author changes) that should update the book sidecar. For FILE-level changes (file name, narrator), use RenameOrganizedFileOnly.

func RenameOrganizedFileForSupplement deprecated

func RenameOrganizedFileForSupplement(currentPath string, opts OrganizedNameOptions) (string, error)

RenameOrganizedFileForSupplement is an alias for RenameOrganizedFileOnly for backwards compatibility.

Deprecated: Use RenameOrganizedFileOnly instead.

func RenameOrganizedFileOnly

func RenameOrganizedFileOnly(currentPath string, opts OrganizedNameOptions) (string, error)

RenameOrganizedFileOnly renames a file with new metadata, but does NOT rename the book sidecar. Renames the file and its associated cover images and file-specific sidecar. Use this for FILE-level changes (file name, narrator) that should not affect the book sidecar. The book sidecar should only be renamed when book-level metadata (title, author) changes.

func RenameOrganizedFolder

func RenameOrganizedFolder(currentFolderPath string, opts OrganizedNameOptions) (string, error)

RenameOrganizedFolder renames a folder containing organized files.

func SplitNames

func SplitNames(s string) []string

SplitNames splits a string of names by common delimiters (comma and semicolon), trims whitespace from each name, and returns non-empty names. This is used for parsing author and narrator lists from metadata.

Types

type OrganizeFileResult

type OrganizeFileResult struct {
	OriginalPath  string
	NewPath       string
	FolderCreated bool
	Moved         bool
	CoversMoved   int
	CoversError   error
}

OrganizeFileResult contains the results of organizing a file.

func OrganizeRootLevelFile

func OrganizeRootLevelFile(originalPath string, opts OrganizedNameOptions) (*OrganizeFileResult, error)

OrganizeRootLevelFile creates a folder and moves a root-level file into it.

type OrganizedNameOptions

type OrganizedNameOptions struct {
	AuthorNames   []string // Author names as strings for file naming
	NarratorNames []string // Narrator names for M4B file naming
	Title         string
	SeriesNumber  *float64
	FileType      string // for determining volume number formatting
}

OrganizedNameOptions contains the data needed to generate organized file/folder names.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL