Repository navigation
Expand file tree
/
Copy pathfiles.go
More file actions
123 lines (104 loc) · 3.77 KB
/
Copy pathfiles.go
File metadata and controls
123 lines (104 loc) · 3.77 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 viya
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"time"
"go.opentelemetry.io/otel/codes"
)
// FileListOptions configures SAS Viya Files Service collection paging and filtering.
type FileListOptions struct {
Start int
Limit int
FilterName string
}
// ViyaFile describes a file stored in the SAS Viya Files Service.
type ViyaFile struct {
ID string `json:"id"`
Name string `json:"name"`
ContentType string `json:"contentType,omitempty"`
Size int64 `json:"size,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
CreationTimeStamp time.Time `json:"creationTimeStamp,omitempty"`
ModifiedBy string `json:"modifiedBy,omitempty"`
ModifiedTimeStamp time.Time `json:"modifiedTimeStamp,omitempty"`
Links []Link `json:"links,omitempty"`
Version int `json:"version,omitempty"`
}
// ViyaFilesResponse is a collection of SAS Viya Files Service files.
type ViyaFilesResponse = ListResponse[ViyaFile]
// GetFiles returns files visible to the caller in the SAS Viya Files Service.
func (c *Client) GetFiles(ctx context.Context, opts FileListOptions) (resp ViyaFilesResponse, err error) {
ctx, span := tracer.Start(ctx, "GetFiles")
defer span.End()
req := c.collectionRequest(ctx, ListOptions{Start: opts.Start, Limit: opts.Limit}).
SetResult(&resp)
if opts.FilterName != "" {
req.SetQueryParam("filter", fmt.Sprintf("contains(name,'%s')", strings.ReplaceAll(opts.FilterName, "'", "''")))
}
r, err := req.Get("/files/files")
if err != nil {
return resp, err
}
if !r.IsStatusSuccess() {
span.SetStatus(codes.Error, r.String())
return resp, fmt.Errorf("failed to get files, status code: %d", r.StatusCode())
}
return resp, nil
}
// UploadFile uploads content to the SAS Viya Files Service.
func (c *Client) UploadFile(ctx context.Context, fileName string, contentType string, content []byte) (resp ViyaFile, err error) {
return c.UploadFileFromReader(ctx, fileName, contentType, bytes.NewReader(content))
}
// UploadFileFromReader uploads content from r to the SAS Viya Files Service.
func (c *Client) UploadFileFromReader(ctx context.Context, fileName string, contentType string, r io.Reader) (resp ViyaFile, err error) {
ctx, span := tracer.Start(ctx, "UploadFileFromReader")
defer span.End()
if contentType == "" {
contentType = "application/octet-stream"
}
httpResp, err := c.client.R().
SetContext(ctx).
SetHeader("Accept", AcceptJSONError).
SetHeader("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, strings.ReplaceAll(fileName, `"`, `\"`))).
SetContentType(contentType).
SetBody(r).
SetResult(&resp).
Post("/files/files")
if err != nil {
return resp, err
}
if !httpResp.IsStatusSuccess() {
span.SetStatus(codes.Error, httpResp.String())
return resp, fmt.Errorf("failed to upload file, status code: %d", httpResp.StatusCode())
}
return resp, nil
}
// DownloadFile downloads content from the SAS Viya Files Service.
func (c *Client) DownloadFile(ctx context.Context, fileID string) (content []byte, err error) {
if fileID == "" {
return nil, &ErrInvalidParameter{Parameter: "fileID", Reason: "must not be empty"}
}
ctx, span := tracer.Start(ctx, "DownloadFile")
defer span.End()
r, err := c.client.R().
SetContext(ctx).
SetHeader("Accept", AcceptOctetError).
Get(fmt.Sprintf("/files/files/%s/content", fileID))
if err != nil {
return nil, err
}
if !r.IsStatusSuccess() {
span.SetStatus(codes.Error, r.String())
return nil, fmt.Errorf("failed to download file, status code: %d", r.StatusCode())
}
defer r.Body.Close()
content, err = io.ReadAll(r.Body)
if err != nil {
span.SetStatus(codes.Error, err.Error())
return nil, fmt.Errorf("failed to read file content: %w", err)
}
return content, nil
}