add store metadata:file name, mime type, is_gzipped

add support to upload .gz file directly
This commit is contained in:
Chris Lu 2012-12-22 02:10:45 -08:00
parent 37afb31d05
commit 9b95430e9f
4 changed files with 56 additions and 10 deletions

View file

@ -129,10 +129,28 @@ func GetHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return
} }
if n.NameSize > 0 {
fname := string(n.Name)
dotIndex := strings.LastIndex(fname, ".")
if dotIndex > 0 {
ext = fname[dotIndex:]
}
}
mtype := ""
if ext != "" { if ext != "" {
mtype := mime.TypeByExtension(ext) mtype = mime.TypeByExtension(ext)
}
if n.MimeSize > 0 {
mtype = string(n.Mime)
}
if mtype != "" {
w.Header().Set("Content-Type", mtype) w.Header().Set("Content-Type", mtype)
if storage.IsGzippable(ext, mtype) { }
if n.NameSize > 0 {
w.Header().Set("Content-Disposition", "filename="+string(n.Name))
}
if ext != ".gz" {
if n.IsGzipped() {
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Encoding", "gzip")
} else { } else {
@ -140,6 +158,7 @@ func GetHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
} }
w.Header().Set("Content-Length", strconv.Itoa(len(n.Data)))
w.Write(n.Data) w.Write(n.Data)
} }
func PostHandler(w http.ResponseWriter, r *http.Request) { func PostHandler(w http.ResponseWriter, r *http.Request) {

View file

@ -15,6 +15,9 @@ func IsGzippable(ext, mtype string) bool {
if ext == ".rar" { if ext == ".rar" {
return false return false
} }
if ext == ".gz" {
return false
}
if strings.Index(mtype,"text/")==0 { if strings.Index(mtype,"text/")==0 {
return true return true
} }

View file

@ -50,15 +50,32 @@ func NewNeedle(r *http.Request) (n *Needle, fname string, e error) {
} }
fname = part.FileName() fname = part.FileName()
data, _ := ioutil.ReadAll(part) data, _ := ioutil.ReadAll(part)
//log.Println("uploading file " + part.FileName())
dotIndex := strings.LastIndex(fname, ".") dotIndex := strings.LastIndex(fname, ".")
ext, mtype := "", ""
if dotIndex > 0 { if dotIndex > 0 {
ext := fname[dotIndex:] ext = fname[dotIndex:]
mtype := mime.TypeByExtension(ext) mtype = mime.TypeByExtension(ext)
}
contentType := part.Header.Get("Content-Type")
if contentType != "" && mtype != contentType && len(contentType) < 256 {
n.Mime = []byte(contentType)
mtype = contentType
}
if IsGzippable(ext, mtype) { if IsGzippable(ext, mtype) {
data = GzipData(data) data = GzipData(data)
n.SetGzipped()
}
if ext == ".gz" {
n.SetGzipped()
}
if len(fname) < 256 {
if strings.HasSuffix(fname, ".gz") {
n.Name = []byte(fname[:len(fname)-3])
} else {
n.Name = []byte(fname)
} }
} }
n.Data = data n.Data = data
n.Checksum = NewCRC(data) n.Checksum = NewCRC(data)

View file

@ -142,3 +142,10 @@ func (n *Needle) ReadNeedleBody(r *os.File, version Version, bodyLength uint32)
} }
return return
} }
func (n *Needle) IsGzipped() bool{
return n.Flags & 0x01 == 0x01
}
func (n *Needle) SetGzipped(){
n.Flags = n.Flags | 0x01
}