Skip to content

Commit fcbc306

Browse files
feat: add method to get limits for file upload low level client
1 parent 45ef1d4 commit fcbc306

File tree

8 files changed

+96
-79
lines changed

8 files changed

+96
-79
lines changed

internal/fileupload/lowlevel/client.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package lowlevel_fileupload //nolint:revive // underscore naming is intentional for this internal package
1+
package lowlevel
22

33
import (
44
"bytes"
@@ -14,42 +14,43 @@ import (
1414
"github.com/snyk/error-catalog-golang-public/snyk_errors"
1515
)
1616

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 {
1919
CreateRevision(ctx context.Context, orgID OrgID) (*UploadRevisionResponseBody, error)
2020
UploadFiles(ctx context.Context, orgID OrgID, revisionID RevisionID, files []UploadFile) error
2121
SealRevision(ctx context.Context, orgID OrgID, revisionID RevisionID) (*SealUploadRevisionResponseBody, error)
22+
23+
GetLimits() Limits
2224
}
2325

2426
// This will force go to complain if the type doesn't satisfy the interface.
25-
var _ Client = (*HTTPClient)(nil)
27+
var _ SealableClient = (*HTTPSealableClient)(nil)
2628

2729
// Config contains configuration for the file upload client.
2830
type Config struct {
2931
BaseURL string
3032
}
3133

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 {
3436
cfg Config
3537
httpClient *http.Client
3638
}
3739

3840
// APIVersion specifies the API version to use for requests.
3941
const APIVersion = "2024-10-15"
4042

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+
)
4647

4748
// 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 {
4950
httpClient := &http.Client{
5051
Transport: http.DefaultTransport,
5152
}
52-
c := HTTPClient{cfg, httpClient}
53+
c := HTTPSealableClient{cfg, httpClient}
5354

5455
for _, opt := range opts {
5556
opt(&c)
@@ -62,7 +63,7 @@ func NewClient(cfg Config, opts ...Opt) *HTTPClient {
6263
}
6364

6465
// 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) {
6667
if orgID == uuid.Nil {
6768
return nil, ErrEmptyOrgID
6869
}
@@ -106,7 +107,7 @@ func (c *HTTPClient) CreateRevision(ctx context.Context, orgID OrgID) (*UploadRe
106107
}
107108

108109
// 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 {
110111
if orgID == uuid.Nil {
111112
return ErrEmptyOrgID
112113
}
@@ -174,8 +175,8 @@ func streamFilesToPipe(pipeWriter *io.PipeWriter, mpartWriter *multipart.Writer,
174175

175176
// validateFiles validates the files before upload.
176177
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)
179180
}
180181

181182
if len(files) == 0 {
@@ -192,16 +193,16 @@ func validateFiles(files []UploadFile) error {
192193
return NewDirectoryError(file.Path)
193194
}
194195

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)
197198
}
198199
}
199200

200201
return nil
201202
}
202203

203204
// 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) {
205206
if orgID == uuid.Nil {
206207
return nil, ErrEmptyOrgID
207208
}
@@ -268,3 +269,11 @@ func handleUnexpectedStatusCodes(body io.ReadCloser, statusCode int, status, ope
268269

269270
return NewHTTPError(statusCode, status, operation, bts)
270271
}
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

Comments
 (0)