/
artwork.go
137 lines (115 loc) · 2.85 KB
/
artwork.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
// Copyright 2015, David Howden
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package store
import (
"bytes"
"fmt"
"image"
"image/jpeg"
"image/png"
"net/http"
"path/filepath"
"strings"
"golang.org/x/net/context"
"github.com/dhowden/tag"
ico "github.com/Kodeworks/golang-image-ico"
"github.com/nfnt/resize"
)
// ArtworkFileSystem wraps a FileSystem, reworking file system operations
// to refer to artwork from the underlying file.
func ArtworkFileSystem(fs FileSystem) FileSystem {
return artworkFileSystem{
FileSystem: fs,
}
}
type artworkFileSystem struct {
FileSystem
}
// Open the given file and return an http.File which contains the artwork, and hence
// the Name() of the returned file will have an extention for the artwork, not the
// media file.
func (afs artworkFileSystem) Open(ctx context.Context, path string) (http.File, error) {
f, err := afs.FileSystem.Open(ctx, path)
if err != nil {
return nil, err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return nil, err
}
var m tag.Metadata
m, err = tag.ReadFrom(f)
if err != nil {
return nil, fmt.Errorf("error extracting picture from '%v': %v", path, err)
}
p := m.Picture()
if p == nil {
return nil, fmt.Errorf("no picture attached to '%v'", path)
}
name := stat.Name()
if p.Ext != "" {
name += "." + p.Ext
}
return &file{
ReadSeeker: bytes.NewReader(p.Data),
stat: &fileInfo{
name: name,
size: int64(len(p.Data)),
modTime: stat.ModTime(),
},
}, nil
}
// FaviconFileSystem wraps another FileSystem assumed to contain only images, which are then
// resized to 48px x 48px and returned in .ico format.
func FaviconFileSystem(fs FileSystem) FileSystem {
return faviconFileSystem{
FileSystem: fs,
}
}
type faviconFileSystem struct {
FileSystem
}
// Open the given path (assumed to contain an image) and then resize to 48px x 48px and
// return in .ico format.
func (ffs faviconFileSystem) Open(ctx context.Context, path string) (http.File, error) {
f, err := ffs.FileSystem.Open(ctx, path)
if err != nil {
return nil, err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return nil, err
}
filename := stat.Name()
ext := filepath.Ext(filename)
var img image.Image
switch ext {
case ".jpeg", ".jpg":
img, err = jpeg.Decode(f)
case ".png":
img, err = png.Decode(f)
default:
err = fmt.Errorf("unsupported favicon image source: %v", stat.Name())
}
if err != nil {
return nil, err
}
img = resize.Thumbnail(48, 48, img, resize.NearestNeighbor)
buf := &bytes.Buffer{}
err = ico.Encode(buf, img)
if err != nil {
return nil, err
}
icoFilename := strings.TrimSuffix(filename, ext) + ".ico"
return &file{
ReadSeeker: bytes.NewReader(buf.Bytes()),
stat: &fileInfo{
name: icoFilename,
size: int64(buf.Len()),
modTime: stat.ModTime(),
},
}, nil
}