Documentation
¶
Overview ¶
Package storage stores article attachments and raw (plain) emails, following OTRS/Znuny's Ticket::Article::Backend::MIMEBase::ArticleStorage semantics.
Two backends exist:
- DB (ArticleStorageDB): article_data_mime_attachment / article_data_mime_plain.
- FS (ArticleStorageFS): <article dir>/<content_path>/<article_id>/<file>, with <file>.content_type, .content_id, .content_alternative and .disposition sidecar files and plain.txt for the raw email. An existing OTRS/Znuny var/article tree can be mounted and read as-is.
The article body stays in article_data_mime.a_body for both backends, exactly as in OTRS; it is not handled here.
Index ¶
- Constants
- Variables
- func Configure(cfg Config) error
- func ContentPath(t time.Time) string
- func IsHTMLBody(a Attachment) bool
- type ArticleStore
- type Attachment
- type Config
- type DatabaseStore
- func (s *DatabaseStore) Backend() string
- func (s *DatabaseStore) DeleteArticle(ctx context.Context, articleID int64) error
- func (s *DatabaseStore) DeleteAttachment(ctx context.Context, articleID, fileID int64) error
- func (s *DatabaseStore) GetAttachment(ctx context.Context, articleID, fileID int64) (Attachment, []byte, error)
- func (s *DatabaseStore) ListAttachments(ctx context.Context, articleID int64) ([]Attachment, error)
- func (s *DatabaseStore) ReadPlain(ctx context.Context, articleID int64) ([]byte, error)
- func (s *DatabaseStore) Stats(ctx context.Context) (files, size int64, err error)
- func (s *DatabaseStore) WithTx(tx *sql.Tx) ArticleStore
- func (s *DatabaseStore) WriteAttachment(ctx context.Context, articleID int64, in NewAttachment) (Attachment, error)
- func (s *DatabaseStore) WritePlain(ctx context.Context, articleID int64, raw []byte, createBy int) error
- type FilesystemStore
- func (s *FilesystemStore) ArticleDir(ctx context.Context, articleID int64) (string, error)
- func (s *FilesystemStore) Backend() string
- func (s *FilesystemStore) DeleteArticle(ctx context.Context, articleID int64) error
- func (s *FilesystemStore) DeleteAttachment(ctx context.Context, articleID, fileID int64) error
- func (s *FilesystemStore) Dir() string
- func (s *FilesystemStore) GetAttachment(ctx context.Context, articleID, fileID int64) (Attachment, []byte, error)
- func (s *FilesystemStore) ListAttachments(ctx context.Context, articleID int64) ([]Attachment, error)
- func (s *FilesystemStore) ReadPlain(ctx context.Context, articleID int64) ([]byte, error)
- func (s *FilesystemStore) Stats() (files, size int64, err error)
- func (s *FilesystemStore) WithTx(tx *sql.Tx) ArticleStore
- func (s *FilesystemStore) WriteAttachment(ctx context.Context, articleID int64, in NewAttachment) (Attachment, error)
- func (s *FilesystemStore) WritePlain(ctx context.Context, articleID int64, raw []byte, _ int) error
- type NewAttachment
Constants ¶
const ( BackendDB = "DB" BackendFS = "FS" )
Backend names.
const DefaultStorageRoot = "/app/storage"
DefaultStorageRoot is the storage root when neither STORAGE_PATH nor storage.local.path is set.
const HTMLBodyFilename = "html-body.html"
HTMLBodyFilename is the attachment name GoatFlow uses for an article's HTML body (OTRS uses "file-2").
Variables ¶
var ErrNotFound = errors.New("not found")
ErrNotFound is returned when an article, attachment or plain email does not exist.
Functions ¶
func Configure ¶ added in v0.10.0
Configure validates cfg and makes it the backend used by ForDB. For FS the article directory is created when missing.
func ContentPath ¶ added in v0.10.0
ContentPath returns the OTRS article content path ("YYYY/MM/DD") for t.
func IsHTMLBody ¶ added in v0.10.0
func IsHTMLBody(a Attachment) bool
IsHTMLBody reports whether an attachment is the article's HTML body rather than a user-visible attachment (OTRS ExcludeHTMLBody: an inline text/html part named file-2, or GoatFlow's html-body.html).
Types ¶
type ArticleStore ¶ added in v0.10.0
type ArticleStore interface {
// Backend returns BackendDB or BackendFS.
Backend() string
// ListAttachments returns the article's attachments in index order.
ListAttachments(ctx context.Context, articleID int64) ([]Attachment, error)
// GetAttachment returns one attachment and its content, or ErrNotFound.
GetAttachment(ctx context.Context, articleID, fileID int64) (Attachment, []byte, error)
// WriteAttachment stores an attachment. A filename already used by the
// article gets an OTRS-style "-N" suffix.
WriteAttachment(ctx context.Context, articleID int64, a NewAttachment) (Attachment, error)
// DeleteAttachment removes one attachment, or returns ErrNotFound.
DeleteAttachment(ctx context.Context, articleID, fileID int64) error
// WritePlain stores the raw email of an article, replacing any previous one.
WritePlain(ctx context.Context, articleID int64, raw []byte, createBy int) error
// ReadPlain returns the raw email of an article, or ErrNotFound.
ReadPlain(ctx context.Context, articleID int64) ([]byte, error)
// DeleteArticle removes every attachment and the raw email of an article.
DeleteArticle(ctx context.Context, articleID int64) error
// WithTx returns a store whose database work runs on tx.
WithTx(tx *sql.Tx) ArticleStore
}
ArticleStore reads and writes article attachments and raw emails.
func ForDB ¶ added in v0.10.0
func ForDB(db *sql.DB) ArticleStore
ForDB returns the configured article store on db.
type Attachment ¶ added in v0.10.0
type Attachment struct {
ArticleID int64
// FileID addresses the attachment within its article: the
// article_data_mime_attachment id for DB, the 1-based OTRS index (position
// in the sorted file list) for FS.
FileID int64
Filename string
ContentType string
ContentID string
ContentAlternative string
Disposition string
Size int64
// CreateTime is the row's create_time (DB) or the file's modification time (FS).
CreateTime time.Time
// CreateBy is the creating user (DB); 0 for FS, which does not record it.
CreateBy int
}
Attachment describes one attachment of an article (an OTRS attachment index entry).
type Config ¶
type Config struct {
// Backend is BackendDB or BackendFS (case-insensitive).
Backend string
// ArticleDir is the ArticleStorageFS root (OTRS ArticleDataDir); FS only.
ArticleDir string
}
Config selects the article storage backend.
func ConfigFromApp ¶ added in v0.10.0
ConfigFromApp resolves the article storage configuration: STORAGE_TYPE or storage.type picks the backend (DB when unset); the FS tree is <root>/var/article, root being STORAGE_PATH, storage.local.path or DefaultStorageRoot. cfg may be nil.
type DatabaseStore ¶ added in v0.10.0
type DatabaseStore struct {
// contains filtered or unexported fields
}
DatabaseStore keeps attachments in article_data_mime_attachment and raw emails in article_data_mime_plain (OTRS ArticleStorageDB).
func NewDatabaseStore ¶ added in v0.10.0
func NewDatabaseStore(db *sql.DB) *DatabaseStore
NewDatabaseStore returns a DB-backed article store.
func (*DatabaseStore) Backend ¶ added in v0.10.0
func (s *DatabaseStore) Backend() string
Backend returns BackendDB.
func (*DatabaseStore) DeleteArticle ¶ added in v0.10.0
func (s *DatabaseStore) DeleteArticle(ctx context.Context, articleID int64) error
DeleteArticle removes the article's attachments and raw email rows.
func (*DatabaseStore) DeleteAttachment ¶ added in v0.10.0
func (s *DatabaseStore) DeleteAttachment(ctx context.Context, articleID, fileID int64) error
DeleteAttachment deletes one attachment row.
func (*DatabaseStore) GetAttachment ¶ added in v0.10.0
func (s *DatabaseStore) GetAttachment(ctx context.Context, articleID, fileID int64) (Attachment, []byte, error)
GetAttachment returns one attachment row and its content.
func (*DatabaseStore) ListAttachments ¶ added in v0.10.0
func (s *DatabaseStore) ListAttachments(ctx context.Context, articleID int64) ([]Attachment, error)
ListAttachments returns the article's attachments ordered by id.
func (*DatabaseStore) Stats ¶ added in v0.10.0
func (s *DatabaseStore) Stats(ctx context.Context) (files, size int64, err error)
Stats counts attachment and plain-email rows and their bytes.
func (*DatabaseStore) WithTx ¶ added in v0.10.0
func (s *DatabaseStore) WithTx(tx *sql.Tx) ArticleStore
WithTx returns a store running on tx.
func (*DatabaseStore) WriteAttachment ¶ added in v0.10.0
func (s *DatabaseStore) WriteAttachment(ctx context.Context, articleID int64, in NewAttachment) (Attachment, error)
WriteAttachment inserts an attachment row.
func (*DatabaseStore) WritePlain ¶ added in v0.10.0
func (s *DatabaseStore) WritePlain(ctx context.Context, articleID int64, raw []byte, createBy int) error
WritePlain replaces the article's raw email.
type FilesystemStore ¶ added in v0.10.0
type FilesystemStore struct {
// contains filtered or unexported fields
}
FilesystemStore keeps attachments and raw emails in an OTRS ArticleStorageFS tree: <dir>/<content_path>/<article_id>/. The content path comes from article_data_mime.content_path, or from the article's create_time when unset.
func NewFilesystemStore ¶ added in v0.10.0
func NewFilesystemStore(dir string, db *sql.DB) *FilesystemStore
NewFilesystemStore returns an FS-backed article store rooted at dir (the OTRS ArticleDataDir, e.g. /opt/otrs/var/article).
func (*FilesystemStore) ArticleDir ¶ added in v0.10.0
ArticleDir returns the article's directory, or ErrNotFound when the article does not exist.
func (*FilesystemStore) Backend ¶ added in v0.10.0
func (s *FilesystemStore) Backend() string
Backend returns BackendFS.
func (*FilesystemStore) DeleteArticle ¶ added in v0.10.0
func (s *FilesystemStore) DeleteArticle(ctx context.Context, articleID int64) error
DeleteArticle removes the article directory.
func (*FilesystemStore) DeleteAttachment ¶ added in v0.10.0
func (s *FilesystemStore) DeleteAttachment(ctx context.Context, articleID, fileID int64) error
DeleteAttachment removes attachment number fileID and its sidecars. Later attachments of the article move down one index.
func (*FilesystemStore) Dir ¶ added in v0.10.0
func (s *FilesystemStore) Dir() string
Dir returns the root directory of the tree.
func (*FilesystemStore) GetAttachment ¶ added in v0.10.0
func (s *FilesystemStore) GetAttachment(ctx context.Context, articleID, fileID int64) (Attachment, []byte, error)
GetAttachment returns attachment number fileID of the article and its content.
func (*FilesystemStore) ListAttachments ¶ added in v0.10.0
func (s *FilesystemStore) ListAttachments(ctx context.Context, articleID int64) ([]Attachment, error)
ListAttachments returns the article's attachments in OTRS index order. An unknown article has no attachments.
func (*FilesystemStore) Stats ¶ added in v0.10.0
func (s *FilesystemStore) Stats() (files, size int64, err error)
Stats counts the content files (attachments and plain.txt, not sidecars) in the tree and their bytes.
func (*FilesystemStore) WithTx ¶ added in v0.10.0
func (s *FilesystemStore) WithTx(tx *sql.Tx) ArticleStore
WithTx returns a store whose article lookups run on tx (file writes are not transactional).
func (*FilesystemStore) WriteAttachment ¶ added in v0.10.0
func (s *FilesystemStore) WriteAttachment(ctx context.Context, articleID int64, in NewAttachment) (Attachment, error)
WriteAttachment writes the attachment and its sidecar files.
func (*FilesystemStore) WritePlain ¶ added in v0.10.0
WritePlain writes plain.txt.