This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
142 lines (123 loc) · 3.11 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Package thumbnailer provides a more efficient media thumbnailer than \
// available with native Go processing libraries through ffmpeg bindings.
package thumbnailer
import (
"image"
"io"
"time"
)
// Source stores 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
// Source dimensions, if file is image or video
Dims
// Mime type of the source file
Mime string
// Canonical file extension
Extension string
// Codec of the source file when applicable
Codec string
// Optional metadata
Meta
}
// File metadata
type Meta struct {
Title, Artist string
}
// Dims store the dimensions of an image
type Dims struct {
Width, Height uint
}
// Options suplied to the Thumbnail function
type Options struct {
// Maximum source image dimensions. Any image exceeding either will be
// rejected and return with ErrTooTall or ErrTooWide.
// If not set, all image processing will not be restricted by that
// dimension.
MaxSourceDims Dims
// Target Maximum dimensions for the thumbnail.
//
// This defines the bounding box of the thumbnail. The thumbnail will be
// scaled down until both dimensions fit the bounding box.
// To scale only by one dimension, specify the other as math.MaxUint32.
//
// Defaults to 150x150, if unset.
ThumbDims Dims
// MIME types to accept for thumbnailing.
// If nil, all MIME types will be processed.
//
// To process MIME types that are a subset of archive files, like
// "application/x-cbz", "application/x-cbr", "application/x-cb7" and
// "application/x-cbt", you must accept the corresponding archive type
// such as "application/zip" or leave this nil.
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 image.Image, err error,
) {
if opts.ThumbDims.Width == 0 {
opts.ThumbDims.Width = 150
}
if opts.ThumbDims.Height == 0 {
opts.ThumbDims.Height = 150
}
src.Mime, src.Extension, err = DetectMIME(rs, opts.AcceptedMimeTypes)
if err != nil {
return
}
_, err = rs.Seek(0, 0)
if err != nil {
return
}
// TODO: PDF Processing
// TODO: SVG processing
var fn Processor
override := overrideProcessors[src.Mime]
if override != nil {
fn = override
} else {
switch src.Mime {
case
"image/jpeg",
"image/png",
"image/gif",
"image/webp",
"application/ogg",
"video/webm",
"video/x-matroska",
"video/mp4",
"video/avi",
"video/quicktime",
"video/x-ms-wmv",
"video/x-flv",
"audio/mpeg",
"audio/aac",
"audio/wave",
"audio/x-flac",
"audio/midi":
fn = processMedia
case mimeZip:
fn = processZip
case mimeRar:
fn = processRar
default:
err = ErrUnsupportedMIME(src.Mime)
return
}
}
thumb, err = fn(rs, &src, opts)
switch src.Mime {
case "image/jpeg",
"image/png",
"image/gif",
"image/webp":
// FFmpeg considers images to be video for processing reasons
src.HasVideo = false
}
return
}