Skip to content
Snippets Groups Projects
Commit 18bdb90e authored by James Page's avatar James Page
Browse files

#29 Reworked UploadWithSettingsRevised to handle file paths in a simpler way

parent 4f50e736
Branches
Tags
1 merge request!32Resolve "Extend new S3.UploadWithSettingsRevised to handle file path and file name separately"
This commit is part of merge request !32. Comments created here will be created in the context of that merge request.
......@@ -32,11 +32,10 @@ type S3UploadSettings struct {
RetrieveSignedUrl bool
ExpiryDuration *time.Duration // Used to set expiry datetime of download links. NB: does not affect deletion of object from S3 bucket.
AddContentDisposition bool
FileName string // Used to specify the file name (excluding path) for content disposition; if FilePath is not present, it is used as the object key as well.
FilePath string // The complete file path (including name) used for the object key in S3.
GenerateFileNameFromParts bool // Whether the file extension needs to be specified. If true, supply FilePrefix and FileExt. Adds a UUID between the prefix and the extension.
FilePrefix string // Required when GenerateFileNameFromParts is true
FileExt string // Required when GenerateFileNameFromParts is true
FilePath string
FileName string
FileExt string
InsertUUID bool
}
// Duration constants
......@@ -155,35 +154,27 @@ func (s SessionWithHelpers) UploadWithSettings(data []byte, bucket, fileName str
// UploadWithSettingsRevised can be renamed to UploadWithSettings once original function has been deprecated.
func (s SessionWithHelpers) UploadWithSettingsRevised(data []byte, bucket string, settings S3UploadSettings) (S3UploadResponse, error) {
var fileName, filePath, uploadUrl string
var fullFileName, uploadUrl string
// fileName is used as the object key in S3
if settings.FileName != "" {
fileName = settings.FileName
}
if settings.GenerateFileNameFromParts {
fileName = fmt.Sprintf("%s_%s.%s", settings.FilePrefix, uuid.New().String(), settings.FileExt)
uuidString := ""
if settings.InsertUUID {
uuidString = uuid.New().String()
}
if settings.FilePath != "" {
filePath = settings.FilePath
} else {
filePath = fileName
}
fullFileName = fmt.Sprintf("%s%s_%s.%s", settings.FilePath, settings.FileName, uuidString, settings.FileExt)
// Uploaded objects require a key
if fileName == "" {
if fullFileName == "" {
return S3UploadResponse{}, errors.Error("no file name supplied for upload")
}
if settings.MimeType == "" {
settings.MimeType = getTypeForFilename(fileName)
settings.MimeType = getTypeForFilename(fullFileName)
}
putInput := &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(filePath),
Key: aws.String(fullFileName),
ContentType: aws.String(string(settings.MimeType)),
Body: bytes.NewReader(data),
}
......@@ -203,7 +194,7 @@ func (s SessionWithHelpers) UploadWithSettingsRevised(data []byte, bucket string
if settings.AddContentDisposition {
headers = map[string]string{
"content-disposition": "attachment; filename=\"" + fileName + "\"",
"content-disposition": fmt.Sprintf("attachment; filename=\"%s.%s\"", settings.FileName, settings.FileExt),
}
}
......@@ -212,7 +203,7 @@ func (s SessionWithHelpers) UploadWithSettingsRevised(data []byte, bucket string
downloadUrlExpiry = *settings.ExpiryDuration
}
uploadUrl, err = s.GetSignedDownloadURL(bucket, filePath, downloadUrlExpiry, headers)
uploadUrl, err = s.GetSignedDownloadURL(bucket, fullFileName, downloadUrlExpiry, headers)
if err != nil {
return S3UploadResponse{}, err
}
......@@ -223,7 +214,7 @@ func (s SessionWithHelpers) UploadWithSettingsRevised(data []byte, bucket string
response := S3UploadResponse{
URL: uploadUrl,
Filename: fileName,
Filename: fullFileName,
Bucket: bucket,
FileSize: fileSizeInBytes,
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment