forked from carl-mastrangelo/pixur
-
Notifications
You must be signed in to change notification settings - Fork 1
/
picfile.go
61 lines (52 loc) · 1.41 KB
/
picfile.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
package schema
import (
"path/filepath"
"pixur.org/pixur/be/status"
)
var picFileMimeExt = map[Pic_File_Mime]string{
Pic_File_JPEG: ".jpg",
Pic_File_GIF: ".gif",
Pic_File_PNG: ".png",
Pic_File_WEBM: ".webm",
Pic_File_MP4: ".mp4",
}
var picFileMimeTypes = map[string]Pic_File_Mime{
".jpg": Pic_File_JPEG,
".gif": Pic_File_GIF,
".png": Pic_File_PNG,
".webm": Pic_File_WEBM,
".mp4": Pic_File_MP4,
}
func init() {
if len(picFileMimeExt) != len(Pic_File_Mime_name)-1 {
panic("mime map wrong")
}
if len(picFileMimeTypes) != len(Pic_File_Mime_name)-1 {
panic("mime map wrong")
}
for k, _ := range Pic_File_Mime_name {
if Pic_File_Mime(k) == Pic_File_UNKNOWN {
continue
}
if _, present := picFileMimeExt[Pic_File_Mime(k)]; !present {
panic("missing value in mime map")
}
}
}
func PicFilePath(pixPath string, picId int64, mime Pic_File_Mime) (string, status.S) {
ext, present := picFileMimeExt[mime]
if !present {
return "", status.InvalidArgument(nil, "unknown mime", mime)
}
return filepath.Join(PicBaseDir(pixPath, picId), Varint(picId).Encode()+ext), nil
}
func PicFileDerivedPath(
pixPath string, picId, index int64, mime Pic_File_Mime) (string, status.S) {
ext, present := picFileMimeExt[mime]
if !present {
return "", status.InvalidArgument(nil, "unknown mime", mime)
}
return filepath.Join(
PicBaseDir(pixPath, picId),
Varint(picId).Encode()+Varint(index).Encode()+ext), nil
}