gridfs

package module
v0.0.0-...-5fd4925 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2020 License: GPL-3.0 Imports: 21 Imported by: 0

README

GridFS

Advanced GridFS driver.

Documentation

Index

Constants

View Source
const (
	// DefaultBucketName is the default name of bucket.
	DefaultBucketName string = "fs"
	// DefaultChunkSize is the default size of each file chunk.
	DefaultChunkSize int32 = 255 * 1024 // 255KiB
)
View Source
const (
	// StreamCreated equal 1 if stream buffer has been modified
	StreamCreated uint8 = 1 << 0
	// StreamClosed equal 1 if stream closed
	StreamClosed uint8 = 1 << 1
	// StreamRead equal 1 if stream can be read
	StreamRead uint8 = 1 << 2
	// StreamWrite equal 1 if stream can be wrote
	StreamWrite uint8 = 1 << 3
	// StreamModified equal 1 if stream buffer has been modified
	StreamModified uint8 = 1 << 4
)

Variables

View Source
var (
	// ErrFileNotFound occurs if a user asks to download a file with a file ID that isn't found in the files collection.
	ErrFileNotFound = errors.New("file with given parameters not found")

	// ErrWrongIndex is used when the chunk retrieved from the server does not have the expected index.
	ErrWrongIndex = errors.New("chunk index does not match expected index")

	// ErrWrongSize is used when the chunk retrieved from the server does not have the expected size.
	ErrWrongSize = errors.New("chunk size does not match expected size")

	// ErrFileInvalid indicates an invalid argument.
	// Methods on File will return this error when the receiver is nil.
	ErrFileInvalid = errors.New("invalid argument")

	// ErrStreamClosed is an error returned if an operation is attempted on a closed/aborted stream.
	ErrStreamClosed = errors.New("stream is closed or aborted")

	// ErrStreamSeekUnsupport is an error returned if unsupported whence value.
	ErrStreamSeekUnsupport = errors.New("unsupported whence value")

	// ErrStreamSeekOverflow is an error returned if seek past end of file.
	ErrStreamSeekOverflow = errors.New("seek past end of file")

	// ErrStreamPermission is an error returned if an operation is attempted on a not readable/writable stream.
	ErrStreamPermission = errors.New("stream is not readable or writable")
)

Functions

This section is empty.

Types

type Bucket

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

Bucket represents a GridFS bucket.

func NewBucket

func NewBucket(db *mongo.Database, opts ...*options.BucketOptions) *Bucket

NewBucket creates a GridFS bucket.

func (*Bucket) DownloadToStream

func (b *Bucket) DownloadToStream(fileID interface{}, stream io.Writer) (int64, error)

DownloadToStream downloads the file with the specified fileID and writes it to the provided io.Writer. Returns the number of bytes written to the steam and an error, or nil if there was no error.

func (*Bucket) DownloadToStreamByName

func (b *Bucket) DownloadToStreamByName(filename string, stream io.Writer, opts ...*options.NameOptions) (int64, error)

DownloadToStreamByName downloads the file with the given name to the given io.Writer.

func (*Bucket) OpenDownloadStream

func (b *Bucket) OpenDownloadStream(fileID interface{}) (*File, error)

OpenDownloadStream creates a stream from which the contents of the file can be read and seek.

func (*Bucket) OpenDownloadStreamByName

func (b *Bucket) OpenDownloadStreamByName(filename string, opts ...*options.NameOptions) (*File, error)

OpenDownloadStreamByName opens a download stream for the file with the given filename.

func (*Bucket) OpenUploadStream

func (b *Bucket) OpenUploadStream(filename string, opts ...*options.UploadOptions) (*File, error)

OpenUploadStream creates a file ID new upload stream for a file given the filename.

func (*Bucket) OpenUploadStreamWithID

func (b *Bucket) OpenUploadStreamWithID(fileID interface{}, filename string, opts ...*options.UploadOptions) (*File, error)

OpenUploadStreamWithID creates a new upload stream for a file given the file ID and filename.

func (*Bucket) SetOpenDeadline

func (b *Bucket) SetOpenDeadline(t time.Time) error

SetOpenDeadline sets the open deadline for this bucket

func (*Bucket) SetReadDeadline

func (b *Bucket) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline for this bucket

func (*Bucket) SetWriteDeadline

func (b *Bucket) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the write deadline for this bucket.

func (*Bucket) UploadFromStream

func (b *Bucket) UploadFromStream(filename string, source io.Reader, opts ...*options.UploadOptions) (primitive.ObjectID, error)

UploadFromStream creates a fileID and uploads a file given a source stream.

func (*Bucket) UploadFromStreamWithID

func (b *Bucket) UploadFromStreamWithID(fileID interface{}, filename string, source io.Reader, opts ...*options.UploadOptions) error

UploadFromStreamWithID uploads a file given a source stream.

type File

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

File represents an open file descriptor.

func (*File) Close

func (f *File) Close() (err error)

Close closes the File, rendering it unusable for I/O. On files that support SetDeadline, any pending I/O operations will be canceled and return immediately with an error. Close will return an error if it has already been called.

func (*File) MD5Hash

func (f *File) MD5Hash() (md5Hash string, err error)

func (*File) Name

func (f *File) Name() (name string)

func (*File) Read

func (f *File) Read(b []byte) (n int, err error)

Read reads up to len(b) bytes from the File. It returns the number of bytes read and any error encountered. At end of file, Read returns 0, io.EOF.

func (*File) ReadAt

func (f *File) ReadAt(b []byte, off int64) (n int, err error)

ReadAt reads len(b) bytes from the File starting at byte offset off. It returns the number of bytes read and the error, if any. ReadAt always returns a non-nil error when n < len(b). At end of file, that error is io.EOF.

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (ret int64, err error)

Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any. The behavior of Seek on a file opened with O_APPEND is not specified.

func (*File) Write

func (f *File) Write(b []byte) (n int, err error)

Write writes len(b) bytes to the File. It returns the number of bytes written and an error, if any. Write returns a non-nil error when n != len(b).

func (*File) WriteAt

func (f *File) WriteAt(b []byte, off int64) (n int, err error)

WriteAt writes len(b) bytes to the File starting at byte offset off. It returns the number of bytes written and an error, if any. WriteAt returns a non-nil error when n != len(b).

If file was opened with the O_APPEND flag, WriteAt returns an error.

func (*File) WriteString

func (f *File) WriteString(s string) (n int, err error)

WriteString is like Write, but writes the contents of string s rather than a slice of bytes.

type FileInfo

type FileInfo struct {
	ID         interface{}    `bson:"_id"`
	ChunkSize  int32          `bson:"chunkSize"`
	Filename   string         `bson:"filename"`
	Length     int64          `bson:"length"`
	Metadata   interface{}    `bson:"metadata"`
	Reference  *FileReference `bson:"reference,omitempty"`
	UploadDate time.Time      `bson:"uploadDate"`
	// contains filtered or unexported fields
}

FileInfo represents an database file descriptor.

func (*FileInfo) Doc

func (fileInfo *FileInfo) Doc() (*bsonx.Doc, error)

func (*FileInfo) FileID

func (fileInfo *FileInfo) FileID() (bsonx.Val, error)

type FileReference

type FileReference struct {
	Hashes               Hashes    `bson:"hashes,omitempty"`
	MimeType             string    `bson:"mimeType,omitempty"`
	CreatedDateTime      time.Time `bson:"createdDateTime,omitempty"`
	LastAccessedDateTime time.Time `bson:"lastAccessedDateTime,omitempty"`
	LastModifiedDateTime time.Time `bson:"lastModifiedDateTime,omitempty"`
}

FileReference represents an database file reference.

type Hashes

type Hashes struct {
	MD5Hash      *string `bson:"md5Hash,omitempty"`      // hex
	CRC32Hash    *string `bson:"crc32Hash,omitempty"`    // hex
	SHA1Hash     *string `bson:"sha1Hash,omitempty"`     // hex
	QuickXorHash *string `bson:"quickXorHash,omitempty"` // base64
}

Hashes represents an database file reference.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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