Skip to content

Commit

Permalink
feat: add pdf catch-control config (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreys-cat authored Feb 18, 2023
1 parent c9ed0ec commit b9cc586
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}


8 changes: 5 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions operation/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file not shown.
5 changes: 5 additions & 0 deletions utils/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit b9cc586

Please sign in to comment.