/
storage.go
153 lines (119 loc) · 2.87 KB
/
storage.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
package provider
import (
"context"
"mime"
"strconv"
"strings"
"time"
absto "github.com/ViBiOh/absto/pkg/model"
)
func IgnoreNotExistsErr[K comparable](_ context.Context, _ K, err error) error {
if absto.IsNotExist(err) {
return nil
}
return err
}
func lowerString(first, second string) bool {
return strings.Compare(strings.ToLower(first), strings.ToLower(second)) < 0
}
func greaterTime(first, second time.Time) bool {
return first.After(second)
}
type ByHybridSort []absto.Item
func (a ByHybridSort) Len() int {
return len(a)
}
func (a ByHybridSort) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a ByHybridSort) Less(i, j int) bool {
first := a[i]
second := a[j]
if first.IsDir() && second.IsDir() {
return lowerString(first.Name(), second.Name())
}
if first.IsDir() {
return true
}
if second.IsDir() {
return false
}
return greaterTime(first.Date, second.Date)
}
type ByID []absto.Item
func (a ByID) Len() int { return len(a) }
func (a ByID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByID) Less(i, j int) bool {
return a[i].ID < a[j].ID
}
type RenderItem struct {
Aggregate Aggregate
Tags []string
URL string
Path string
absto.Item
HasThumbnail bool
IsCover bool
}
func (r RenderItem) String() string {
var output strings.Builder
output.WriteString(r.URL)
output.WriteString(strconv.FormatBool(r.HasThumbnail))
output.WriteString(r.Path)
output.WriteString(strconv.FormatBool(r.IsCover))
output.WriteString(r.ID)
output.WriteString(strconv.FormatInt(r.Size(), 10))
output.WriteString(r.Date.String())
return output.String()
}
func (r RenderItem) IsZero() bool {
return r.Item.IsZero()
}
func (r RenderItem) IsImage() bool {
_, ok := ImageExtensions[r.Extension]
return ok
}
func (r RenderItem) IsVideo() bool {
_, ok := VideoExtensions[r.Extension]
return ok
}
func (r RenderItem) Mime() string {
if mimeType, ok := VideoExtensions[r.Extension]; ok {
return mimeType
}
if mimeType := mime.TypeByExtension(r.Extension); mimeType != "" {
return mimeType
}
if CodeExtensions[r.Extension] {
return "text/plain; charset=utf-8"
}
return ""
}
func StorageToRender(item absto.Item, request Request) RenderItem {
return RenderItem{
URL: request.RelativeURL(item),
Path: request.Path,
Item: item,
}
}
type StoryItem struct {
Exif Metadata
RenderItem
}
func (s StoryItem) String() string {
var output strings.Builder
output.WriteString(s.URL)
output.WriteString(strconv.FormatBool(s.HasThumbnail))
output.WriteString(s.Path)
output.WriteString(strconv.FormatBool(s.IsCover))
output.WriteString(s.ID)
output.WriteString(strconv.FormatInt(s.Size(), 10))
output.WriteString(s.Date.String())
return output.String()
}
func StorageToStory(item absto.Item, request Request, exif Metadata) StoryItem {
return StoryItem{
RenderItem: StorageToRender(item, request),
Exif: exif,
}
}