Documentation
ΒΆ
Index ΒΆ
- Constants
- Variables
- func BindRead(ctx context.Context, fn func(ctx context.Context, b *Binder) error) error
- func BindReadResult[T any](ctx context.Context, fn func(ctx context.Context, b *Binder) (*T, error)) (*T, error)
- func Configure(config Config) error
- func Copy(dst io.Writer, src io.Reader) (int64, error)
- func NopCloser(r io.Reader) io.ReadCloser
- func Read(ctx context.Context, source StreamReader, fn ReadFunc) error
- func ReadFileLines(ctx context.Context, path string, fn LineFunc) error
- func ReadLines(ctx context.Context, src StreamReader, fn LineFunc) error
- func ReadList(ctx context.Context, sources []StreamReader, fn ReadListFunc) error
- func ReadListResult[T any](ctx context.Context, sources []StreamReader, ...) (*T, error)
- func ReadResult[T any](ctx context.Context, source StreamReader, ...) (*T, error)
- func Size(ctx context.Context, source StreamReader) (int64, error)
- func SizeFromPath(path string) (int64, error)
- func SizeFromReader(r io.Reader) int64
- func SizeFromStream(sr StreamReader) int64
- func ToExt(format string) string
- func WithSession(ctx context.Context, ses IoSession) context.Context
- func WriteFile(r io.Reader, path string) (int64, error)
- func WriteStreamToFile(src StreamReader, path string) (int64, error)
- type Binder
- type BytesReader
- type Config
- type DownloadReaderCloser
- type FileReader
- type GenericReader
- type InOption
- type IoManager
- type IoSession
- type LineFunc
- type MultipartReader
- type OutConfig
- type Output
- func BindProcess(ctx context.Context, out OutConfig, ...) (*Output, error)
- func BindProcessResult[T any](ctx context.Context, out OutConfig, ...) (*Output, *T, error)
- func Process(ctx context.Context, source StreamReader, out OutConfig, fn ProcessFunc) (*Output, error)
- func ProcessList(ctx context.Context, sources []StreamReader, out OutConfig, fn ProcessListFunc) (*Output, error)
- func ProcessListResult[T any](ctx context.Context, sources []StreamReader, out OutConfig, ...) (*Output, *T, error)
- func ProcessResult[T any](ctx context.Context, source StreamReader, out OutConfig, ...) (*Output, *T, error)
- func ToOutput(ctx context.Context, src StreamReader, out OutConfig) (*Output, error)
- func (o *Output) AsReaderAt(ctx context.Context, opts ...ToReaderAtOption) (*ReaderAtResult, error)
- func (o *Output) Bytes() ([]byte, error)
- func (o *Output) Data() []byte
- func (o *Output) Keep() *Output
- func (o *Output) OpenReader() (io.ReadCloser, error)
- func (o *Output) OpenWriter() (io.WriteCloser, error)
- func (o *Output) Path() string
- func (o *Output) Reader() StreamReader
- func (o *Output) SaveAs(path string) error
- func (o *Output) StorageType() StorageType
- func (o *Output) WriteTo(w io.Writer) (int64, error)
- type ProcessFunc
- type ProcessListFunc
- type ReadFunc
- type ReadListFunc
- type ReaderAtResult
- type ReaderList
- type StorageType
- type StreamReader
- type ToReaderAtOption
- type ToReaderAtOptions
- type URLReader
- type URLReaderOptions
- type UseReader
Constants ΒΆ
const ( Json = ".json" Csv = ".csv" Txt = ".txt" Xml = ".xml" Pdf = ".pdf" Docx = ".docx" Xlsx = ".xlsx" Pptx = ".pptx" Jpg = ".jpg" Jpeg = ".jpeg" Png = ".png" Zip = ".zip" )
const ( Memory = StorageMemory File = StorageFile )
Shorthand aliases
const (
DefaultBaseTempDir = "./temp"
)
Variables ΒΆ
var ( ErrNilSource = errors.New("sio: nil source") ErrOpenFailed = errors.New("sio: cannot open reader") ErrIoManagerClosed = errors.New("sio: manager is closed") ErrIoSessionClosed = errors.New("sio: session is closed") ErrInvalidURL = errors.New("sio: invalid URL") ErrDownloadFailed = errors.New("sio: download failed") ErrNoSession = errors.New("sio: session is nil") ErrInvalidSessionType = errors.New("sio: invalid session type") )
Functions ΒΆ
func BindRead ΒΆ added in v0.0.7
BindRead opens multiple StreamReaders lazily through Binder and executes fn. This is standalone (no session required).
func BindReadResult ΒΆ added in v0.0.7
func BindReadResult[T any](ctx context.Context, fn func(ctx context.Context, b *Binder) (*T, error)) (*T, error)
BindReadResult is like BindRead but also returns a result value.
func Read ΒΆ
func Read(ctx context.Context, source StreamReader, fn ReadFunc) error
Read is a convenience wrapper that gets IoSession from context and calls ses.Read(...). If there is no session, it falls back to direct streaming from the StreamReader.
func ReadFileLines ΒΆ added in v0.0.4
func ReadLines ΒΆ added in v0.0.4
func ReadLines(ctx context.Context, src StreamReader, fn LineFunc) error
func ReadList ΒΆ added in v0.0.3
func ReadList(ctx context.Context, sources []StreamReader, fn ReadListFunc) error
ReadList is a convenience wrapper for IoSession.ReadList.
func ReadListResult ΒΆ added in v0.0.3
func ReadResult ΒΆ added in v0.0.3
func SizeFromPath ΒΆ added in v0.0.7
func SizeFromReader ΒΆ added in v0.0.7
func SizeFromStream ΒΆ added in v0.0.7
func SizeFromStream(sr StreamReader) int64
func WithSession ΒΆ added in v0.0.3
WithSession attaches a IoSession into context.
func WriteStreamToFile ΒΆ added in v0.0.5
func WriteStreamToFile(src StreamReader, path string) (int64, error)
Types ΒΆ
type Binder ΒΆ added in v0.0.7
type Binder struct {
// contains filtered or unexported fields
}
Binder collects StreamReaders and opens them eagerly on registration.
type BytesReader ΒΆ
type BytesReader struct {
// contains filtered or unexported fields
}
BytesReader exposes an in-memory []byte as a StreamReader. Cleanup() optionally clears the underlying buffer.
func NewBytesReader ΒΆ
func NewBytesReader(data []byte) *BytesReader
func (*BytesReader) Cleanup ΒΆ
func (b *BytesReader) Cleanup() error
func (*BytesReader) Data ΒΆ
func (b *BytesReader) Data() []byte
Data returns the underlying byte slice.
func (*BytesReader) Open ΒΆ
func (b *BytesReader) Open() (io.ReadCloser, error)
func (*BytesReader) Size ΒΆ added in v0.0.7
func (b *BytesReader) Size() int64
type Config ΒΆ added in v0.0.5
type Config struct {
// contains filtered or unexported fields
}
type DownloadReaderCloser ΒΆ
DownloadReaderCloser is a convenience interface for a read-only stream that also supports Close.
func NewDownloadReaderCloser ΒΆ
func NewDownloadReaderCloser(streamReader StreamReader, cleanup ...func()) (DownloadReaderCloser, error)
NewDownloadReaderCloser wraps a StreamReader as a DownloadReaderCloser.
type FileReader ΒΆ
type FileReader struct {
// contains filtered or unexported fields
}
FileReader reads from a file path on disk. Cleanup() does NOT remove the original file.
func NewFileReader ΒΆ
func NewFileReader(path string) *FileReader
func (*FileReader) Cleanup ΒΆ
func (f *FileReader) Cleanup() error
func (*FileReader) Open ΒΆ
func (f *FileReader) Open() (io.ReadCloser, error)
func (*FileReader) Path ΒΆ
func (f *FileReader) Path() string
Path returns the underlying file path.
func (*FileReader) Size ΒΆ added in v0.0.7
func (f *FileReader) Size() int64
type GenericReader ΒΆ added in v0.0.7
type GenericReader struct {
// contains filtered or unexported fields
}
GenericReader wraps an uploaded io.Reader. Cleanup() does nothing because frameworks handle their own temp files.
func NewGenericReader ΒΆ added in v0.0.7
func NewGenericReader(r io.Reader) *GenericReader
func NewPartReader ΒΆ added in v0.0.3
func NewPartReader(p *multipart.Part) *GenericReader
NewPartReader wraps multipart.Part as StreamReader (streaming, no buffering).
func (*GenericReader) Cleanup ΒΆ added in v0.0.7
func (m *GenericReader) Cleanup() error
func (*GenericReader) Open ΒΆ added in v0.0.7
func (m *GenericReader) Open() (io.ReadCloser, error)
type InOption ΒΆ added in v0.0.3
type InOption func(*inConfig)
InOption is a functional option for In(...).
func DeleteAfterUse ΒΆ added in v0.0.3
func DeleteAfterUse() InOption
DeleteAfterUse tells In(...) to delete the underlying file after the StreamReader is cleaned up (only works for *FileReader).
type IoManager ΒΆ added in v0.0.3
func NewIoManager ΒΆ added in v0.0.3
func NewIoManager(baseDir string, storageType ...StorageType) (IoManager, error)
NewIoManager creates a new IoManager.
- baseDir == "" β create a temp folder using os.MkdirTemp("", "sio-")
- baseDir != "" β create/use the provided directory
- storageType: optional parameter to specify storage strategy (default: StorageFile)
type IoSession ΒΆ added in v0.0.3
type IoSession interface {
Read(ctx context.Context, source StreamReader, fn ReadFunc) error
ReadList(ctx context.Context, sources []StreamReader, fn ReadListFunc) error
Process(ctx context.Context, source StreamReader, out OutConfig, fn ProcessFunc) (*Output, error)
ProcessList(ctx context.Context, sources []StreamReader, out OutConfig, fn ProcessListFunc) (*Output, error)
Cleanup() error
}
type MultipartReader ΒΆ
type MultipartReader struct {
// contains filtered or unexported fields
}
MultipartReader wraps an uploaded multipart.FileHeader. Cleanup() does nothing because frameworks handle their own temp files.
func NewMultipartReader ΒΆ
func NewMultipartReader(fh *multipart.FileHeader) *MultipartReader
func (*MultipartReader) Cleanup ΒΆ
func (m *MultipartReader) Cleanup() error
func (*MultipartReader) Open ΒΆ
func (m *MultipartReader) Open() (io.ReadCloser, error)
type OutConfig ΒΆ added in v0.0.7
type OutConfig struct {
// contains filtered or unexported fields
}
OutConfig configures output behavior
func Out ΒΆ added in v0.0.7
func Out(ext string, opts ...StorageType) OutConfig
Out creates output configuration.
Usage:
sio.Out(".pdf") // use session default
sio.Out(".pdf", sio.Memory) // force memory storage
sio.Out(".pdf", sio.File) // force file storage
sio.Out(".pdf", sio.Storage("memory")) // from string config
func (OutConfig) StorageType ΒΆ added in v0.0.7
func (o OutConfig) StorageType() *StorageType
StorageType returns the configured storage type, or nil when unset.
type Output ΒΆ
type Output struct {
// contains filtered or unexported fields
}
Output represents a file produced by a IoSession.Process call. It can be kept beyond session cleanup via Output.Keep().
func BindProcess ΒΆ added in v0.0.7
func BindProcess(ctx context.Context, out OutConfig, fn func(ctx context.Context, b *Binder, w io.Writer) error) (*Output, error)
BindProcess opens multiple StreamReaders lazily, creates an output, and executes fn. Requires a session in context.
func BindProcessResult ΒΆ added in v0.0.7
func BindProcessResult[T any](ctx context.Context, out OutConfig, fn func(ctx context.Context, b *Binder, w io.Writer) (*T, error)) (*Output, *T, error)
BindProcessResult is like BindProcess but also returns a result value.
func Process ΒΆ
func Process(ctx context.Context, source StreamReader, out OutConfig, fn ProcessFunc) (*Output, error)
Process is a convenience wrapper for IoSession.Process.
func ProcessList ΒΆ added in v0.0.3
func ProcessList(ctx context.Context, sources []StreamReader, out OutConfig, fn ProcessListFunc) (*Output, error)
ProcessList is a convenience wrapper for IoSession.ProcessList.
func ProcessListResult ΒΆ added in v0.0.3
func ProcessResult ΒΆ added in v0.0.3
func (*Output) AsReaderAt ΒΆ added in v0.0.7
func (o *Output) AsReaderAt(ctx context.Context, opts ...ToReaderAtOption) (*ReaderAtResult, error)
func (*Output) Bytes ΒΆ
Bytes loads the entire file into memory. Use carefully for very large files.
func (*Output) Data ΒΆ added in v0.0.7
Data returns the raw bytes for StorageMemory mode. Returns nil for StorageFile mode. The returned slice is owned by Output - do not modify.
func (*Output) Keep ΒΆ
Keep marks the output file as persistent. IoSession.Cleanup() will NOT delete the file.
func (*Output) OpenReader ΒΆ
func (o *Output) OpenReader() (io.ReadCloser, error)
func (*Output) OpenWriter ΒΆ
func (o *Output) OpenWriter() (io.WriteCloser, error)
func (*Output) Reader ΒΆ added in v0.0.3
func (o *Output) Reader() StreamReader
func (*Output) SaveAs ΒΆ
SaveAs copies the output file to a persistent path outside of the session dir.
func (*Output) StorageType ΒΆ added in v0.0.7
func (o *Output) StorageType() StorageType
StorageType returns the storage type of this output.
type ProcessFunc ΒΆ added in v0.0.3
type ProcessListFunc ΒΆ added in v0.0.3
type ReadListFunc ΒΆ added in v0.0.3
type ReaderAtResult ΒΆ added in v0.0.7
type ReaderAtResult struct {
// contains filtered or unexported fields
}
func ToReaderAt ΒΆ added in v0.0.7
func ToReaderAt(ctx context.Context, r io.Reader, opts ...ToReaderAtOption) (*ReaderAtResult, error)
ToReaderAt converts any io.Reader into something that supports io.ReaderAt. - If the input already supports ReaderAt β returns it directly. - If it is small enough β keeps it in memory. - If too large β spills to temporary file.
func (*ReaderAtResult) Cleanup ΒΆ added in v0.0.7
func (r *ReaderAtResult) Cleanup() error
Cleanup releases any resources created by ToReaderAt.
func (*ReaderAtResult) ReaderAt ΒΆ added in v0.0.7
func (r *ReaderAtResult) ReaderAt() io.ReaderAt
ReaderAt exposes the underlying ReaderAt.
func (*ReaderAtResult) Size ΒΆ added in v0.0.7
func (r *ReaderAtResult) Size() int64
Size returns the byte size of the underlying data when known.
func (*ReaderAtResult) Source ΒΆ added in v0.0.7
func (r *ReaderAtResult) Source() string
Source reports where the ReaderAt was sourced from.
type ReaderList ΒΆ added in v0.0.3
type ReaderList struct {
// contains filtered or unexported fields
}
ReaderList holds opened readers and provides cleanup functionality.
func OpenReaderList ΒΆ added in v0.0.3
func OpenReaderList(sources []StreamReader) (*ReaderList, error)
OpenReaderList opens all StreamReaders and returns a ReaderList.
func (*ReaderList) Close ΒΆ added in v0.0.3
func (rl *ReaderList) Close() error
Close closes all opened readers and cleans up all source StreamReaders.
func (*ReaderList) Readers ΒΆ added in v0.0.3
func (rl *ReaderList) Readers() []io.Reader
Readers returns the list of opened readers.
type StorageType ΒΆ added in v0.0.7
type StorageType int
StorageType defines how the IoManager stores data
const ( // StorageFile stores data as temporary files on disk (default) StorageFile StorageType = iota // StorageMemory stores data in memory as byte slices StorageMemory )
func Storage ΒΆ added in v0.0.7
func Storage(s string) StorageType
Storage converts string to StorageType. Supports: "file", "memory"
func (StorageType) String ΒΆ added in v0.0.7
func (s StorageType) String() string
String returns string representation of StorageType
type StreamReader ΒΆ
type StreamReader interface {
Open() (io.ReadCloser, error)
Cleanup() error
}
StreamReader represents any input source that can be opened as an io.ReadCloser, and is responsible for cleaning up its own underlying resources (temporary files, buffers, etc.).
func In ΒΆ added in v0.0.3
func In(sr StreamReader, opts ...InOption) StreamReader
In wraps a StreamReader with additional behavior, such as DeleteAfterUse.
type ToReaderAtOption ΒΆ added in v0.0.7
type ToReaderAtOption func(*ToReaderAtOptions)
func WithMaxMemoryBytes ΒΆ added in v0.0.7
func WithMaxMemoryBytes(n int64) ToReaderAtOption
func WithTempDir ΒΆ added in v0.0.7
func WithTempDir(dir string) ToReaderAtOption
func WithTempPattern ΒΆ added in v0.0.7
func WithTempPattern(p string) ToReaderAtOption
type ToReaderAtOptions ΒΆ added in v0.0.7
type ToReaderAtOptions struct {
// contains filtered or unexported fields
}
type URLReader ΒΆ
type URLReader struct {
// contains filtered or unexported fields
}
URLReader streams content directly from a URL. Each Open() call creates a new HTTP request. Cleanup() is a no-op since the response body is closed via the returned ReadCloser.
func NewURLReader ΒΆ
func NewURLReader(urlStr string, opts ...URLReaderOptions) *URLReader
type URLReaderOptions ΒΆ added in v0.0.5
type URLReaderOptions struct {
// contains filtered or unexported fields
}
func (URLReaderOptions) WithClient ΒΆ added in v0.0.7
func (o URLReaderOptions) WithClient(client *http.Client) URLReaderOptions
WithClient sets a custom HTTP client for URLReaderOptions.
func (URLReaderOptions) WithInsecureTLS ΒΆ added in v0.0.7
func (o URLReaderOptions) WithInsecureTLS(insecure bool) URLReaderOptions
WithInsecureTLS configures TLS verification for URLReaderOptions.
func (URLReaderOptions) WithTimeout ΒΆ added in v0.0.7
func (o URLReaderOptions) WithTimeout(timeout time.Duration) URLReaderOptions
WithTimeout sets the timeout for URLReaderOptions.
type UseReader ΒΆ added in v0.0.7
type UseReader struct {
// contains filtered or unexported fields
}
UseReader wraps an opened io.Reader from Binder.
