-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfiles.go
190 lines (156 loc) · 4.83 KB
/
files.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
package replicate
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
"os"
"path/filepath"
)
type File struct {
ID string `json:"id"`
Name string `json:"name"`
ContentType string `json:"content_type"`
Size int `json:"size"`
Etag string `json:"etag"`
Checksums map[string]string `json:"checksums"`
Metadata map[string]string `json:"metadata"`
CreatedAt string `json:"created_at"`
ExpiresAt string `json:"expires_at"`
URLs map[string]string `json:"urls"`
rawJSON json.RawMessage `json:"-"`
}
var _ json.Unmarshaler = (*File)(nil)
func (f *File) UnmarshalJSON(data []byte) error {
f.rawJSON = data
type Alias File
alias := &struct{ *Alias }{Alias: (*Alias)(f)}
return json.Unmarshal(data, alias)
}
func (f *File) RawJSON() json.RawMessage {
return f.rawJSON
}
type CreateFileOptions struct {
Filename string `json:"filename"`
ContentType string `json:"content_type"`
Metadata map[string]string `json:"metadata"`
}
// CreateFileFromPath creates a new file from a file path.
func (r *Client) CreateFileFromPath(ctx context.Context, filePath string, options *CreateFileOptions) (*File, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()
if options == nil {
options = &CreateFileOptions{}
}
if options.Filename == "" {
_, options.Filename = filepath.Split(filePath)
}
if options.ContentType == "" {
if options.Filename != "" {
ext := filepath.Ext(options.Filename)
options.ContentType = mime.TypeByExtension(ext)
}
}
return r.createFile(ctx, f, *options)
}
// CreateFileFromBytes creates a new file from bytes.
func (r *Client) CreateFileFromBytes(ctx context.Context, data []byte, options *CreateFileOptions) (*File, error) {
buf := bytes.NewBuffer(data)
if options == nil {
options = &CreateFileOptions{}
}
if options.ContentType == "" {
options.ContentType = http.DetectContentType(data)
}
return r.createFile(ctx, buf, *options)
}
// CreateFileFromBuffer creates a new file from a buffer.
func (r *Client) CreateFileFromBuffer(ctx context.Context, buf *bytes.Buffer, options *CreateFileOptions) (*File, error) {
if options == nil {
options = &CreateFileOptions{}
}
return r.createFile(ctx, buf, *options)
}
// CreateFile creates a new file.
func (r *Client) createFile(ctx context.Context, reader io.Reader, options CreateFileOptions) (*File, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
filename := options.Filename
if filename == "" {
filename = "file"
}
contentType := options.ContentType
if contentType == "" {
contentType = "application/octet-stream"
}
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="content"; filename="%s"`, filename))
h.Set("Content-Type", contentType)
content, err := writer.CreatePart(h)
if err != nil {
return nil, fmt.Errorf("failed to create form file: %w", err)
}
_, err = io.Copy(content, reader)
if err != nil {
return nil, fmt.Errorf("failed to write file to form: %w", err)
}
if options.Metadata != nil {
metadata, err := json.Marshal(options.Metadata)
if err != nil {
return nil, fmt.Errorf("failed to marshal metadata: %w", err)
}
err = writer.WriteField("metadata", string(metadata))
if err != nil {
return nil, fmt.Errorf("failed to write metadata to form: %w", err)
}
}
err = writer.Close()
if err != nil {
return nil, fmt.Errorf("failed to close writer: %w", err)
}
req, err := r.newRequest(ctx, http.MethodPost, "/files", body)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
file := &File{}
err = r.do(req, file)
if err != nil {
return nil, fmt.Errorf("failed to create file: %w", err)
}
return file, nil
}
// ListFiles lists your files.
func (r *Client) ListFiles(ctx context.Context) (*Page[File], error) {
response := &Page[File]{}
err := r.fetch(ctx, http.MethodGet, "/files", nil, response)
if err != nil {
return nil, fmt.Errorf("failed to list files: %w", err)
}
return response, nil
}
// GetFile retrieves information about a file.
func (r *Client) GetFile(ctx context.Context, fileID string) (*File, error) {
file := &File{}
err := r.fetch(ctx, http.MethodGet, fmt.Sprintf("/files/%s", fileID), nil, file)
if err != nil {
return nil, fmt.Errorf("failed to get file: %w", err)
}
return file, nil
}
// DeleteFile deletes a file.
func (r *Client) DeleteFile(ctx context.Context, fileID string) error {
err := r.fetch(ctx, http.MethodDelete, fmt.Sprintf("/files/%s", fileID), nil, nil)
if err != nil {
return fmt.Errorf("failed to delete file: %w", err)
}
return nil
}