-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathattachment.go
More file actions
123 lines (111 loc) · 4.41 KB
/
attachment.go
File metadata and controls
123 lines (111 loc) · 4.41 KB
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
package openproject
import (
"context"
"fmt"
"io"
"time"
)
// AttachmentService handles attachments for the OpenProject instance / API.
type AttachmentService struct {
client *Client
}
// Attachment is the object representing OpenProject attachments.
type Attachment struct {
Embedded struct {
Author struct {
Type string `json:"_type,omitempty"`
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Avatar string `json:"avatar,omitempty"`
Links struct {
Self OPGenericLink `json:"self,omitempty"`
} `json:"_links,omitempty"`
} `json:"author,omitempty"`
Container struct {
Type string `json:"_type,omitempty"`
ID int `json:"id,omitempty"`
RowCount int `json:"rowCount,omitempty"`
ColumnCount int `json:"columnCount,omitempty"`
Options struct {
} `json:"options,omitempty"`
Widgets []struct {
Type string `json:"_type,omitempty"`
ID int `json:"id,omitempty"`
Identifier string `json:"identifier,omitempty"`
StartRow int `json:"startRow,omitempty"`
EndRow int `json:"endRow,omitempty"`
StartColumn int `json:"startColumn,omitempty"`
EndColumn int `json:"endColumn,omitempty"`
Options struct {
Name string `json:"name,omitempty"`
Text struct {
Format string `json:"format,omitempty"`
Raw string `json:"raw,omitempty"`
HTML string `json:"html,omitempty"`
} `json:"text,omitempty"`
QueryID string `json:"queryId,omitempty"`
ChartType string `json:"chartType,omitempty"`
} `json:"options,omitempty"`
} `json:"widgets,omitempty"`
CreatedAt time.Time `json:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty"`
Links struct {
Attachments OPGenericLink `json:"attachments,omitempty"`
Scope OPGenericLink `json:"scope,omitempty"`
Self OPGenericLink `json:"self,omitempty"`
} `json:"_links,omitempty"`
} `json:"container,omitempty"`
} `json:"_embedded,omitempty"`
Type string `json:"_type,omitempty"`
ID int `json:"id,omitempty"`
FileName string `json:"fileName,omitempty"`
FileSize int `json:"fileSize,omitempty"`
Description struct {
Format string `json:"format,omitempty"`
Raw string `json:"raw,omitempty"`
HTML string `json:"html,omitempty"`
} `json:"description,omitempty"`
ContentType string `json:"contentType,omitempty"`
Digest AttachmentDigest `json:"digest,omitempty"`
CreatedAt time.Time `json:"createdAt,omitempty"`
Links struct {
Self OPGenericLink `json:"self,omitempty"`
Author OPGenericLink `json:"author,omitempty"`
Container OPGenericLink `json:"container,omitempty"`
StaticDownloadLocation OPGenericLink `json:"staticDownloadLocation,omitempty"`
DownloadLocation OPGenericLink `json:"downloadLocation,omitempty"`
} `json:"_links,omitempty"`
}
// AttachmentDigest wraps algorithm and hash
type AttachmentDigest struct {
Algorithm string `json:"algorithm,omitempty" structs:"algorithm,omitempty"`
Hash string `json:"hash,omitempty" structs:"hash,omitempty"`
}
// GetWithContext gets a wiki page from OpenProject using its ID
func (s *AttachmentService) GetWithContext(ctx context.Context, attachmentID string) (*Attachment, *Response, error) {
apiEndPoint := fmt.Sprintf("api/v3/attachments/%s", attachmentID)
Obj, Resp, err := GetWithContext(ctx, s, apiEndPoint)
return Obj.(*Attachment), Resp, err
}
// Get wraps GetWithContext using the background context.
func (s *AttachmentService) Get(attachmentID string) (*Attachment, *Response, error) {
return s.GetWithContext(context.Background(), attachmentID)
}
// DownloadWithContext downloads a file from attachment using attachment ID
func (s *AttachmentService) DownloadWithContext(ctx context.Context, attachmentID string) (*[]byte, error) {
apiEndpoint := fmt.Sprintf("api/v3/attachments/%s/content", attachmentID)
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Download(req)
if err != nil {
return nil, err
}
respBytes, err := io.ReadAll(resp.Body)
return &respBytes, err
}
// Download wraps DownloadWithContext using the background context.
func (s *AttachmentService) Download(attachmentID string) (*[]byte, error) {
return s.DownloadWithContext(context.Background(), attachmentID)
}