1
- package lowlevel_fileupload //nolint:revive // underscore naming is intentional for this internal package
1
+ package lowlevel
2
2
3
3
import (
4
4
"bytes"
@@ -14,42 +14,43 @@ import (
14
14
"github.com/snyk/error-catalog-golang-public/snyk_errors"
15
15
)
16
16
17
- // Client defines the interface for file upload API operations.
18
- type Client interface {
17
+ // SealableClient defines the interface for file upload API operations.
18
+ type SealableClient interface {
19
19
CreateRevision (ctx context.Context , orgID OrgID ) (* UploadRevisionResponseBody , error )
20
20
UploadFiles (ctx context.Context , orgID OrgID , revisionID RevisionID , files []UploadFile ) error
21
21
SealRevision (ctx context.Context , orgID OrgID , revisionID RevisionID ) (* SealUploadRevisionResponseBody , error )
22
+
23
+ GetLimits () Limits
22
24
}
23
25
24
26
// This will force go to complain if the type doesn't satisfy the interface.
25
- var _ Client = (* HTTPClient )(nil )
27
+ var _ SealableClient = (* HTTPSealableClient )(nil )
26
28
27
29
// Config contains configuration for the file upload client.
28
30
type Config struct {
29
31
BaseURL string
30
32
}
31
33
32
- // HTTPClient implements the Client interface for file upload operations via HTTP API.
33
- type HTTPClient struct {
34
+ // HTTPSealableClient implements the SealableClient interface for file upload operations via HTTP API.
35
+ type HTTPSealableClient struct {
34
36
cfg Config
35
37
httpClient * http.Client
36
38
}
37
39
38
40
// APIVersion specifies the API version to use for requests.
39
41
const APIVersion = "2024-10-15"
40
42
41
- // FileSizeLimit specifies the maximum allowed file size in bytes.
42
- const FileSizeLimit = 50_000_000 // arbitrary number, chosen to support max size of SBOMs
43
-
44
- // FileCountLimit specifies the maximum number of files allowed in a single upload.
45
- const FileCountLimit = 100 // arbitrary number, will need to be re-evaluated
43
+ const (
44
+ fileSizeLimit = 50_000_000 // arbitrary number, chosen to support max size of SBOMs
45
+ fileCountLimit = 100 // arbitrary number, will need to be re-evaluated
46
+ )
46
47
47
48
// NewClient creates a new file upload client with the given configuration and options.
48
- func NewClient (cfg Config , opts ... Opt ) * HTTPClient {
49
+ func NewClient (cfg Config , opts ... Opt ) * HTTPSealableClient {
49
50
httpClient := & http.Client {
50
51
Transport : http .DefaultTransport ,
51
52
}
52
- c := HTTPClient {cfg , httpClient }
53
+ c := HTTPSealableClient {cfg , httpClient }
53
54
54
55
for _ , opt := range opts {
55
56
opt (& c )
@@ -62,7 +63,7 @@ func NewClient(cfg Config, opts ...Opt) *HTTPClient {
62
63
}
63
64
64
65
// CreateRevision creates a new upload revision for the specified organization.
65
- func (c * HTTPClient ) CreateRevision (ctx context.Context , orgID OrgID ) (* UploadRevisionResponseBody , error ) {
66
+ func (c * HTTPSealableClient ) CreateRevision (ctx context.Context , orgID OrgID ) (* UploadRevisionResponseBody , error ) {
66
67
if orgID == uuid .Nil {
67
68
return nil , ErrEmptyOrgID
68
69
}
@@ -106,7 +107,7 @@ func (c *HTTPClient) CreateRevision(ctx context.Context, orgID OrgID) (*UploadRe
106
107
}
107
108
108
109
// UploadFiles uploads the provided files to the specified revision. It will not close the file descriptors.
109
- func (c * HTTPClient ) UploadFiles (ctx context.Context , orgID OrgID , revisionID RevisionID , files []UploadFile ) error {
110
+ func (c * HTTPSealableClient ) UploadFiles (ctx context.Context , orgID OrgID , revisionID RevisionID , files []UploadFile ) error {
110
111
if orgID == uuid .Nil {
111
112
return ErrEmptyOrgID
112
113
}
@@ -174,8 +175,8 @@ func streamFilesToPipe(pipeWriter *io.PipeWriter, mpartWriter *multipart.Writer,
174
175
175
176
// validateFiles validates the files before upload.
176
177
func validateFiles (files []UploadFile ) error {
177
- if len (files ) > FileCountLimit {
178
- return NewFileCountLimitError (len (files ), FileCountLimit )
178
+ if len (files ) > fileCountLimit {
179
+ return NewFileCountLimitError (len (files ), fileCountLimit )
179
180
}
180
181
181
182
if len (files ) == 0 {
@@ -192,16 +193,16 @@ func validateFiles(files []UploadFile) error {
192
193
return NewDirectoryError (file .Path )
193
194
}
194
195
195
- if fileInfo .Size () > FileSizeLimit {
196
- return NewFileSizeLimitError (file .Path , fileInfo .Size (), FileSizeLimit )
196
+ if fileInfo .Size () > fileSizeLimit {
197
+ return NewFileSizeLimitError (file .Path , fileInfo .Size (), fileSizeLimit )
197
198
}
198
199
}
199
200
200
201
return nil
201
202
}
202
203
203
204
// SealRevision seals the specified upload revision, marking it as complete.
204
- func (c * HTTPClient ) SealRevision (ctx context.Context , orgID OrgID , revisionID RevisionID ) (* SealUploadRevisionResponseBody , error ) {
205
+ func (c * HTTPSealableClient ) SealRevision (ctx context.Context , orgID OrgID , revisionID RevisionID ) (* SealUploadRevisionResponseBody , error ) {
205
206
if orgID == uuid .Nil {
206
207
return nil , ErrEmptyOrgID
207
208
}
@@ -268,3 +269,11 @@ func handleUnexpectedStatusCodes(body io.ReadCloser, statusCode int, status, ope
268
269
269
270
return NewHTTPError (statusCode , status , operation , bts )
270
271
}
272
+
273
+ // GetLimits returns the upload Limits defined in the low level client.
274
+ func (c * HTTPSealableClient ) GetLimits () Limits {
275
+ return Limits {
276
+ FileCountLimit : fileCountLimit ,
277
+ FileSizeLimit : fileSizeLimit ,
278
+ }
279
+ }
0 commit comments