@@ -43,7 +43,7 @@ func fatal(format string, a ...interface{}) {
43
43
}
44
44
45
45
// GetFileMd5 returns a checksum for a given file
46
- func GetFileMd5 (file * os. File ) (string , error ) {
46
+ func GetFileMd5 (file io. Reader ) (string , error ) {
47
47
var fileHash string
48
48
hash := md5 .New ()
49
49
if _ , err := io .Copy (hash , file ); err != nil {
@@ -143,13 +143,31 @@ func UploadFile(file *os.File, filename string, bucket string) (err error) {
143
143
return nil
144
144
}
145
145
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
+
146
163
// VersionAndUploadFiles will verion files and upload them to s3 and return
147
164
// a map of filenames and their version hashes
148
165
func VersionAndUploadFiles (
149
166
bucket string ,
150
167
directory string ,
151
168
filenames []string ,
152
169
dryRun bool ,
170
+ skipVersioning bool ,
153
171
) (map [string ]string , error ) {
154
172
fileVersions := map [string ]string {}
155
173
@@ -162,14 +180,9 @@ func VersionAndUploadFiles(
162
180
}
163
181
defer file .Close ()
164
182
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
173
186
}
174
187
bucketFilename := path .Join (directory , uploadFilename )
175
188
fileURL := GetFileURL (bucket , bucketFilename )
@@ -220,6 +233,7 @@ func main() {
220
233
format := flag .String ("format" , "json" , "format of the output [json,csv]" )
221
234
dryRun := flag .Bool ("dry-run" , false , "print the output only, skip file uploads and manifest creation" )
222
235
printVersion := flag .Bool ("v" , false , "print the current buffer-static-upload version" )
236
+ skipVersioning := flag .Bool ("skip-versioning" , false , "skip versioning uploaded files" )
223
237
flag .Parse ()
224
238
225
239
if * printVersion {
@@ -239,7 +253,7 @@ func main() {
239
253
fmt .Printf ("Found %d files to upload and version:\n " , len (files ))
240
254
241
255
SetupS3Uploader ()
242
- fileVersions , err := VersionAndUploadFiles (* s3Bucket , * directory , files , * dryRun )
256
+ fileVersions , err := VersionAndUploadFiles (* s3Bucket , * directory , files , * dryRun , * skipVersioning )
243
257
if err != nil {
244
258
fatal ("failed to upload files %s" , err )
245
259
}
0 commit comments