Skip to content

Allow user to override Content-Type and Content-Disposition headers #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ MAX_IDLE_CONNECTIONS | Allowed number of idle connections to the S3 storage
IDLE_CONNECTION_TIMEOUT | Allowed timeout to the S3 storage. | | 10
DISABLE_COMPRESSION | If true will pass encoded content through as-is. | | true
INSECURE_TLS | If true it will skip cert checks | | false
CONTENT_TYPE | Override the default Content-Type response header | | -
CONTENT_DISPOSITION | Override the default Content-Disposition response header | | -

### 2. Run the application

Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type config struct { // nolint
DisableCompression bool // DISABLE_COMPRESSION
InsecureTLS bool // Disables TLS validation on request endpoints.
JwtSecretKey string // JWT_SECRET_KEY
ContentType string // Override default Content-Type
ContentDisposition string // Override default Content-Disposition
}

// Setup configurations with environment variables
Expand Down Expand Up @@ -128,6 +130,8 @@ func Setup() {
DisableCompression: disableCompression,
InsecureTLS: insecureTLS,
JwtSecretKey: os.Getenv("JWT_SECRET_KEY"),
ContentType: os.Getenv("CONTENT_TYPE"),
ContentDisposition: os.Getenv("CONTENT_DISPOSITION"),
}
// Proxy
log.Printf("[config] Proxy to %v", Config.S3Bucket)
Expand Down
6 changes: 6 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func defaultConfig() *config {
IdleConnTimeout: time.Duration(10) * time.Second,
DisableCompression: true,
InsecureTLS: false,
ContentType: "",
ContentDisposition: "",
}
}

Expand All @@ -56,6 +58,8 @@ func TestChangeDefaults(t *testing.T) {
os.Setenv("IDLE_CONNECTION_TIMEOUT", "60")
os.Setenv("DISABLE_COMPRESSION", "FALSE")
os.Setenv("INSECURE_TLS", "t")
os.Setenv("CONTENT_TYPE", "application/octet-stream")
os.Setenv("CONTENT_DISPOSITION", "attachment")

Setup()

Expand All @@ -69,6 +73,8 @@ func TestChangeDefaults(t *testing.T) {
expected.IdleConnTimeout = time.Duration(60) * time.Second
expected.DisableCompression = false
expected.InsecureTLS = true
expected.ContentType = "application/octet-stream"
expected.ContentDisposition = "attachment"

assert.Equal(t, expected, Config)
}
12 changes: 10 additions & 2 deletions internal/controllers/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func setHeadersFromAwsResponse(w http.ResponseWriter, obj *s3.GetObjectOutput, h
} else {
setStrHeader(w, "Expires", obj.Expires)
}
setStrHeader(w, "Content-Disposition", obj.ContentDisposition)
setStrHeader(w, "Content-Encoding", obj.ContentEncoding)
setStrHeader(w, "Content-Language", obj.ContentLanguage)

Expand All @@ -116,7 +115,16 @@ func setHeadersFromAwsResponse(w http.ResponseWriter, obj *s3.GetObjectOutput, h
setIntHeader(w, "Content-Length", obj.ContentLength)
}
setStrHeader(w, "Content-Range", obj.ContentRange)
setStrHeader(w, "Content-Type", obj.ContentType)
if config.Config.ContentType == "" {
setStrHeader(w, "Content-Type", obj.ContentType)
} else {
setStrHeader(w, "Content-Type", &config.Config.ContentType)
}
if config.Config.ContentDisposition == "" {
setStrHeader(w, "Content-Disposition", obj.ContentDisposition)
} else {
setStrHeader(w, "Content-Disposition", &config.Config.ContentDisposition)
}
setStrHeader(w, "ETag", obj.ETag)
setTimeHeader(w, "Last-Modified", obj.LastModified)

Expand Down