-
Notifications
You must be signed in to change notification settings - Fork 62
/
image.go
198 lines (174 loc) · 4.81 KB
/
image.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package image
import (
"fmt"
"strconv"
"github.com/trim21/go-phpserialize"
"github.com/bangumi/server/internal/dal/dao"
"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/pkg/errgo"
"github.com/bangumi/server/internal/pkg/util"
)
type Image struct {
Cat *int64 `php:"cat,omitempty"`
GroupID *string `php:"grp_id,omitempty"`
GroupName *string `php:"grp_name,omitempty"`
Name *string `php:"name,omitempty"`
Title *string `php:"title,omitempty"`
ID *int `php:"id,omitempty"`
UserID *string `php:"uid,omitempty"`
SubjectID *string `php:"subject_id,omitempty"`
Images *string `php:"images,omitempty"`
}
func (i *Image) ToModel() *model.TimeLineImage {
result := &model.TimeLineImage{}
util.CopySameNameField(result, i)
return result
}
func (i *Image) UnmarshalPHP(b []byte) error {
m := make(map[string]any)
if err := phpserialize.Unmarshal(b, &m); err != nil {
return errgo.Wrap(err, "phpserialize.Unmarshal")
}
i.Cat = extractAs[int64](m, "cat")
i.GroupID = extractAs[string](m, "grp_id")
i.GroupName = extractAs[string](m, "grp_name")
i.Name = extractAs[string](m, "name")
i.Title = extractAs[string](m, "title")
i.UserID = extractAs[string](m, "uid")
i.SubjectID = extractAs[string](m, "subject_id")
i.Images = extractAs[string](m, "images")
i.ID = extractAs[int](m, "id")
if i.ID == nil {
// for some cases, the ID is stored as string in db
// try to extractAs string if failed as int
i.ID = strPtrToIntPtr(extractAs[string](m, "id"))
}
return nil
}
func extractAs[T interface{ int64 | string | int }](m map[string]any, key string) *T {
d, ok := m[key]
if !ok {
return nil
}
d2, ok := d.(T)
if !ok {
return nil
}
return &d2
}
func strPtrToIntPtr(s *string) *int {
if s == nil {
return nil
}
result, err := strconv.Atoi(*s)
if err != nil {
return nil
}
return &result
}
func intPtrToStrPtr(i *int) *string {
if i == nil {
return nil
}
result := strconv.Itoa(*i)
return &result
}
func (i *Image) FromModel(image *model.TimeLineImage) {
util.CopySameNameField(i, image)
}
//nolint:gocyclo
func imageToMap(image *model.TimeLineImage, cat uint16) map[string]any {
var i Image
i.FromModel(image)
m := make(map[string]any)
if i.Cat != nil {
m["cat"] = i.Cat
}
if i.GroupID != nil {
m["grp_id"] = i.GroupID
}
if i.GroupName != nil {
m["grp_name"] = i.GroupName
}
if i.Name != nil {
m["name"] = i.Name
}
if i.Title != nil {
m["title"] = i.Title
}
if i.UserID != nil {
m["uid"] = i.UserID
}
if i.SubjectID != nil {
m["subject_id"] = i.SubjectID
}
if i.Images != nil {
m["images"] = i.Images
}
if i.ID != nil {
if cat == 9 {
// yes, the cat 9 is the case that image id is stored as string
m["id"] = intPtrToStrPtr(i.ID)
} else {
m["id"] = i.ID
}
}
return m
}
type Images []Image
func (is Images) ToModel() model.TimeLineImages {
if is == nil {
return nil
}
var images model.TimeLineImages
for _, i := range is {
images = append(images, *i.ToModel())
}
return images
}
func DAOToModel(tl *dao.TimeLine) (model.TimeLineImages, error) {
b := tl.Img
// the image []byte in db is in the type: Union[Image, Images]
// handle the single-Image case first, fast path
var resultFP Image // result fast path
errFP := phpserialize.Unmarshal(b, &resultFP)
if errFP == nil {
return []model.TimeLineImage{*resultFP.ToModel()}, nil
}
// unmarshal Image errors, try the Images unmarshalling path
// wraps error raised in single-Image handling path
var images Images
if err := phpserialize.Unmarshal(b, &images); err != nil {
return nil, errgo.Wrap(err,
fmt.Sprintf("phpserialize.unmarshal(with unmarshal as single-Image failed too with error: %v)", errFP),
)
}
return images.ToModel(), nil
}
func ModelToDAO(tl *model.TimeLine) ([]byte, error) {
images := tl.Image
// handle the single-Image case first, fast path
if len(images) == 1 {
result, err := phpserialize.Marshal(imageToMap(&images[0], tl.Cat))
return result, errgo.Wrap(err, "phpserialize.Marshal")
}
var is []map[string]any
for i := range images {
is = append(is, imageToMap(&images[i], tl.Cat))
}
result, err := phpserialize.Marshal(is)
return result, errgo.Wrap(err, "phpserialize.marshal")
}