service

package
v1.13.1 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 34 Imported by: 0

Documentation

Overview

Package service provides shared business logic used by CLI commands. It extracts reusable operations from cmd/ to reduce file size and enable testing without cobra dependencies.

Index

Constants

View Source
const ContentProducerKey = "ContentProducer"

TC260 field keys.

Variables

This section is empty.

Functions

func DownloadFile

func DownloadFile(source, outputDir, taskID string) (string, error)

DownloadFile saves a resource (HTTP URL, data URI, or base64) to outputDir with auto-naming: <taskID><ext>. The extension comes from the source URL.

func ExtractExt

func ExtractExt(rawURL string) string

ExtractExt returns the file extension from a URL, defaulting to ".mp4".

func FetchBytes

func FetchBytes(rawURL string) ([]byte, error)

FetchBytes gets raw bytes from a URL, data URI, or base64 string. Supports:

  • HTTP/HTTPS URLs (download via GET with http.DefaultClient)
  • data: URIs (e.g. data:image/png;base64,...)
  • Raw base64 strings

func ParseCompressOption added in v1.13.1

func ParseCompressOption(val string) (targetSize int64, quality int, err error)

ParseCompressOption parses a --compress flag value. Supported formats:

  • "800KB" or "800kb" → TargetSize = 800 * 1024
  • "2MB" or "2mb" → TargetSize = 2 * 1024 * 1024
  • "85%" → Quality = 85

Returns targetSize, quality, error.

func PreviewFile

func PreviewFile(path string) error

PreviewFile opens a file or URL with the system default application. For image files, it also attempts inline terminal display when the terminal supports it (Kitty, iTerm2, or Sixel). URLs are downloaded to a temporary file first.

func PrintDetectResult

func PrintDetectResult(w io.Writer, result *DetectResult, verbose bool) error

PrintDetectResult writes detection results to w. When verbose=false: only print C2PA and TC260 watermark info. When verbose=true: print everything (file stats, dimensions, format, all metadata).

func PrintDetectResultJSON

func PrintDetectResultJSON(w io.Writer, result *DetectResult) error

PrintDetectResultJSON writes detection results as JSON to w.

func PrintImageInfo

func PrintImageInfo(w io.Writer, path string) error

PrintImageInfo writes image metadata to w. Backward-compatible wrapper that prints everything (verbose mode).

func ReadDescription added in v1.8.0

func ReadDescription(path string) (string, error)

ReadDescription reads the caption/description from image file metadata.

func SaveBase64Fallback

func SaveBase64Fallback(outputDir, prefix, raw string, index int) string

SaveBase64Fallback saves raw image data as a .txt file (pure base64, no headers). Used when FetchBytes cannot decode the data as an image.

func SaveBase64Image

func SaveBase64Image(outputDir, prefix, b64 string, index int) (string, error)

SaveBase64Image tries to decode and save a base64-encoded image. On success, saves as an image file. On failure, saves the raw base64 data as a text file with instructions for manual conversion.

func SavePrompt

func SavePrompt(outputDir, taskID, prompt string)

SavePrompt writes the generation prompt alongside result files.

func SaveResource

func SaveResource(source, dest string) error

SaveResource saves content from source (HTTP URL, data URI, or base64) to dest. For HTTP URLs uses http.DefaultClient with atomic write.

func SaveResourceWithAuth

func SaveResourceWithAuth(source, apiKey, dest string) error

SaveResourceWithAuth saves an HTTP resource with an Authorization header. Legacy wrapper, prefers SaveResourceWithBearer for new code.

func SaveResourceWithBearer added in v1.10.0

func SaveResourceWithBearer(source, token, dest string) error

SaveResourceWithBearer saves an HTTP resource with an optional Bearer token. If token is empty, falls back to SaveResource. Includes a friendlier 401 error message for HuggingFace gated models.

func WriteDescription added in v1.8.0

func WriteDescription(path, caption string) error

WriteDescription writes a caption/description into the image file metadata.

Types

type AIDetectResult

type AIDetectResult struct {
	AIGenRate float64 `json:"ai_gen_rate"`       // 0-1, higher = more likely AI
	Emoji     string  `json:"emoji"`             // summary emoji (🟢🟡🟠🔴🤖)
	Summary   string  `json:"summary"`           // human-readable summary
	Details   string  `json:"details,omitempty"` // signal breakdown
}

AIDetectResult holds the multi-signal fusion AIGC detection result.

type C2PAResult

type C2PAResult struct {
	Present  bool   `json:"present"`
	Vendor   string `json:"vendor,omitempty"`
	Software string `json:"software,omitempty"`
	Version  string `json:"version,omitempty"`
	Source   string `json:"source,omitempty"`
}

C2PAResult holds C2PA watermark detection results.

type CameraInfo

type CameraInfo struct {
	Make         string `json:"make,omitempty"`
	Model        string `json:"model,omitempty"`
	LensModel    string `json:"lens_model,omitempty"`
	FocalLength  string `json:"focal_length,omitempty"`
	FNumber      string `json:"f_number,omitempty"`
	ISO          string `json:"iso,omitempty"`
	ExposureTime string `json:"exposure_time,omitempty"`
}

CameraInfo holds EXIF camera metadata extracted from JPEG images.

type CompressOptions added in v1.13.1

type CompressOptions struct {
	TargetSize int64  // Target file size in bytes (0 = not set)
	Quality    int    // Fixed quality 1-100 (0 = auto-detect via binary search)
	Format     string // Output format: "jpg", "png", "webp" ("" = keep original)
}

CompressOptions defines how to compress an image.

type CompressResult added in v1.13.1

type CompressResult struct {
	DstPath string // Output file path
	Before  int64  // Original file size in bytes
	After   int64  // Compressed file size in bytes
	Skipped bool   // True if compression was skipped
	Reason  string // Why it was skipped (if Skipped=true)
	Format  string // Output format: jpg, webp, png
	Quality int    // Quality used (0 for palette-based PNG)
}

CompressResult holds the outcome of a compression.

func CompressImage added in v1.13.1

func CompressImage(srcPath string, opts *CompressOptions) (*CompressResult, error)

CompressImage reads an image from srcPath, re-encodes it according to opts, and writes the result alongside the original with a _compress suffix.

TargetSize > 0 → binary-search the highest quality that fits ≤ TargetSize Quality > 0 → encode at that fixed quality (ignores TargetSize) Format == "" → keep the original file extension

Returns a CompressResult describing what happened.

type DetectResult

type DetectResult struct {
	Path      string            `json:"path"`
	Size      int64             `json:"size"`
	SizeHuman string            `json:"size_human"`
	Modified  time.Time         `json:"modified"`
	Format    string            `json:"format"`
	Width     int               `json:"width"`
	Height    int               `json:"height"`
	C2PA      *C2PAResult       `json:"c2pa,omitempty"`
	TC260     *TC260Result      `json:"tc260,omitempty"`
	SynthID   *SynthIDResult    `json:"synthid,omitempty"`
	AIDetect  *AIDetectResult   `json:"ai_detect,omitempty"`
	Camera    *CameraInfo       `json:"camera,omitempty"`
	Metadata  map[string]string `json:"metadata,omitempty"`
	Comment   string            `json:"comment,omitempty"`
	Software  string            `json:"software,omitempty"`
	Caption   string            `json:"caption,omitempty"` // EXIF ImageDescription / XMP description
}

DetectResult holds structured image detection data.

func DetectImage

func DetectImage(path string) (*DetectResult, error)

DetectImage analyzes an image file and returns structured detection data including file stats, format, dimensions, C2PA info, TC260 info, and metadata.

type SynthIDResult

type SynthIDResult struct {
	Present   bool   `json:"present"`
	Likely    bool   `json:"likely"`
	Source    string `json:"source,omitempty"`
	Inference string `json:"inference,omitempty"`
}

SynthIDResult holds SynthID watermark inference results. Note: this is currently metadata-based inference from C2PA manifests. Pixel-level detection requires additional spectral analysis.

type TC260Result

type TC260Result struct {
	Present  bool              `json:"present"`
	Data     string            `json:"data,omitempty"`
	Provider string            `json:"provider,omitempty"`
	Fields   map[string]string `json:"fields,omitempty"`
}

TC260Result holds TC260 AIGC label detection results.

Jump to

Keyboard shortcuts

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