zim

package module
v0.0.0-...-c20fbd1 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2024 License: MIT Imports: 15 Imported by: 0

README

go-zim

Go Reference

A Golang library to read and serve ZIM archives.

Inspired by github.com/tim-st/go-zim.

Usage

Reading a ZIM file
package main

import (
	"flag"
	"net/http"

	"github.com/Bornholm/go-zim"
)

func main() {
	reader, err := zim.Open("my-archive.zim")
	if err != nil {
		panic(err)
	}

	defer func() {
		if err := reader.Close(); err != nil {
			panic(err)
		}
	}()

	fmt.Println("Entries count:", reader.EntryCount())

	mainPage, err := reader.MainPage()
	if err != nil {
		panic(err)
	}

	fmt.Println("Main Page Title:", mainPage.Title())
	fmt.Println("Main Page Full URL:", mainPage.FullURL())
}
Serving a ZIM file with a HTTP server
package main

import (
	"flag"
	"net/http"

	"github.com/Bornholm/go-zim"
	zimFS "github.com/Bornholm/go-zim/fs"
)

func main() {
	reader, err := zim.Open(zimPath)
	if err != nil {
		panic(err)
	}

	defer func() {
		if err := reader.Close(); err != nil {
			panic(err)
		}
	}()

	fs := zimFS.New(reader)
	fileServer := http.FileServer(http.FS(fs))

	if err := http.ListenAndServe(":8080", fileServer); err != nil {
		panic(err)
	}
}

See examples/zim-server for an runnable example.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidIndex                     = errors.New("invalid index")
	ErrNotFound                         = errors.New("not found")
	ErrInvalidRedirect                  = errors.New("invalid redirect")
	ErrCompressionAlgorithmNotSupported = errors.New("compression algorithm not supported")
)

Functions

This section is empty.

Types

type BaseEntry

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

func (*BaseEntry) FullURL

func (e *BaseEntry) FullURL() string

func (*BaseEntry) Namespace

func (e *BaseEntry) Namespace() Namespace

func (*BaseEntry) Title

func (e *BaseEntry) Title() string

func (*BaseEntry) URL

func (e *BaseEntry) URL() string

type BlobDecoderFactory

type BlobDecoderFactory func(io.Reader) (io.ReadSeekCloser, error)

type BlobReader

type BlobReader interface {
	io.ReadSeekCloser
	Size() (int64, error)
}

type CompressedBlobReader

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

func NewCompressedBlobReader

func NewCompressedBlobReader(reader *Reader, decoderFactory BlobDecoderFactory, clusterStartOffset, clusterEndOffset uint64, blobIndex uint32, blobSize int) *CompressedBlobReader

func NewXZBlobReader

func NewXZBlobReader(reader *Reader, clusterStartOffset, clusterEndOffset uint64, blobIndex uint32, blobSize int) *CompressedBlobReader

func NewZStdBlobReader

func NewZStdBlobReader(reader *Reader, clusterStartOffset, clusterEndOffset uint64, blobIndex uint32, blobSize int) *CompressedBlobReader

func (*CompressedBlobReader) Close

func (r *CompressedBlobReader) Close() error

Close implements io.ReadCloser.

func (*CompressedBlobReader) Read

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

Read implements io.ReadCloser.

func (*CompressedBlobReader) Seek

func (r *CompressedBlobReader) Seek(offset int64, whence int) (int64, error)

Seek implements BlobReader.

func (*CompressedBlobReader) Size

func (r *CompressedBlobReader) Size() (int64, error)

Size implements BlobReader.

type ContentEntry

type ContentEntry struct {
	*BaseEntry
	// contains filtered or unexported fields
}

func (*ContentEntry) Compression

func (e *ContentEntry) Compression() (int, error)

func (*ContentEntry) MimeType

func (e *ContentEntry) MimeType() string

func (*ContentEntry) Reader

func (e *ContentEntry) Reader() (BlobReader, error)

func (*ContentEntry) Redirect

func (e *ContentEntry) Redirect() (*ContentEntry, error)

type Entry

type Entry interface {
	Redirect() (*ContentEntry, error)
	Namespace() Namespace
	URL() string
	FullURL() string
	Title() string
}

type EntryIterator

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

func (*EntryIterator) Entry

func (it *EntryIterator) Entry() Entry

func (*EntryIterator) Err

func (it *EntryIterator) Err() error

func (*EntryIterator) Index

func (it *EntryIterator) Index() int

func (*EntryIterator) Next

func (it *EntryIterator) Next() bool

type MetadataKey

type MetadataKey string
const (
	MetadataName                 MetadataKey = "Name"
	MetadataTitle                MetadataKey = "Title"
	MetadataDescription          MetadataKey = "Description"
	MetadataLongDescription      MetadataKey = "LongDescription"
	MetadataCreator              MetadataKey = "Creator"
	MetadataTags                 MetadataKey = "Tags"
	MetadataDate                 MetadataKey = "Date"
	MetadataPublisher            MetadataKey = "Publisher"
	MetadataFlavour              MetadataKey = "Flavour"
	MetadataSource               MetadataKey = "Source"
	MetadataLanguage             MetadataKey = "Language"
	MetadataIllustration48x48at1 MetadataKey = "Illustration_48x48@1"
	MetadataIllustration96x96at2 MetadataKey = "Illustration_96x96@2"
)

See https://wiki.openzim.org/wiki/Metadata

type Namespace

type Namespace string
const (
	V6NamespaceContent   Namespace = "C"
	V6NamespaceMetadata  Namespace = "M"
	V6NamespaceWellKnown Namespace = "W"
	V6NamespaceSearch    Namespace = "X"
)
const (
	V5NamespaceLayout              Namespace = "-"
	V5NamespaceArticle             Namespace = "A"
	V5NamespaceArticleMetadata     Namespace = "B"
	V5NamespaceImageFile           Namespace = "I"
	V5NamespaceImageText           Namespace = "J"
	V5NamespaceMetadata            Namespace = "M"
	V5NamespaceCategoryText        Namespace = "U"
	V5NamespaceCategoryArticleList Namespace = "V"
	V5NamespaceCategoryPerArticle  Namespace = "W"
	V5NamespaceSearch              Namespace = "X"
)

type NoopReadSeekCloser

type NoopReadSeekCloser struct {
	io.ReadSeeker
}

func (*NoopReadSeekCloser) Close

func (*NoopReadSeekCloser) Close() error

Close implements io.Closer.

type OptionFunc

type OptionFunc func(opts *Options)

func WithCacheSize

func WithCacheSize(size int) OptionFunc

type Options

type Options struct {
	URLCacheSize int
	URLCacheTTL  time.Duration
	CacheSize    int
}

func NewOptions

func NewOptions(funcs ...OptionFunc) *Options

type ReadAtCloser

type ReadAtCloser interface {
	io.Closer
	ReadAt(data []byte, offset int64) (n int, err error)
}

type Reader

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

func NewReader

func NewReader(r ReadAtCloser, funcs ...OptionFunc) (*Reader, error)

func Open

func Open(path string, funcs ...OptionFunc) (*Reader, error)

func (*Reader) Close

func (r *Reader) Close() error

func (*Reader) ClusterCount

func (r *Reader) ClusterCount() uint32

func (*Reader) Entries

func (r *Reader) Entries() *EntryIterator

func (*Reader) EntryAt

func (r *Reader) EntryAt(idx int) (Entry, error)

func (*Reader) EntryCount

func (r *Reader) EntryCount() uint32

func (*Reader) EntryWithFullURL

func (r *Reader) EntryWithFullURL(url string) (Entry, error)

func (*Reader) EntryWithTitle

func (r *Reader) EntryWithTitle(ns Namespace, title string) (Entry, error)

func (*Reader) EntryWithURL

func (r *Reader) EntryWithURL(ns Namespace, url string) (Entry, error)

func (*Reader) Favicon

func (r *Reader) Favicon() (*ContentEntry, error)

func (*Reader) MainPage

func (r *Reader) MainPage() (Entry, error)

func (*Reader) Metadata

func (r *Reader) Metadata(keys ...MetadataKey) (map[MetadataKey]string, error)

Metadata returns a copy of the internal metadata map of the ZIM file.

func (*Reader) UUID

func (r *Reader) UUID() string

func (*Reader) Version

func (r *Reader) Version() (majorVersion, minorVersion uint16)

type RedirectEntry

type RedirectEntry struct {
	*BaseEntry
	// contains filtered or unexported fields
}

func (*RedirectEntry) Redirect

func (e *RedirectEntry) Redirect() (*ContentEntry, error)

type UncompressedBlobReader

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

func NewUncompressedBlobReader

func NewUncompressedBlobReader(reader *Reader, blobStartOffset, blobEndOffset uint64, blobSize int) *UncompressedBlobReader

func (*UncompressedBlobReader) Close

func (r *UncompressedBlobReader) Close() error

Close implements io.ReadCloser.

func (*UncompressedBlobReader) Read

func (r *UncompressedBlobReader) Read(p []byte) (n int, err error)

Read implements io.ReadCloser.

func (*UncompressedBlobReader) Seek

func (r *UncompressedBlobReader) Seek(offset int64, whence int) (int64, error)

Seek implements BlobReader.

func (*UncompressedBlobReader) Size

func (r *UncompressedBlobReader) Size() (int64, error)

Size implements BlobReader.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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