-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.go
128 lines (105 loc) · 2.74 KB
/
media.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
package presskit
import (
"io/ioutil"
"path/filepath"
"strings"
"code.cloudfoundry.org/bytefmt"
)
const (
// directory of images
imageDir = "images"
// directory of videos
videoDir = "videos"
)
// media holds all media information of a document
type media struct {
Header string
Icon string
Logo string
Images []string // images for the images section
Videos []video
zipSizes map[string]string
}
// newMedia reads all images and videos
func newMedia(path string, h videoHolder) (*media, error) {
var m media
// images
images, _ := ioutil.ReadDir(join(path, imageDir))
for _, f := range images {
// use only base name to identify usage
switch strings.TrimSuffix(f.Name(), filepath.Ext(f.Name())) {
case "_header":
m.Header = join(imageDir, f.Name())
case "_logo":
m.Logo = join(imageDir, f.Name())
case "_icon":
m.Icon = join(imageDir, f.Name())
default:
m.Images = append(m.Images, join(imageDir, f.Name()))
}
}
// copy external videos
m.Videos = h.GetVideos()
// add local videos (err if no folder)
videos, _ := ioutil.ReadDir(join(path, videoDir))
for _, f := range videos {
ext := filepath.Ext(f.Name())
// trim extension
name := strings.TrimSuffix(f.Name(), ext)
name = strings.Replace(name, "__", " ", -1)
v := video{
Title: name,
Source: strings.TrimPrefix(ext, "."),
Id: join(videoDir, f.Name()),
}
m.Videos = append(m.Videos, v)
}
m.zipSizes = make(map[string]string)
return &m, nil
}
// generateZip writes all files in images to a zip file and returns the file data
func (m media) generateZip(outputPath string) error {
zipOutputPath := join(outputPath, "zip")
zips := map[string][]string{
"images.zip": m.Images,
"logo.zip": {m.Logo, m.Icon},
}
for name, fileList := range zips {
// generate
zipData, err := zipFiles(outputPath, fileList)
if err != nil {
return err
}
// safe
err = writeFile(join(zipOutputPath, name), zipData)
if err != nil {
return err
}
// TODO own format func
m.zipSizes[name] = bytefmt.ByteSize(uint64(len(zipData)))
}
return nil
}
// ZipSize is called from template to get the size string representation of a zip file
func (m media) ZipSize(name string) string {
return m.zipSizes[name]
}
// HasLogoOrIcon is used in template to determine if a logo or an icon is present
func (m media) HasLogoOrIcon() bool {
if m.Logo == "" && m.Icon == "" {
return false
}
return true
}
// videoHolder is used to get the videos from both game and company in newMedia
type videoHolder interface {
GetVideos() []video
}
// GetVideos returns all videos of the data file
func (c company) GetVideos() []video {
return c.Videos
}
// GetVideos returns all videos of the data file
func (g game) GetVideos() []video {
return g.Videos
}