This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
asset.go
248 lines (197 loc) · 5.78 KB
/
asset.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package contentful
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
)
// AssetsService service
type AssetsService service
// File model
type File struct {
Name string `json:"fileName,omitempty"`
ContentType string `json:"contentType,omitempty"`
URL string `json:"url,omitempty"`
UploadURL string `json:"upload,omitempty"`
Detail *FileDetail `json:"details,omitempty"`
}
// FileDetail model
type FileDetail struct {
Size int `json:"size,omitempty"`
Image *FileImage `json:"image,omitempty"`
}
// FileImage model
type FileImage struct {
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
}
// FileFields model
type FileFields struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
File *File `json:"file,omitempty"`
}
// Asset model
type Asset struct {
locale string
Sys *Sys `json:"sys"`
Fields *FileFields `json:"fields"`
}
// MarshalJSON for custom json marshaling
func (asset *Asset) MarshalJSON() ([]byte, error) {
payload := map[string]interface{}{
"sys": "",
"fields": map[string]interface{}{
"title": map[string]string{},
"description": map[string]string{},
"file": map[string]interface{}{},
},
}
payload["sys"] = asset.Sys
fields := payload["fields"].(map[string]interface{})
// title
title := fields["title"].(map[string]string)
title[asset.locale] = asset.Fields.Title
// description
description := fields["description"].(map[string]string)
description[asset.locale] = asset.Fields.Description
// file
file := fields["file"].(map[string]interface{})
file[asset.locale] = asset.Fields.File
return json.Marshal(payload)
}
// UnmarshalJSON for custom json unmarshaling
func (asset *Asset) UnmarshalJSON(data []byte) error {
type Alias *Asset
var payload map[string]interface{}
if err := json.Unmarshal(data, &payload); err != nil {
return err
}
fileName := payload["fields"].(map[string]interface{})["file"].(map[string]interface{})["fileName"]
localized := true
if fileName == nil {
localized = false
}
if localized == false {
asset.Sys = &Sys{}
b, _ := json.Marshal(payload["sys"])
if err := json.Unmarshal(b, asset.Sys); err != nil {
return err
}
title := payload["fields"].(map[string]interface{})["title"]
if title != nil {
title = title.(map[string]interface{})[asset.locale]
}
description := payload["fields"].(map[string]interface{})["description"]
if description != nil {
description = description.(map[string]interface{})[asset.locale]
}
asset.Fields = &FileFields{
Title: title.(string),
Description: description.(string),
File: &File{},
}
file := payload["fields"].(map[string]interface{})["file"].(map[string]interface{})[asset.locale]
if err := json.Unmarshal([]byte(file.(string)), asset.Fields.File); err != nil {
return err
}
} else {
if err := json.Unmarshal(data, Alias(asset)); err != nil {
return err
}
}
return nil
}
// GetVersion returns entity version
func (asset *Asset) GetVersion() int {
version := 1
if asset.Sys != nil {
version = asset.Sys.Version
}
return version
}
// List returns asset collection
func (service *AssetsService) List(spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s/assets", spaceID)
method := "GET"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return &Collection{}
}
col := NewCollection(&CollectionOptions{})
col.c = service.c
col.req = req
return col
}
// Get returns a single asset entity
func (service *AssetsService) Get(spaceID, assetID string) (*Asset, error) {
path := fmt.Sprintf("/spaces/%s/assets/%s", spaceID, assetID)
method := "GET"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return nil, err
}
var asset Asset
if err := service.c.do(req, &asset); err != nil {
return nil, err
}
return &asset, nil
}
// Upsert updates or creates a new asset entity
func (service *AssetsService) Upsert(spaceID string, asset *Asset) error {
bytesArray, err := json.Marshal(asset)
if err != nil {
return err
}
var path string
var method string
if asset.Sys.CreatedAt != "" {
path = fmt.Sprintf("/spaces/%s/assets/%s", spaceID, asset.Sys.ID)
method = "PUT"
} else {
path = fmt.Sprintf("/spaces/%s/assets", spaceID)
method = "POST"
}
req, err := service.c.newRequest(method, path, nil, bytes.NewReader(bytesArray))
if err != nil {
return err
}
req.Header.Set("X-Contentful-Version", strconv.Itoa(asset.GetVersion()))
return service.c.do(req, asset)
}
// Delete sends delete request
func (service *AssetsService) Delete(spaceID string, asset *Asset) error {
path := fmt.Sprintf("/spaces/%s/assets/%s", spaceID, asset.Sys.ID)
method := "DELETE"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return err
}
version := strconv.Itoa(asset.Sys.Version)
req.Header.Set("X-Contentful-Version", version)
return service.c.do(req, nil)
}
// Process the asset
func (service *AssetsService) Process(spaceID string, asset *Asset) error {
path := fmt.Sprintf("/spaces/%s/assets/%s/files/%s/process", spaceID, asset.Sys.ID, asset.locale)
method := "PUT"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return err
}
version := strconv.Itoa(asset.Sys.Version)
req.Header.Set("X-Contentful-Version", version)
return service.c.do(req, nil)
}
// Publish published the asset
func (service *AssetsService) Publish(spaceID string, asset *Asset) error {
path := fmt.Sprintf("/spaces/%s/assets/%s/published", spaceID, asset.Sys.ID)
method := "PUT"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return err
}
version := strconv.Itoa(asset.Sys.Version)
req.Header.Set("X-Contentful-Version", version)
return service.c.do(req, asset)
}