-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallpapers.go
161 lines (134 loc) · 3.77 KB
/
wallpapers.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package wallpapers
import (
"cmp"
"context"
"fmt"
"hash/crc32"
"path/filepath"
"regexp"
"slices"
"strings"
"time"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
)
const (
Bucket = "iccowalls"
)
var (
NameRegex = regexp.MustCompile("[^a-z0-9]")
)
// FormatName formats a filename to match our requirements.
func FormatName(in string) string {
ext := strings.ToLower(filepath.Ext(in))
if ext == ".jpeg" {
ext = ".jpg"
}
name, _ := strings.CutSuffix(in, filepath.Ext(in))
name = strings.ToLower(filepath.Base(name))
name = NameRegex.ReplaceAllString(name, "")
return name + ext
}
func GetGoogleCRC(ctx context.Context, filename string) (uint32, error) {
client, err := storage.NewClient(ctx)
if err != nil {
return 0, err
}
attr, err := client.Bucket(Bucket).Object(filename).Attrs(ctx)
if err != nil && err != storage.ErrObjectNotExist {
return 0, fmt.Errorf("could not get attrs: %w", err)
}
if err == storage.ErrObjectNotExist {
return 0, nil
}
return attr.CRC32C, nil
}
func GetFileCRC(content []byte) uint32 {
return crc32.Checksum(content, crc32.MakeTable(crc32.Castagnoli))
}
func DeleteFile(ctx context.Context, filename string) error {
client, err := storage.NewClient(ctx)
if err != nil {
return err
}
return client.Bucket(Bucket).Object(filename).Delete(ctx)
}
// UploadFile takes a file name and content and uploads it to GoogleCloud.
func UploadFile(ctx context.Context, filename string, content []byte) error {
client, err := storage.NewClient(ctx)
if err != nil {
return err
}
wc := client.Bucket(Bucket).Object(filename).NewWriter(ctx)
wc.CRC32C = GetFileCRC(content)
wc.SendCRC32C = true
wc.ACL = []storage.ACLRule{{Entity: storage.AllUsers, Role: storage.RoleReader}}
if _, err := wc.Write(content); err != nil {
return fmt.Errorf("failed write: %w", err)
}
if err := wc.Close(); err != nil {
return fmt.Errorf("failed close: %w", err)
}
return nil
}
// FullRezURL returns the URL a cropped version hosted by imgix.
func FullRezURL(key string) string {
w := 3840
h := 2160
return fmt.Sprintf("https://icco-walls.imgix.net/%s?auto=compress&w=%d&h=%d&crop=entropy&fm=png", key, w, h)
}
// ThumbUrl returns the URL a small cropped version hosted by imgix.
func ThumbURL(key string) string {
w := 800
h := 450
return fmt.Sprintf("https://icco-walls.imgix.net/%s?w=%d&h=%d&fit=crop&auto=compress&fm=png", key, w, h)
}
// File is a subset of storage.ObjectAttrs that we need.
type File struct {
CRC32C uint32 `json:"-"`
Etag string `json:"etag"`
FileURL string `json:"-"`
FullRezURL string `json:"cdn"`
Name string `json:"key"`
Size int64 `json:"-"`
ThumbnailURL string `json:"thumbnail"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
}
// GetAll returns all of the attributes for files in GCS.
func GetAll(ctx context.Context) ([]*File, error) {
client, err := storage.NewClient(ctx)
if err != nil {
return nil, err
}
var ret []*File
query := &storage.Query{
Projection: storage.ProjectionNoACL,
}
it := client.Bucket(Bucket).Objects(ctx, query)
for {
objAttrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, fmt.Errorf("error on iterating: %w", err)
}
ret = append(ret, &File{
CRC32C: objAttrs.CRC32C,
Etag: objAttrs.Etag,
Name: objAttrs.Name,
Size: objAttrs.Size,
Created: objAttrs.Created,
Updated: objAttrs.Updated,
ThumbnailURL: ThumbURL(objAttrs.Name),
FileURL: objAttrs.MediaLink,
FullRezURL: FullRezURL(objAttrs.Name),
})
}
// Sort by created date
slices.SortStableFunc(ret, func(b, a *File) int {
return cmp.Compare(a.Created.String(), b.Created.String())
})
return ret, nil
}