Permalink
Browse files

Set content-disposition headers for tar and zip.

cbugg: close bug-621
  • Loading branch information...
1 parent 3252606 commit a4a439649a0d27688ffbe6694c628c276b7b14cf @dustin dustin committed Jul 2, 2013
Showing with 20 additions and 0 deletions.
  1. +3 −0 tar.go
  2. +17 −0 zip.go
View
3 tar.go
@@ -3,6 +3,7 @@ package main
import (
"archive/tar"
"compress/gzip"
+ "fmt"
"log"
"net/http"
)
@@ -18,6 +19,8 @@ func doTarDocs(w http.ResponseWriter, req *http.Request,
go pathGenerator(path, ch, cherr, quit)
go logErrors("tar", cherr)
+ w.Header().Set("Content-Disposition",
+ fmt.Sprintf("attachment; filename=%q", archiveFilename(path, "tar")))
w.Header().Set("Content-Type", "application/x-tar")
if canGzip(req) {
View
17 zip.go
@@ -2,10 +2,25 @@ package main
import (
"archive/zip"
+ "fmt"
"log"
"net/http"
+ "strings"
)
+func archiveFilename(path, ext string) string {
+ parts := strings.Split(path, "/")
+ filename := "cbfs-archive." + ext
+ for len(parts) > 0 {
+ if p := parts[len(parts)-1]; p != "" {
+ filename = p + "." + ext
+ break
+ }
+ parts = parts[:len(parts)-1]
+ }
+ return filename
+}
+
func doZipDocs(w http.ResponseWriter, req *http.Request,
path string) {
@@ -17,6 +32,8 @@ func doZipDocs(w http.ResponseWriter, req *http.Request,
go pathGenerator(path, ch, cherr, quit)
go logErrors("zip", cherr)
+ w.Header().Set("Content-Disposition",
+ fmt.Sprintf("attachment; filename=%q", archiveFilename(path, "zip")))
w.Header().Set("Content-Type", "application/zip")
w.WriteHeader(200)

0 comments on commit a4a4396

Please sign in to comment.