diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 798809a..9ccad6b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -53,6 +53,7 @@ jobs: htmlCacheControl: no-cache imageCacheControl: max-age=864001 otherCacheControl: max-age=2592001 + pdfCacheControl: no-cache skipSetting: false # not support recursive pattern ** exclude: | diff --git a/action.yml b/action.yml index abb8be0..af1ebf9 100644 --- a/action.yml +++ b/action.yml @@ -51,6 +51,10 @@ inputs: description: 'Cache-Control for image' required: false default: 'max-age=864000' + pdfCacheControl: + description: 'Cache-Control for PDF' + required: false + default: 'max-age=2592000' otherCacheControl: description: 'Cache-Control for other files' required: false @@ -73,4 +77,6 @@ runs: HTML_CACHE_CONTROL: ${{ inputs.htmlCacheControl }} IMAGE_CACHE_CONTROL: ${{ inputs.imageCacheControl }} OTHER_CACHE_CONTROL: ${{ inputs.otherCacheControl }} + PDF_CACHE_CONTROL: ${{ inputs.pdfCacheControl }} + diff --git a/config/config.go b/config/config.go index f489b0a..9939690 100644 --- a/config/config.go +++ b/config/config.go @@ -21,13 +21,14 @@ var ( Client *oss.Client Bucket *oss.Bucket SkipSetting bool - IsIncremental bool + IsIncremental bool IndexPage string NotFoundPage string HTMLCacheControl string ImageCacheControl string OtherCacheControl string + PDFCacheControl string ) func init() { @@ -49,6 +50,7 @@ func init() { HTMLCacheControl = utils.Getenv("HTML_CACHE_CONTROL", "no-cache") ImageCacheControl = utils.Getenv("IMAGE_CACHE_CONTROL", "max-age=864000") OtherCacheControl = utils.Getenv("OTHER_CACHE_CONTROL", "max-age=2592000") + PDFCacheControl = utils.Getenv("PDF_CACHE_CONTROL", "max-age=2592000") currentPath, err := os.Getwd() if err != nil { @@ -57,8 +59,8 @@ func init() { fmt.Printf("current directory: %s\n", currentPath) fmt.Printf("endpoint: %s\nbucketName: %s\nfolder: %s\nincremental: %t\nexclude: %v\nindexPage: %s\nnotFoundPage: %s\nisCname: %t\nskipSetting: %t\n", Endpoint, BucketName, Folder, IsIncremental, Exclude, IndexPage, NotFoundPage, IsCname, SkipSetting) - fmt.Printf("HTMLCacheControl: %s\nimageCacheControl: %s\notherCacheControl: %s\n", - HTMLCacheControl, ImageCacheControl, OtherCacheControl) + fmt.Printf("HTMLCacheControl: %s\nimageCacheControl: %s\notherCacheControl: %s\npdfCacheControl: %s\n", + HTMLCacheControl, ImageCacheControl, OtherCacheControl, PDFCacheControl) Client, err = oss.New(Endpoint, AccessKeyID, AccessKeySecret, oss.UseCname(IsCname)) if err != nil { diff --git a/main_test.go b/main_test.go index 6a97200..ac28be8 100644 --- a/main_test.go +++ b/main_test.go @@ -127,6 +127,8 @@ func testUploadIncrementalFirst(t *testing.T) { cacheControl := props.Get("Cache-Control") if utils.IsImage(u.ObjectKey) { assert.Equal(config.ImageCacheControl, cacheControl) + } else if utils.IsPDF(u.ObjectKey) { + assert.Equal(config.PDFCacheControl, cacheControl) } else if utils.IsHTML(u.ObjectKey) { assert.Equal(config.HTMLCacheControl, cacheControl) } else { diff --git a/operation/upload.go b/operation/upload.go index 7745fd9..40fe312 100644 --- a/operation/upload.go +++ b/operation/upload.go @@ -86,6 +86,8 @@ func getCacheControlOption(item *utils.FileInfoType) oss.Option { // pic name may not contains hash, so use different strategy // 10 days value = config.ImageCacheControl + } else if utils.IsPDF(filename) { + value = config.PDFCacheControl } else { // static assets like .js .css, use contentHash in file name, so html can update these files. // 30 days diff --git "a/testdata/group1/\350\256\272\351\233\252\350\216\261\343\200\212\347\210\261\347\232\204\345\223\262\345\255\246\343\200\213\344\270\255\347\232\204\346\240\271\351\232\220\345\226\273.pdf" "b/testdata/group1/\350\256\272\351\233\252\350\216\261\343\200\212\347\210\261\347\232\204\345\223\262\345\255\246\343\200\213\344\270\255\347\232\204\346\240\271\351\232\220\345\226\273.pdf" new file mode 100644 index 0000000..8cb7969 Binary files /dev/null and "b/testdata/group1/\350\256\272\351\233\252\350\216\261\343\200\212\347\210\261\347\232\204\345\223\262\345\255\246\343\200\213\344\270\255\347\232\204\346\240\271\351\232\220\345\226\273.pdf" differ diff --git a/utils/ext.go b/utils/ext.go index efa6f3f..0f0e9d4 100644 --- a/utils/ext.go +++ b/utils/ext.go @@ -10,6 +10,11 @@ func IsHTML(filename string) bool { return strings.HasSuffix(strings.ToLower(filename), ".html") } +// IsPDF is used to determine if a file is PDF +func IsPDF(filename string) bool { + return strings.HasSuffix(strings.ToLower(filename), ".pdf") +} + // IsImage is used to determine if a file is image func IsImage(filename string) bool { imageExts := []string{