forked from bakape/thumbnailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (83 loc) · 2.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Package thumbnailer provides a more efficient image/video/audio/PDF
// thumbnailer than available with native Go processing libraries through
// GraphicsMagic and ffmpeg bindings.
package thumbnailer
import (
"bytes"
"io"
"time"
)
// Thumbnail stores a processed thumbnail.
// Take note, that in case an audio file with no cover art is passed, this
// struct will be unassigned.
type Thumbnail struct {
// Thumbnails can be either JPEG or PNG. Only images with transparency will
// be PNG.
IsPNG bool
Image
}
// Source stores the source image, including information about the source file
type Source struct {
// Some containers may or may not have either
HasAudio, HasVideo bool
// Length of the stream. Applies to audio and video files.
Length time.Duration
// Mime type of the source file
Mime string
// optional metadata
Title, Artist string
// Canonical file extension
Extension string
Image
}
// Dims store the dimensions of an image
type Dims struct {
Width, Height uint
}
// Options suplied to the Thumbnail function
type Options struct {
// JPEG thumbnail quality to use. [0,100]
JPEGQuality uint8
// Maximum source image dimensions. Any image exceeding either will be
// rejected and return with ErrTooTall or ErrTooWide. If not set, all images
// are processed.
MaxSourceDims Dims
// Target Maximum dimensions for the thumbnail
ThumbDims Dims
// MIME types to accept for thumbnailing. If nil, all MIME types will be
// processed.
AcceptedMimeTypes map[string]bool
}
// Process generates a thumbnail from a file of unknown type and performs some
// basic meta information extraction
func Process(rs io.ReadSeeker, opts Options) (
src Source, thumb Thumbnail, err error,
) {
src.Mime, src.Extension, err = DetectMIME(rs, opts.AcceptedMimeTypes)
if err != nil {
return
}
_, err = rs.Seek(0, 0)
if err != nil {
return
}
buf := bytes.NewBuffer(GetBuffer())
_, err = buf.ReadFrom(rs)
if err != nil {
return
}
src.Data = buf.Bytes()
return processFile(src, opts)
}
// ProcessBuffer is like Process, but takes []byte as input. More efficient,
// if you already have the file buffered into memory.
func ProcessBuffer(buf []byte, opts Options) (
src Source, thumb Thumbnail, err error,
) {
src.Mime, src.Extension, err = DetectMIMEBuffer(buf, opts.AcceptedMimeTypes)
if err != nil {
return
}
src.Data = buf
return processFile(src, opts)
}