ddrv

package
v0.0.0-...-04b4601 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2024 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyClosed = errors.New("already closed")

ErrAlreadyClosed is returned when the reader/writer is already closed

View Source
var ErrClosed = errors.New("is closed")

ErrClosed is returned when a writer or reader is closed and caller is trying to read or write

View Source
var ErrInvalidWebhookURL = errors.New("invalid webhook URL")

ErrInvalidWebhookURL is the error returned for an invalid webhook URL.

View Source
var WebhookURLRegex = regexp.MustCompile(`^https://(?:[a-zA-Z]+\.)?discord\.com/api/webhooks/\d+/[a-zA-Z0-9_-]+$`)

WebhookURLRegex is the regular expression pattern to validate a webhook URL.

Functions

func NewNWriter

func NewNWriter(onChunk func(chunk *Attachment), chunkSize int, mgr *Manager) io.WriteCloser

func NewReader

func NewReader(chunks []Attachment, pos int64, arc *Manager) (io.ReadCloser, error)

NewReader creates new Reader instance which implements io.ReadCloser.

func NewWriter

func NewWriter(onChunk func(chunk *Attachment), chunkSize int, mgr *Manager) io.WriteCloser

NewWriter creates a new Writer with the given chunk Size and manager.

Types

type Attachment

type Attachment struct {
	URL   string `json:"url"`  // URL where the data is stored
	Size  int    `json:"size"` // Size of the data
	Start int64  // Start position of the data in the overall data sequence
	End   int64  // End position of the data in the overall data sequence
}

Attachment represents a Discord attachment URL and Size

type Manager

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

Manager provides an interface to read and write large files to Discord by splitting the files into smaller chunks, uploading or downloading these chunks through Discord webhooks, and reassembling them into the original files.

func NewManager

func NewManager(chunkSize int, webhooks []string) (*Manager, error)

NewManager returns a new instance of Manager with specified chunk size and webhook URLs. It initializes a list of webhook rest clients for each webhook URL.

func (*Manager) NewNWriter

func (mgr *Manager) NewNWriter(onChunk func(chunk *Attachment)) io.WriteCloser

NewNWriter creates a new ddrv.NWriter instance that implements an io.WriterCloser. This allows for writing large files to Discord as small, manageable chunks. NWriter buffers bytes into memory and writes data to discord in parallel

func (*Manager) NewReader

func (mgr *Manager) NewReader(chunks []Attachment, pos int64) (io.ReadCloser, error)

NewReader creates a new Reader instance that implements an io.ReaderCloser. This allows for reading large files from Discord that were split into small chunks.

func (*Manager) NewWriter

func (mgr *Manager) NewWriter(onChunk func(chunk *Attachment)) io.WriteCloser

NewWriter creates a new ddrv.Writer instance that implements an io.WriterCloser. This allows for writing large files to Discord as small, manageable chunks.

type Message

type Message struct {
	Attachments []Attachment `json:"attachments"`
}

Message represents a Discord message and contains attachments (files uploaded within the message).

type NWriter

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

NWriter implements io.WriteCloser. It streams data in chunks to Discord server channels using webhook NWriter buffers bytes into memory and writes data to discord in parallel at the cost of high-memory usage. expected memory usage - (chunkSize * number of webhooks) + 20% bytes

func (*NWriter) Close

func (w *NWriter) Close() error

func (*NWriter) Write

func (w *NWriter) Write(p []byte) (int, error)

type Reader

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

Reader is a structure that manages the reading of a sequence of Chunks. It reads chunks in order, closing each one after it's Read and moving on to the next.

func (*Reader) Close

func (r *Reader) Close() error

Close implements the Close method of io.Closer. It closes the Reader. If the Reader is already closed, Close returns ErrAlreadyClosed.

func (*Reader) Read

func (r *Reader) Read(p []byte) (int, error)

Read reads data from the current chunk into p. If it reaches the end of a chunk, it moves to the next one. It reads until p is full or there are no more chunks to Read from.

type Rest

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

Rest represents the Discord webhook REST client.

func NewRest

func NewRest(webhookURL string) (*Rest, error)

NewRest creates a new Rest instance with the provided webhook URL.

func (*Rest) CreateAttachment

func (r *Rest) CreateAttachment(reader io.Reader) (*Attachment, error)

CreateAttachment uploads a file to the Discord channel using the webhook.

type Writer

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

Writer implements io.WriteCloser. It streams data in chunks to Discord server channels using webhook

func (*Writer) Close

func (w *Writer) Close() error

Close implements the Close method of io.Closer. It closes the Writer. If the Writer is already closed, Close returns ErrAlreadyClosed.

func (*Writer) Write

func (w *Writer) Write(p []byte) (int, error)

Write implements the Write method of io.Writer. It writes p to the Writer. If p is larger than the remaining space in the current chunk, Write splits p across multiple chunks as needed. Returns the total number of bytes from p that were written.

Jump to

Keyboard shortcuts

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