transfer

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2026 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package transfer provides import/export functionality for oCMS content.

Index

Constants

View Source
const ExportVersion = "1.0"

ExportVersion is the current version of the export format.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConflictStrategy

type ConflictStrategy string

ConflictStrategy defines how to handle conflicts during import.

const (
	// ConflictSkip skips importing items that already exist.
	ConflictSkip ConflictStrategy = "skip"
	// ConflictOverwrite updates existing items with imported data.
	ConflictOverwrite ConflictStrategy = "overwrite"
	// ConflictRename creates a new item with a modified slug if one exists.
	ConflictRename ConflictStrategy = "rename"
)

type ExportCategory

type ExportCategory struct {
	ID           int64            `json:"id"`
	Name         string           `json:"name"`
	Slug         string           `json:"slug"`
	Description  string           `json:"description,omitempty"`
	ParentSlug   string           `json:"parent_slug,omitempty"`
	Position     int64            `json:"position"`
	LanguageCode string           `json:"language_code,omitempty"`
	Translations map[string]int64 `json:"translations,omitempty"`
	CreatedAt    time.Time        `json:"created_at"`
	UpdatedAt    time.Time        `json:"updated_at"`
}

ExportCategory represents a category with hierarchy support.

type ExportData

type ExportData struct {
	Version    string            `json:"version"`
	ExportedAt time.Time         `json:"exported_at"`
	Site       ExportSite        `json:"site"`
	Languages  []ExportLanguage  `json:"languages,omitempty"`
	Users      []ExportUser      `json:"users,omitempty"`
	Pages      []ExportPage      `json:"pages,omitempty"`
	Categories []ExportCategory  `json:"categories,omitempty"`
	Tags       []ExportTag       `json:"tags,omitempty"`
	Media      []ExportMedia     `json:"media,omitempty"`
	Menus      []ExportMenu      `json:"menus,omitempty"`
	Forms      []ExportForm      `json:"forms,omitempty"`
	Config     map[string]string `json:"config,omitempty"`
}

ExportData represents the complete export structure.

type ExportForm

type ExportForm struct {
	ID             int64                  `json:"id"`
	Name           string                 `json:"name"`
	Slug           string                 `json:"slug"`
	Title          string                 `json:"title"`
	Description    string                 `json:"description,omitempty"`
	SuccessMessage string                 `json:"success_message,omitempty"`
	EmailTo        string                 `json:"email_to,omitempty"`
	IsActive       bool                   `json:"is_active"`
	LanguageCode   string                 `json:"language_code,omitempty"` // Language code for this form
	Fields         []ExportFormField      `json:"fields,omitempty"`
	Submissions    []ExportFormSubmission `json:"submissions,omitempty"`
	CreatedAt      time.Time              `json:"created_at"`
	UpdatedAt      time.Time              `json:"updated_at"`
}

ExportForm represents a form definition.

type ExportFormField

type ExportFormField struct {
	Type        string `json:"type"`
	Name        string `json:"name"`
	Label       string `json:"label"`
	Placeholder string `json:"placeholder,omitempty"`
	HelpText    string `json:"help_text,omitempty"`
	Options     string `json:"options,omitempty"`
	Validation  string `json:"validation,omitempty"`
	IsRequired  bool   `json:"is_required"`
	Position    int64  `json:"position"`
}

ExportFormField represents a form field.

type ExportFormSubmission

type ExportFormSubmission struct {
	Data      string    `json:"data"`
	IPAddress string    `json:"ip_address,omitempty"`
	UserAgent string    `json:"user_agent,omitempty"`
	IsRead    bool      `json:"is_read"`
	CreatedAt time.Time `json:"created_at"`
}

ExportFormSubmission represents a form submission.

type ExportLanguage

type ExportLanguage struct {
	Code       string `json:"code"`
	Name       string `json:"name"`
	NativeName string `json:"native_name"`
	IsDefault  bool   `json:"is_default"`
	IsActive   bool   `json:"is_active"`
	Direction  string `json:"direction"`
	Position   int64  `json:"position"`
}

ExportLanguage represents a language configuration.

type ExportMedia

type ExportMedia struct {
	UUID         string          `json:"uuid"`
	Filename     string          `json:"filename"`
	MimeType     string          `json:"mime_type"`
	Size         int64           `json:"size"`
	Width        *int64          `json:"width,omitempty"`
	Height       *int64          `json:"height,omitempty"`
	Alt          string          `json:"alt,omitempty"`
	Caption      string          `json:"caption,omitempty"`
	FolderPath   string          `json:"folder_path,omitempty"`
	UploadedBy   string          `json:"uploaded_by"`
	LanguageCode string          `json:"language_code,omitempty"` // Language code for this media item
	Variants     []ExportVariant `json:"variants,omitempty"`
	FilePath     string          `json:"file_path,omitempty"` // Path within zip archive (e.g., "media/originals/{uuid}/{filename}")
	CreatedAt    time.Time       `json:"created_at"`
}

ExportMedia represents media metadata (not the actual file).

type ExportMediaRef

type ExportMediaRef struct {
	UUID     string `json:"uuid"`
	Filename string `json:"filename"`
}

ExportMediaRef is a reference to a media item by UUID.

type ExportMenu

type ExportMenu struct {
	ID           int64            `json:"id"`
	Name         string           `json:"name"`
	Slug         string           `json:"slug"`
	LanguageCode string           `json:"language_code,omitempty"`
	Items        []ExportMenuItem `json:"items,omitempty"`
	CreatedAt    time.Time        `json:"created_at"`
	UpdatedAt    time.Time        `json:"updated_at"`
}

ExportMenu represents a navigation menu.

type ExportMenuItem

type ExportMenuItem struct {
	ID       int64            `json:"id"`
	Title    string           `json:"title"`
	URL      string           `json:"url,omitempty"`
	Target   string           `json:"target,omitempty"`
	PageSlug string           `json:"page_slug,omitempty"`
	CSSClass string           `json:"css_class,omitempty"`
	IsActive bool             `json:"is_active"`
	Position int64            `json:"position"`
	Children []ExportMenuItem `json:"children,omitempty"`
}

ExportMenuItem represents a menu item.

type ExportOptions

type ExportOptions struct {
	IncludeUsers       bool   `json:"include_users"`
	IncludePages       bool   `json:"include_pages"`
	IncludeCategories  bool   `json:"include_categories"`
	IncludeTags        bool   `json:"include_tags"`
	IncludeMedia       bool   `json:"include_media"`
	IncludeMediaFiles  bool   `json:"include_media_files"` // Include actual media files (creates zip)
	IncludeMenus       bool   `json:"include_menus"`
	IncludeForms       bool   `json:"include_forms"`
	IncludeSubmissions bool   `json:"include_submissions"`
	IncludeConfig      bool   `json:"include_config"`
	IncludeLanguages   bool   `json:"include_languages"`
	PageStatus         string `json:"page_status"` // "all", "published", "draft"
}

ExportOptions configures what to include in the export.

func DefaultExportOptions

func DefaultExportOptions() ExportOptions

DefaultExportOptions returns options that include everything.

type ExportPage

type ExportPage struct {
	ID            int64            `json:"id"`
	Title         string           `json:"title"`
	Slug          string           `json:"slug"`
	Body          string           `json:"body"`
	Status        string           `json:"status"`
	AuthorEmail   string           `json:"author_email"`
	Categories    []string         `json:"categories,omitempty"`
	Tags          []string         `json:"tags,omitempty"`
	SEO           *ExportPageSEO   `json:"seo,omitempty"`
	FeaturedImage *ExportMediaRef  `json:"featured_image,omitempty"`
	LanguageCode  string           `json:"language_code,omitempty"`
	Translations  map[string]int64 `json:"translations,omitempty"`
	CreatedAt     time.Time        `json:"created_at"`
	UpdatedAt     time.Time        `json:"updated_at"`
	PublishedAt   *time.Time       `json:"published_at,omitempty"`
	ScheduledAt   *time.Time       `json:"scheduled_at,omitempty"`
}

ExportPage represents a page with all its relationships.

type ExportPageSEO

type ExportPageSEO struct {
	MetaTitle       string          `json:"meta_title,omitempty"`
	MetaDescription string          `json:"meta_description,omitempty"`
	MetaKeywords    string          `json:"meta_keywords,omitempty"`
	OgImage         *ExportMediaRef `json:"og_image,omitempty"`
	NoIndex         bool            `json:"no_index,omitempty"`
	NoFollow        bool            `json:"no_follow,omitempty"`
	CanonicalURL    string          `json:"canonical_url,omitempty"`
}

ExportPageSEO contains SEO metadata for a page.

type ExportSite

type ExportSite struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	URL         string `json:"url,omitempty"`
}

ExportSite contains basic site information.

type ExportTag

type ExportTag struct {
	ID           int64            `json:"id"`
	Name         string           `json:"name"`
	Slug         string           `json:"slug"`
	LanguageCode string           `json:"language_code,omitempty"`
	Translations map[string]int64 `json:"translations,omitempty"`
	CreatedAt    time.Time        `json:"created_at"`
	UpdatedAt    time.Time        `json:"updated_at"`
}

ExportTag represents a tag.

type ExportUser

type ExportUser struct {
	Email     string    `json:"email"`
	Name      string    `json:"name"`
	Role      string    `json:"role"`
	CreatedAt time.Time `json:"created_at"`
}

ExportUser represents a user for export (no passwords).

type ExportVariant

type ExportVariant struct {
	Type     string `json:"type"`
	Width    int64  `json:"width"`
	Height   int64  `json:"height"`
	Size     int64  `json:"size"`
	FilePath string `json:"file_path,omitempty"` // Path within zip archive
}

ExportVariant represents a media variant (thumbnail, etc.).

type Exporter

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

Exporter handles exporting CMS content to JSON format.

func NewExporter

func NewExporter(queries *store.Queries, logger *slog.Logger) *Exporter

NewExporter creates a new Exporter instance.

func (*Exporter) Export

func (e *Exporter) Export(ctx context.Context, opts ExportOptions) (*ExportData, error)

Export generates an ExportData structure based on the provided options.

func (*Exporter) ExportToFile

func (e *Exporter) ExportToFile(ctx context.Context, opts ExportOptions, path string) error

ExportToFile writes the export as JSON to a file.

func (*Exporter) ExportToWriter

func (e *Exporter) ExportToWriter(ctx context.Context, opts ExportOptions, w io.Writer) error

ExportToWriter writes the export as JSON to the provided writer.

func (*Exporter) ExportWithMedia

func (e *Exporter) ExportWithMedia(ctx context.Context, opts ExportOptions, w io.Writer) error

ExportWithMedia creates a zip archive containing export.json and media files.

func (*Exporter) ExportWithMediaToFile

func (e *Exporter) ExportWithMediaToFile(ctx context.Context, opts ExportOptions, path string) error

ExportWithMediaToFile creates a zip archive file containing export.json and media files.

func (*Exporter) SetUploadDir

func (e *Exporter) SetUploadDir(dir string)

SetUploadDir sets the upload directory for media files.

type IDMapping

type IDMapping map[int64]int64

IDMapping maps old export IDs to new database IDs.

type ImportError

type ImportError struct {
	Entity  string `json:"entity"`
	ID      string `json:"id"`
	Message string `json:"message"`
}

ImportError represents an error that occurred during import.

type ImportOptions

type ImportOptions struct {
	DryRun           bool             `json:"dry_run"`
	ConflictStrategy ConflictStrategy `json:"conflict_strategy"`
	ImportUsers      bool             `json:"import_users"`
	ImportPages      bool             `json:"import_pages"`
	ImportCategories bool             `json:"import_categories"`
	ImportTags       bool             `json:"import_tags"`
	ImportMedia      bool             `json:"import_media"`
	ImportMediaFiles bool             `json:"import_media_files"` // Copy media files from zip to uploads
	ImportMenus      bool             `json:"import_menus"`
	ImportForms      bool             `json:"import_forms"`
	ImportConfig     bool             `json:"import_config"`
	ImportLanguages  bool             `json:"import_languages"`
}

ImportOptions configures what to import and how to handle conflicts.

func DefaultImportOptions

func DefaultImportOptions() ImportOptions

DefaultImportOptions returns options that import everything with skip strategy.

type ImportResult

type ImportResult struct {
	Success bool                 `json:"success"`
	DryRun  bool                 `json:"dry_run"`
	Created map[string]int       `json:"created"`
	Updated map[string]int       `json:"updated"`
	Skipped map[string]int       `json:"skipped"`
	Errors  []ImportError        `json:"errors,omitempty"`
	IDMaps  map[string]IDMapping `json:"-"` // Internal: maps old IDs to new IDs
}

ImportResult contains the results of an import operation.

func NewImportResult

func NewImportResult(dryRun bool) *ImportResult

NewImportResult creates a new ImportResult with initialized maps.

func (*ImportResult) AddError

func (r *ImportResult) AddError(entity, id, message string)

AddError adds an error to the import result.

func (*ImportResult) GetIDMap

func (r *ImportResult) GetIDMap(entity string) IDMapping

GetIDMap returns the ID mapping for an entity type, creating one if needed.

func (*ImportResult) IncrementCreated

func (r *ImportResult) IncrementCreated(entity string)

IncrementCreated increments the created count for an entity type.

func (*ImportResult) IncrementSkipped

func (r *ImportResult) IncrementSkipped(entity string)

IncrementSkipped increments the skipped count for an entity type.

func (*ImportResult) IncrementUpdated

func (r *ImportResult) IncrementUpdated(entity string)

IncrementUpdated increments the updated count for an entity type.

func (*ImportResult) TotalCreated

func (r *ImportResult) TotalCreated() int

TotalCreated returns the total count of created items.

func (*ImportResult) TotalSkipped

func (r *ImportResult) TotalSkipped() int

TotalSkipped returns the total count of skipped items.

func (*ImportResult) TotalUpdated

func (r *ImportResult) TotalUpdated() int

TotalUpdated returns the total count of updated items.

type Importer

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

Importer handles importing CMS content from JSON format.

func NewImporter

func NewImporter(queries *store.Queries, db *sql.DB, logger *slog.Logger) *Importer

NewImporter creates a new Importer instance.

func (*Importer) Import

func (i *Importer) Import(ctx context.Context, data *ExportData, opts ImportOptions) (*ImportResult, error)

Import performs the import operation based on the provided options. The import runs in a transaction and rolls back on error.

func (*Importer) ImportFromFile

func (i *Importer) ImportFromFile(ctx context.Context, path string, opts ImportOptions) (*ImportResult, error)

ImportFromFile reads and imports from a file path.

func (*Importer) ImportFromReader

func (i *Importer) ImportFromReader(ctx context.Context, r io.Reader, opts ImportOptions) (*ImportResult, error)

ImportFromReader reads and imports from an io.Reader.

func (*Importer) ImportFromZip

func (i *Importer) ImportFromZip(ctx context.Context, zipReader *zip.Reader, opts ImportOptions) (*ImportResult, error)

ImportFromZip imports from a zip archive containing export.json and media files.

func (*Importer) ImportFromZipBytes

func (i *Importer) ImportFromZipBytes(ctx context.Context, data []byte, opts ImportOptions) (*ImportResult, error)

ImportFromZipBytes imports from zip archive bytes (useful for HTTP uploads).

func (*Importer) ImportFromZipFile

func (i *Importer) ImportFromZipFile(ctx context.Context, path string, opts ImportOptions) (*ImportResult, error)

ImportFromZipFile imports from a zip file path.

func (*Importer) SetProcessor

func (i *Importer) SetProcessor(p *imaging.Processor)

SetProcessor sets the imaging processor for generating image variants.

func (*Importer) SetUploadDir

func (i *Importer) SetUploadDir(dir string)

SetUploadDir sets the upload directory for media files.

func (*Importer) Validate

func (i *Importer) Validate(data *ExportData) []ImportError

Validate validates the import data without making changes.

func (*Importer) ValidateData

func (i *Importer) ValidateData(ctx context.Context, data *ExportData) (*ValidationResult, error)

ValidateData validates import data and checks for conflicts.

func (*Importer) ValidateFile

func (i *Importer) ValidateFile(ctx context.Context, path string) (*ValidationResult, error)

ValidateFile validates an import file and returns information about its contents.

func (*Importer) ValidateReader

func (i *Importer) ValidateReader(ctx context.Context, r io.Reader) (*ValidationResult, error)

ValidateReader validates import data from a reader.

func (*Importer) ValidateZip

func (i *Importer) ValidateZip(ctx context.Context, zipReader *zip.Reader) (*ValidationResult, error)

ValidateZip validates a zip archive and returns information about its contents.

func (*Importer) ValidateZipBytes

func (i *Importer) ValidateZipBytes(ctx context.Context, data []byte) (*ValidationResult, error)

ValidateZipBytes validates zip data and returns information about its contents.

func (*Importer) ValidateZipFile

func (i *Importer) ValidateZipFile(ctx context.Context, path string) (*ValidationResult, error)

ValidateZipFile validates a zip import file and returns information about its contents.

type ValidationResult

type ValidationResult struct {
	Valid     bool                `json:"valid"`
	Version   string              `json:"version"`
	Entities  map[string]int      `json:"entities"`
	Conflicts map[string][]string `json:"conflicts,omitempty"`
	Errors    []ImportError       `json:"errors,omitempty"`
}

ValidationResult contains the results of validating an import file.

Jump to

Keyboard shortcuts

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