refactor "content upload" out of needle creation

This commit is contained in:
Chris Lu 2013-08-06 11:23:24 -07:00
parent 54906c48f3
commit 952974491b

View file

@ -38,9 +38,7 @@ type Needle struct {
Padding []byte `comment:"Aligned to 8 bytes"`
}
func NewNeedle(r *http.Request) (n *Needle, e error) {
n = new(Needle)
func ParseUpload(r *http.Request) (fileName string, data []byte, mimeType string, isGzipped bool, modifiedTime uint64, e error) {
form, fe := r.MultipartReader()
if fe != nil {
log.Println("MultipartReader [ERROR]", fe)
@ -53,58 +51,70 @@ func NewNeedle(r *http.Request) (n *Needle, e error) {
e = fe
return
}
fname := part.FileName()
if fname != "" {
fname = path.Base(part.FileName())
fileName = part.FileName()
if fileName != "" {
fileName = path.Base(fileName)
} else {
e = errors.New("No file found!")
return
}
data, ioe := ioutil.ReadAll(part)
if ioe != nil {
e = ioe
log.Println("Reading Content [ERROR]", ioe)
data, e = ioutil.ReadAll(part)
if e != nil {
log.Println("Reading Content [ERROR]", e)
return
}
dotIndex := strings.LastIndex(fname, ".")
dotIndex := strings.LastIndex(fileName, ".")
ext, mtype := "", ""
if dotIndex > 0 {
ext = fname[dotIndex:]
ext = strings.ToLower(fileName[dotIndex:])
mtype = mime.TypeByExtension(ext)
}
contentType := part.Header.Get("Content-Type")
if contentType != "" && mtype != contentType && len(contentType) < 256 {
n.Mime = []byte(contentType)
n.SetHasMime()
if contentType != "" && mtype != contentType {
mimeType = contentType //only return mime type if not deductable
mtype = contentType
}
if part.Header.Get("Content-Encoding") == "gzip" {
n.SetGzipped()
isGzipped = true
} else if IsGzippable(ext, mtype) {
if data, e = GzipData(data); e != nil {
return
}
n.SetGzipped()
isGzipped = true
}
if ext == ".gz" {
isGzipped = true
}
if strings.HasSuffix(fileName, ".gz") {
fileName = fileName[:len(fileName)-3]
}
modifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
return
}
func NewNeedle(r *http.Request) (n *Needle, e error) {
fname, mimeType, isGzipped := "", "", false
n = new(Needle)
fname, n.Data, mimeType, isGzipped, n.LastModified, e = ParseUpload(r)
if e != nil {
return
}
if len(fname) < 256 {
n.Name = []byte(fname)
n.SetHasName()
}
if len(mimeType) < 256 {
n.Mime = []byte(mimeType)
n.SetHasMime()
}
if isGzipped {
n.SetGzipped()
}
if len(fname) < 256 {
if strings.HasSuffix(fname, ".gz") {
n.Name = []byte(fname[:len(fname)-3])
} else {
n.Name = []byte(fname)
}
n.SetHasName()
}
var parseError error
if n.LastModified, parseError = strconv.ParseUint(r.FormValue("ts"), 10, 64); parseError != nil {
if n.LastModified == 0 {
n.LastModified = uint64(time.Now().Unix())
n.SetHasLastModifiedDate()
}
n.SetHasLastModifiedDate()
n.Data = data
n.Checksum = NewCRC(data)
n.Checksum = NewCRC(n.Data)
commaSep := strings.LastIndex(r.URL.Path, ",")
dotSep := strings.LastIndex(r.URL.Path, ".")