@@ -43,7 +43,7 @@ func fatal(format string, a ...interface{}) {
4343}
4444
4545// GetFileMd5 returns a checksum for a given file
46- func GetFileMd5 (file * os. File ) (string , error ) {
46+ func GetFileMd5 (file io. Reader ) (string , error ) {
4747 var fileHash string
4848 hash := md5 .New ()
4949 if _ , err := io .Copy (hash , file ); err != nil {
@@ -143,13 +143,31 @@ func UploadFile(file *os.File, filename string, bucket string) (err error) {
143143 return nil
144144}
145145
146+ func ShouldVersionFile (filename string , skipVersioning bool ) (bool ) {
147+ ext := filepath .Ext (filename )
148+ return ! skipVersioning && (ext == ".js" || ext == ".css" )
149+ }
150+
151+ func GetUploadFilename (file io.Reader , filename string , skipVersioning bool ) (string , error ) {
152+ uploadFilename := filename
153+ if ShouldVersionFile (filename , skipVersioning ) {
154+ checksum , errMd5 := GetFileMd5 (file )
155+ if errMd5 != nil {
156+ return "" , errMd5
157+ }
158+ uploadFilename = GetVersionedFilename (filename , checksum )
159+ }
160+ return uploadFilename , nil
161+ }
162+
146163// VersionAndUploadFiles will verion files and upload them to s3 and return
147164// a map of filenames and their version hashes
148165func VersionAndUploadFiles (
149166 bucket string ,
150167 directory string ,
151168 filenames []string ,
152169 dryRun bool ,
170+ skipVersioning bool ,
153171) (map [string ]string , error ) {
154172 fileVersions := map [string ]string {}
155173
@@ -162,14 +180,9 @@ func VersionAndUploadFiles(
162180 }
163181 defer file .Close ()
164182
165- ext := filepath .Ext (filename )
166- uploadFilename := filename
167- if ext == ".js" || ext == ".css" {
168- checksum , errMd5 := GetFileMd5 (file )
169- if errMd5 != nil {
170- return fileVersions , errMd5
171- }
172- uploadFilename = GetVersionedFilename (filename , checksum )
183+ uploadFilename , err := GetUploadFilename (file , filename , skipVersioning )
184+ if err != nil {
185+ return fileVersions , err
173186 }
174187 bucketFilename := path .Join (directory , uploadFilename )
175188 fileURL := GetFileURL (bucket , bucketFilename )
@@ -220,6 +233,7 @@ func main() {
220233 format := flag .String ("format" , "json" , "format of the output [json,csv]" )
221234 dryRun := flag .Bool ("dry-run" , false , "print the output only, skip file uploads and manifest creation" )
222235 printVersion := flag .Bool ("v" , false , "print the current buffer-static-upload version" )
236+ skipVersioning := flag .Bool ("skip-versioning" , false , "skip versioning uploaded files" )
223237 flag .Parse ()
224238
225239 if * printVersion {
@@ -239,7 +253,7 @@ func main() {
239253 fmt .Printf ("Found %d files to upload and version:\n " , len (files ))
240254
241255 SetupS3Uploader ()
242- fileVersions , err := VersionAndUploadFiles (* s3Bucket , * directory , files , * dryRun )
256+ fileVersions , err := VersionAndUploadFiles (* s3Bucket , * directory , files , * dryRun , * skipVersioning )
243257 if err != nil {
244258 fatal ("failed to upload files %s" , err )
245259 }
0 commit comments