export

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package export handles file export operations such as creating zip archives of email attachments.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidContentHash = errors.New("invalid content hash")

ErrInvalidContentHash is returned when a content hash fails validation.

Functions

func CreateExclusiveFile added in v0.5.0

func CreateExclusiveFile(p string, perm os.FileMode) (*os.File, string, error)

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

func FormatBytesLong(b int64) string

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

func SanitizeFilename(s string) string

SanitizeFilename removes or replaces characters that are invalid in filenames.

func StoragePath added in v0.5.0

func StoragePath(attachmentsDir, contentHash string) (string, error)

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

func ValidateContentHash(hash string) error

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

func ValidateOutputPath(outputPath string) error

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

type ExportedFile struct {
	Path string // absolute path of the written file
	Size int64
}

ExportedFile represents a single file written to a directory.

Jump to

Keyboard shortcuts

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