imagestorage

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package imagestorage provides image storage management for cdev.

Package imagestorage provides image storage management for cdev. It handles storing, retrieving, and cleaning up uploaded images with configurable limits, TTL, and security validations.

Index

Constants

View Source
const (
	MaxImagesCount   = 50              // Max images in folder
	MaxTotalSizeMB   = 100             // Max total size in MB
	MaxSingleImageMB = 10              // Max single image in MB
	ImageTTL         = 1 * time.Hour   // Auto-cleanup after 1 hour
	CleanupInterval  = 5 * time.Minute // Check every 5 minutes
	CdevImagesDir    = ".cdev/images"  // Relative path within workspace
)

Storage limits and configuration constants.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages per-workspace image storage instances. It creates storage on demand when a workspace is first accessed.

func NewManager

func NewManager(resolver WorkspacePathResolver) *Manager

NewManager creates a new ImageStorageManager.

func (*Manager) Close

func (m *Manager) Close()

Close closes all storage instances.

func (*Manager) CloseWorkspace

func (m *Manager) CloseWorkspace(workspaceID string)

CloseWorkspace closes and removes the storage for a specific workspace. Use this when a workspace is removed.

func (*Manager) GetStorage

func (m *Manager) GetStorage(workspaceID string) (*Storage, error)

GetStorage returns the image storage for a workspace. Creates the storage on demand if it doesn't exist.

func (*Manager) ListWorkspaces

func (m *Manager) ListWorkspaces() []string

ListWorkspaces returns the IDs of workspaces that have active storage.

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

Storage manages image storage with limits and cleanup.

func New

func New(workspacePath string) (*Storage, error)

New creates a new ImageStorage instance for a workspace. The workspacePath is the root path of the workspace - images will be stored in {workspacePath}/.cdev/images/

func (*Storage) CanAcceptUpload

func (s *Storage) CanAcceptUpload(sizeBytes int64) (bool, string)

CanAcceptUpload checks if the storage can accept an upload of the given size.

func (*Storage) Cleanup

func (s *Storage) Cleanup()

Cleanup removes expired images.

func (*Storage) Clear

func (s *Storage) Clear() error

Clear removes all images.

func (*Storage) Close

func (s *Storage) Close()

Close stops the storage and cleanup goroutine. Safe to call multiple times.

func (*Storage) Delete

func (s *Storage) Delete(id string) error

Delete removes an image by ID.

func (*Storage) Get

func (s *Storage) Get(id string) (*StoredImage, error)

Get retrieves an image by ID.

func (*Storage) GetLocalPath

func (s *Storage) GetLocalPath(id string) (string, error)

GetLocalPath returns the local path for an image ID. This is the path that can be passed to Claude CLI.

func (*Storage) GetStats

func (s *Storage) GetStats() StorageStats

GetStats returns current storage statistics.

func (*Storage) List

func (s *Storage) List() []*StoredImage

List returns all stored images sorted by creation time (newest first).

func (*Storage) Store

func (s *Storage) Store(reader io.Reader, mimeType string) (*StoredImage, error)

Store saves an image from a reader and returns the stored image info. It validates the content, checks limits, and handles deduplication.

func (*Storage) ValidatePath

func (s *Storage) ValidatePath(path string) error

ValidatePath checks if a path is valid and within the images directory. This prevents path traversal attacks using multiple techniques: 1. Clean the path to resolve "..", ".", and redundant separators 2. Verify the cleaned path is still under the images directory 3. Check the path doesn't escape via symlinks

type StorageStats

type StorageStats struct {
	ImageCount     int       `json:"image_count"`
	TotalSizeBytes int64     `json:"total_size_bytes"`
	OldestImage    time.Time `json:"oldest_image"`
	NewestImage    time.Time `json:"newest_image"`
}

StorageStats contains current storage statistics.

type StoredImage

type StoredImage struct {
	ID          string    `json:"id"`
	LocalPath   string    `json:"local_path"` // Relative path: .cdev/images/xxx.jpg (for Claude in workspace)
	FullPath    string    `json:"-"`          // Absolute path (internal use)
	MimeType    string    `json:"mime_type"`
	Size        int64     `json:"size"`
	ContentHash string    `json:"content_hash"` // SHA256 hash for deduplication
	CreatedAt   time.Time `json:"created_at"`
	ExpiresAt   time.Time `json:"expires_at"`
}

StoredImage represents a stored image with metadata.

type WorkspacePathResolver

type WorkspacePathResolver interface {
	// GetWorkspacePath returns the absolute path for a workspace ID.
	// Returns an error if the workspace is not found.
	GetWorkspacePath(workspaceID string) (string, error)
}

WorkspacePathResolver is an interface for looking up workspace paths by ID. This decouples the ImageStorageManager from the workspace package.

Jump to

Keyboard shortcuts

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