|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/md5" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "mime" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/aws/aws-sdk-go/aws" |
| 15 | + "github.com/aws/aws-sdk-go/aws/credentials" |
| 16 | + "github.com/aws/aws-sdk-go/aws/endpoints" |
| 17 | + "github.com/aws/aws-sdk-go/aws/session" |
| 18 | + "github.com/aws/aws-sdk-go/service/s3/s3manager" |
| 19 | +) |
| 20 | + |
| 21 | +// GetFileMd5 returns a checksum for a given file |
| 22 | +func GetFileMd5(file *os.File) (string, error) { |
| 23 | + var fileHash string |
| 24 | + hash := md5.New() |
| 25 | + if _, err := io.Copy(hash, file); err != nil { |
| 26 | + return fileHash, err |
| 27 | + } |
| 28 | + hashInBytes := hash.Sum(nil)[:16] |
| 29 | + fileHash = hex.EncodeToString(hashInBytes) |
| 30 | + return fileHash, nil |
| 31 | +} |
| 32 | + |
| 33 | +// GetVersionedFilename returns a new filename with the version before the extension |
| 34 | +func GetVersionedFilename(filename string, version string) string { |
| 35 | + ext := filepath.Ext(filename) |
| 36 | + versionedExt := "." + version + ext |
| 37 | + versionedFilename := strings.Replace(filename, ext, versionedExt, 1) |
| 38 | + return versionedFilename |
| 39 | +} |
| 40 | + |
| 41 | +// GetFileMimeType returns the mime type of a file using it's extension |
| 42 | +func GetFileMimeType(filename string) string { |
| 43 | + ext := filepath.Ext(filename) |
| 44 | + return mime.TypeByExtension(ext) |
| 45 | +} |
| 46 | + |
| 47 | +// GetFilesFromGlobsList returns a list of files that match a list of |
| 48 | +// comma-deliniated file path globs |
| 49 | +func GetFilesFromGlobsList(globList string) ([]string, error) { |
| 50 | + var files []string |
| 51 | + globs := strings.Split(globList, ",") |
| 52 | + |
| 53 | + for _, glob := range globs { |
| 54 | + fileList, err := filepath.Glob(glob) |
| 55 | + if err != nil { |
| 56 | + return files, err |
| 57 | + } |
| 58 | + files = append(files, fileList...) |
| 59 | + } |
| 60 | + return files, nil |
| 61 | +} |
| 62 | + |
| 63 | +// GetS3Uploader returns a configured Uploader |
| 64 | +func GetS3Uploader() (*s3manager.Uploader, error) { |
| 65 | + var uploader *s3manager.Uploader |
| 66 | + |
| 67 | + awsAccessKeyID := os.Getenv("AWS_ACCESS_KEY_ID") |
| 68 | + awsSecretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY") |
| 69 | + |
| 70 | + creds := credentials.NewStaticCredentials(awsAccessKeyID, awsSecretAccessKey, "") |
| 71 | + |
| 72 | + sess := session.Must(session.NewSession(&aws.Config{ |
| 73 | + Credentials: creds, |
| 74 | + Region: aws.String(endpoints.UsEast1RegionID), |
| 75 | + })) |
| 76 | + |
| 77 | + _, err := creds.Get() |
| 78 | + if err != nil { |
| 79 | + fmt.Printf("Bad credentials: %s", err) |
| 80 | + return uploader, err |
| 81 | + } |
| 82 | + |
| 83 | + uploader = s3manager.NewUploader(sess) |
| 84 | + return uploader, nil |
| 85 | +} |
| 86 | + |
| 87 | +// VersionAndUploadFiles will verion files and upload them to s3 and return |
| 88 | +// a map of filenames and their version hashes |
| 89 | +func VersionAndUploadFiles(bucket string, filenames []string) (map[string]string, error) { |
| 90 | + fileVersions := map[string]string{} |
| 91 | + |
| 92 | + uploader, err := GetS3Uploader() |
| 93 | + if err != nil { |
| 94 | + return fileVersions, err |
| 95 | + } |
| 96 | + |
| 97 | + for _, filename := range filenames { |
| 98 | + file, err := os.Open(filename) |
| 99 | + if err != nil { |
| 100 | + return fileVersions, err |
| 101 | + } |
| 102 | + defer file.Close() |
| 103 | + |
| 104 | + checksum, err := GetFileMd5(file) |
| 105 | + if err != nil { |
| 106 | + return fileVersions, err |
| 107 | + } |
| 108 | + |
| 109 | + fileVersions[filename] = checksum |
| 110 | + versionedFilename := GetVersionedFilename(filename, checksum) |
| 111 | + mimeType := GetFileMimeType(filename) |
| 112 | + |
| 113 | + result, err := uploader.Upload(&s3manager.UploadInput{ |
| 114 | + Bucket: aws.String(bucket), |
| 115 | + Key: aws.String(versionedFilename), |
| 116 | + ContentType: aws.String(mimeType), |
| 117 | + CacheControl: aws.String("public, max-age=31520626"), |
| 118 | + Expires: aws.Time(time.Now().AddDate(10, 0, 0)), |
| 119 | + Body: file, |
| 120 | + }) |
| 121 | + if err != nil { |
| 122 | + return fileVersions, err |
| 123 | + } |
| 124 | + |
| 125 | + fmt.Printf("Uploaded %s\n", result.Location) |
| 126 | + } |
| 127 | + |
| 128 | + return fileVersions, nil |
| 129 | +} |
0 commit comments