manikyr

package module
v0.0.0-...-53b083f Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2015 License: MIT Imports: 11 Imported by: 0

README

manikyr: A thumb specialist

manikyr provides a sensible and filesystem structure agnostic image thumbnailing automation.

Installation

go get github.com/ComSecNinja/manikyr

Documentation

godoc.org/github.com/ComSecNinja/manikyr

Usage

The following example watches /home/timo/picshur-test/ and any direct child directory (/home/timo/picshur-test/*/) for changes.
If a new image file is created (by e.g. copying) in one of the direct child directories, manikyr automatically creates a thumbnail to /home/timo/picshur-test/*/thumbs with the same name as the original file.
If a file is deleted in a direct child directory and a file with the same name is present in the designated thumbnail directory, it gets deleted.
Deleting direct child directories automatically unwatches them so you do not need to worry about that.

package main

import (
	"path"

	"github.com/ComSecNinja/manikyr"
)

const myRoot = "/home/timo/picshur-test"

func main() {
	var err error
	mk := manikyr.New()

	// Thumbnail directory is $myRoot/../{parentDir}/thumbs
	mk.ThumbDirGetter = func(p string) string {
		return path.Join(path.Dir(p), "thumbs")
	}

	// When we bump into a subdir, should we watch it?
	mk.ShouldWatchSubdir = func(root, subdir string) bool {
		ok, _ := manikyr.NthSubdir(root, subdir, 0)
		if ok && subdir[0] != '.' && subdir != mk.ThumbDirGetter(subdir) {
			return true
		}
		return false
	}

	// When we come across of a file, should we try to thumbnail it?
	mk.ShouldCreateThumb = func(root, file string) bool {
		ok, _ := manikyr.NthSubdir(root, file, 1)
		return ok
	}

	// Create chan to receive and print errors
	evtChan := make(chan manikyr.Event)
	go func(c chan manikyr.Event){
		for {
			evt := <-c
			println(evt.String())
		}
	}(evtChan)

	// Add our root directory which holds the gallery directories
	err = mk.AddRoot(myRoot, evtChan)
	if err != nil {
		panic(err)
	}

	// Watch and thumbnail existing files like they were 
	// added after the root got watched
	err = mk.Init(myRoot)
	if err != nil {
		panic(err)
	}

	// Block forever
	done := make(chan struct{})
	<-done
}

Documentation

Overview

Package manikyr provides a sensible and filesystem structure agnostic image thumbnailing automation.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRootNotWatched = errors.New("root is not watched")
	ErrRootWatched    = errors.New("root is already watched")

	NearestNeighbor   = imaging.NearestNeighbor
	Box               = imaging.Box
	Linear            = imaging.Linear
	Hermite           = imaging.Hermite
	MitchellNetravali = imaging.MitchellNetravali
	CatmullRom        = imaging.CatmullRom
	BSpline           = imaging.BSpline
	Gaussian          = imaging.Gaussian
	Bartlett          = imaging.Bartlett
	Lanczos           = imaging.Lanczos
	Hann              = imaging.Hann
	Hamming           = imaging.Hamming
	Blackman          = imaging.Blackman
	Welch             = imaging.Welch
	Cosine            = imaging.Cosine
)

Functions

func NthSubdir

func NthSubdir(root, dir string, n int) (bool, error)

func Subdirectories

func Subdirectories(root string) ([]string, error)

Types

type Event

type Event struct {
	Root  string
	Path  string
	Type  EventType
	Error error
}

Event represents a single event considering watching and thumbnailing files

func (Event) String

func (e Event) String() string

String returns a string representation of the event

type EventType

type EventType uint32
const (
	Error EventType = 1 << iota
	ThumbCreate
	ThumbRemove
	Watch
)

func (EventType) String

func (t EventType) String() string

type Manikyr

type Manikyr struct {
	ThumbDirGetter    func(string) string
	ThumbNameGetter   func(string) string
	ShouldCreateThumb func(string, string) bool
	ShouldWatchSubdir func(string, string) bool
	// contains filtered or unexported fields
}

Manikyr watches specified directory roots for changes. If a new file matching the rules the user has set is created, it will get watched (directory), or thumbnailed (regular file) to a dynamic location with the chosen dimensions and algorithm. Subdirectory unwatching on deletion is automatic.

func New

func New() *Manikyr

New creates a new Manikyr instance which holds a set of preferences and directory roots to apply those rules to. Default values should be kept safe, as in not doing any damage to the filesystem integrity if the instance is initialized as is.

func (*Manikyr) AddRoot

func (m *Manikyr) AddRoot(root string, evtChan chan Event) error

AddRoot adds and watches specified path as a new root, piping future errors to given channel. The error returned considers the watcher creation, not function.

func (*Manikyr) AddSubdir

func (m *Manikyr) AddSubdir(root, subdir string)

AddSubdir adds a subdirectory to a root watcher. Both paths should be absolute.

func (*Manikyr) EmitEvent

func (m *Manikyr) EmitEvent(root string, t EventType, path string, err error)

func (*Manikyr) HasRoot

func (m *Manikyr) HasRoot(root string) bool

func (*Manikyr) Init

func (m *Manikyr) Init(root string) error

Init watches and thumbnail existing files as if they were added after the root directory got watched. Regular files are checked for corresponding thumbnails before creating a new one.

func (*Manikyr) RemoveRoot

func (m *Manikyr) RemoveRoot(root string) error

RemoveRoot removes the named root directory path and unwatches it. A root should always be unwatched this way prior to actual path deletion in the filesystem. If the named path was not previously specified to be a root, a non-nil error is returned.

func (*Manikyr) RemoveSubdir

func (m *Manikyr) RemoveSubdir(root, subdir string) error

RemoveSubdir removes a subdirectory from a root watcher. Both paths should be absolute.

func (*Manikyr) Roots

func (m *Manikyr) Roots() []string

Root returns a list of currently watched root directory paths.

func (*Manikyr) SetThumbAlgorithm

func (m *Manikyr) SetThumbAlgorithm(filter imaging.ResampleFilter)

SetThumbAlgorithm sets the used algorithm for thumbnail creation. See http://godoc.org/github.com/disintegration/imaging#ResampleFilter for more info.

func (*Manikyr) SetThumbDirFileMode

func (m *Manikyr) SetThumbDirFileMode(fm uint32)

SetThumbDirFileMode sets the filemode for thumbnail directories.

func (*Manikyr) SetThumbSize

func (m *Manikyr) SetThumbSize(w, h int)

Set thumbnail dimensions. Dimensions should be positive.

func (*Manikyr) ThumbAlgorithm

func (m *Manikyr) ThumbAlgorithm() imaging.ResampleFilter

ThumbAlgorithm gets the currently used algorithm for thumbnail creation.

func (*Manikyr) ThumbDirFileMode

func (m *Manikyr) ThumbDirFileMode() os.FileMode

ThumbDirFileMode gets the currently set filemode for thumbnail directories.

func (*Manikyr) ThumbSize

func (m *Manikyr) ThumbSize() (int, int)

Get the currently set thumbnail dimensions

Jump to

Keyboard shortcuts

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