Skip to content

Commit 58f4e24

Browse files
implemented multipart upload
1 parent 71f2784 commit 58f4e24

File tree

7 files changed

+449
-1
lines changed

7 files changed

+449
-1
lines changed

internal/commands/scan.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ const (
129129
sbomScanTypeErrMsg = "The --sbom-only flag can only be used when the scan type is sca"
130130
BranchPrimaryPrefix = "--branch-primary="
131131
OverridePolicyManagement = "override-policy-management"
132+
maxSizeGB = 5 // 5 GB
133+
maxSizeBytes = maxSizeGB * 1024 * 1024 * 1024 // 5 GB in bytes
132134
)
133135

134136
var (
@@ -2082,7 +2084,26 @@ func uploadZip(uploadsWrapper wrappers.UploadsWrapper, zipFilePath string, unzip
20822084
var zipFilePathErr error
20832085
// Send a request to uploads service
20842086
var preSignedURL *string
2085-
preSignedURL, zipFilePathErr = uploadsWrapper.UploadFile(zipFilePath, featureFlagsWrapper)
2087+
2088+
// calculate file size and compare with 5GB limit
2089+
fileInfo, err := os.Stat(zipFilePath)
2090+
if err != nil {
2091+
return "", zipFilePath, errors.Wrapf(err, "Failed to stat %s", zipFilePath)
2092+
}
2093+
logger.PrintIfVerbose(fmt.Sprintf("Zip size before upload: %.2fMB\n", float64(fileInfo.Size())/mbBytes))
2094+
2095+
// check for INCREASE_FILE_UPLOAD_LIMIT feature flag
2096+
flagResponse, _ := wrappers.GetSpecificFeatureFlag(featureFlagsWrapper, wrappers.IncreaseFileUploadLimit)
2097+
2098+
if flagResponse.Status && fileInfo.Size() > maxSizeBytes {
2099+
// File size >5GB, proceed with multipart upload
2100+
logger.PrintIfVerbose("File size >5GB and INCREASE_FILE_UPLOAD_LIMIT flag is enabled,hence uploading file in multiple parts...")
2101+
preSignedURL, zipFilePathErr = uploadsWrapper.UploadFileInMultipart(zipFilePath, featureFlagsWrapper)
2102+
} else {
2103+
// File size is within <=5GB, proceed with upload in single part
2104+
logger.PrintIfVerbose("File size is within the limit and uploading file in a single part...")
2105+
preSignedURL, zipFilePathErr = uploadsWrapper.UploadFile(zipFilePath, featureFlagsWrapper)
2106+
}
20862107
if zipFilePathErr != nil {
20872108
if unzip || !userProvidedZip {
20882109
return "", zipFilePath, errors.Wrapf(zipFilePathErr, "%s: Failed to upload sources file\n", failedCreating)

internal/params/binds.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,8 @@ var EnvVarsBinds = []struct {
8080
{RiskManagementPathKey, RiskManagementPathEnv, "api/risk-management/projects/%s/results?scanID=%s"},
8181
{ConfigFilePathKey, ConfigFilePathEnv, ""},
8282
{RealtimeScannerPathKey, RealtimeScannerPathEnv, "api/realtime-scanner"},
83+
{StartMultiPartUploadPathKey, StartMultiPartUploadPathEnv, "api/uploads/start-multipart-upload"},
84+
{MultipartPresignedPathKey, MultipartPresignedPathEnv, "api/uploads/multipart-presigned"},
85+
{CompleteMultiPartUploadPathKey, CompleteMultipartUploadPathEnv, "api/uploads/complete-multipart-upload"},
86+
{MultipartFileSize, MultipartFileSizeEnv, "2"},
8387
}

internal/params/envs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,8 @@ const (
8080
RiskManagementPathEnv = "CX_RISK_MANAGEMENT_PATH"
8181
ConfigFilePathEnv = "CX_CONFIG_FILE_PATH"
8282
RealtimeScannerPathEnv = "CX_REALTIME_SCANNER_PATH"
83+
StartMultiPartUploadPathEnv = "CX_START_MULTIPART_UPLOAD_PATH"
84+
MultipartPresignedPathEnv = "CX_MULTIPART_PRESIGNED_URL_PATH"
85+
CompleteMultipartUploadPathEnv = "CX_COMPLETE_MULTIPART_UPLOAD_PATH"
86+
MultipartFileSizeEnv = "MULTIPART_FILE_SIZE"
8387
)

internal/params/keys.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,8 @@ var (
7979
RiskManagementPathKey = strings.ToLower(RiskManagementPathEnv)
8080
ConfigFilePathKey = strings.ToLower(ConfigFilePathEnv)
8181
RealtimeScannerPathKey = strings.ToLower(RealtimeScannerPathEnv)
82+
StartMultiPartUploadPathKey = strings.ToLower(StartMultiPartUploadPathEnv)
83+
MultipartPresignedPathKey = strings.ToLower(MultipartPresignedPathEnv)
84+
CompleteMultiPartUploadPathKey = strings.ToLower(CompleteMultipartUploadPathEnv)
85+
MultipartFileSize = strings.ToLower(MultipartFileSizeEnv)
8286
)

internal/wrappers/feature-flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const OssRealtimeEnabled = "OSS_REALTIME_ENABLED"
1717
const ScsLicensingV2Enabled = "SSCS_NEW_LICENSING_ENABLED"
1818
const DirectAssociationEnabled = "DIRECT_APP_ASSOCIATION_ENABLED"
1919
const maxRetries = 3
20+
const IncreaseFileUploadLimit = "INCREASE_FILE_UPLOAD_LIMIT"
2021

2122
var DefaultFFLoad bool = false
2223

0 commit comments

Comments
 (0)