Documentation
¶
Overview ¶
Package export handles file export operations such as creating zip archives of email attachments.
Index ¶
- Variables
- func CreateExclusiveFile(p string, perm os.FileMode) (*os.File, string, error)
- func FormatBytesLong(b int64) string
- func FormatExportResult(stats ExportStats) string
- func SanitizeFilename(s string) string
- func StoragePath(attachmentsDir, contentHash string) (string, error)
- func ValidateContentHash(hash string) error
- func ValidateOutputPath(outputPath string) error
- type DirExportResult
- type ExportStats
- type ExportedFile
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidContentHash = errors.New("invalid content hash")
ErrInvalidContentHash is returned when a content hash fails validation.
Functions ¶
func CreateExclusiveFile ¶ added in v0.5.0
CreateExclusiveFile atomically creates a file that doesn't already exist, trying path, path_1, path_2, etc. on conflict. Returns the open file and the path that was actually used. Uses O_CREATE|O_EXCL to avoid TOCTOU races.
func FormatBytesLong ¶
FormatBytesLong formats bytes with full precision for export results.
func FormatExportResult ¶ added in v0.4.0
func FormatExportResult(stats ExportStats) string
FormatExportResult formats ExportStats into a human-readable string for display.
func SanitizeFilename ¶
SanitizeFilename removes or replaces characters that are invalid in filenames.
func StoragePath ¶ added in v0.5.0
StoragePath returns the content-addressed file path for an attachment: attachmentsDir/<hash[:2]>/<hash>. Returns an error if the content hash is invalid (prevents panics from short/empty strings).
func ValidateContentHash ¶ added in v0.5.0
ValidateContentHash validates that a content hash is a valid SHA-256 hex string. This prevents path traversal attacks by ensuring the hash contains only hexadecimal characters and is exactly 64 characters long.
func ValidateOutputPath ¶ added in v0.5.2
ValidateOutputPath checks that an output file path does not escape the current working directory. This guards against email-supplied filenames being passed to --output (e.g., an attachment named "../../.ssh/authorized_keys" or "/etc/cron.d/evil"). Both absolute paths and ".." traversal are rejected.
Types ¶
type DirExportResult ¶ added in v0.5.0
type DirExportResult struct {
Files []ExportedFile
Errors []string
}
DirExportResult contains the results of exporting attachments to a directory.
func AttachmentsToDir ¶ added in v0.5.0
func AttachmentsToDir(outputDir, attachmentsDir string, attachments []query.AttachmentInfo) DirExportResult
AttachmentsToDir exports attachments as individual files into outputDir. It reads attachment content from attachmentsDir using content-hash based paths and writes each file with its original filename (sanitized, deduplicated). Files are created with O_EXCL to avoid overwriting existing files.
func (DirExportResult) TotalSize ¶ added in v0.5.0
func (r DirExportResult) TotalSize() int64
TotalSize returns the sum of all exported file sizes.
type ExportStats ¶ added in v0.4.0
type ExportStats struct {
Count int
Size int64
Errors []string
ZipPath string
WriteError bool // true if a write error occurred and the zip was removed
}
ExportStats contains structured results of an attachment export operation.
func Attachments ¶
func Attachments(zipFilename, attachmentsDir string, attachments []query.AttachmentInfo) ExportStats
Attachments exports the given attachments into a zip file. It reads attachment content from attachmentsDir using content-hash based paths.
type ExportedFile ¶ added in v0.5.0
ExportedFile represents a single file written to a directory.