forked from labd/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasset.go
238 lines (186 loc) · 5.66 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
package contentful
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
)
// AssetsService service
type AssetsService service
// Asset represents a Contentful asset
type Asset struct {
Locale string
Sys *Sys `json:"sys,omitempty"`
Fields *AssetFields `json:"fields,omitempty"`
}
// AssetFields model
type AssetFields struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
File *File `json:"file,omitempty"`
}
// File represents a Contentful File
type File struct {
URL string `json:"url,omitempty"`
UploadURL string `json:"upload,omitempty"`
UploadFrom *UploadFrom `json:"uploadFrom,omitempty"`
Details *FileDetails `json:"details,omitempty"`
FileName string `json:"fileName,omitempty"`
ContentType string `json:"contentType,omitempty"`
}
// UploadFrom model
type UploadFrom struct {
Sys *Sys `json:"sys,omitempty"`
}
// FileDetails model
type FileDetails struct {
Size int `json:"size,omitempty"`
Image *ImageFields `json:"image,omitempty"`
}
// ImageFields model
type ImageFields struct {
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
}
// 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
}
// ListPublished return a content type collection, with only activated content types
func (service *AssetsService) ListPublished(spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s/public/assets", spaceID)
method := "GET"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return nil
}
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 != nil && asset.Sys.ID != "" {
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)
}
// Unpublish the asset
func (service *AssetsService) Unpublish(spaceID string, asset *Asset) error {
path := fmt.Sprintf("/spaces/%s/assets/%s/published", 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, asset)
}
// Archive archives the asset
func (service *AssetsService) Archive(spaceID string, asset *Asset) error {
path := fmt.Sprintf("/spaces/%s/assets/%s/archived", 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)
}
// Unarchive unarchives the asset
func (service *AssetsService) Unarchive(spaceID string, asset *Asset) error {
path := fmt.Sprintf("/spaces/%s/assets/%s/archived", 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, asset)
}