perkeep/pkg/server/uploadhelper.go

74 lines
1.7 KiB
Go
Raw Normal View History

2011-06-09 22:00:20 +00:00
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server
2011-06-09 22:00:20 +00:00
import (
"io"
"net/http"
2011-06-09 22:00:20 +00:00
"camlistore.org/pkg/httputil"
"camlistore.org/pkg/schema"
2011-06-09 22:00:20 +00:00
)
func (ui *UIHandler) serveUploadHelper(rw http.ResponseWriter, req *http.Request) {
ret := make(map[string]interface{})
defer httputil.ReturnJSON(rw, ret)
2011-06-09 22:00:20 +00:00
if ui.root.Storage == nil {
2011-06-09 22:00:20 +00:00
ret["error"] = "No BlobRoot configured"
ret["errorType"] = "server"
return
}
mr, err := req.MultipartReader()
if err != nil {
ret["error"] = "reading body: " + err.Error()
ret["errorType"] = "server"
2011-06-09 22:00:20 +00:00
return
}
got := make([]map[string]interface{}, 0)
for {
part, err := mr.NextPart()
if err == io.EOF {
2011-06-09 22:00:20 +00:00
break
}
if err != nil {
ret["error"] = "reading body: " + err.Error()
2011-06-09 22:00:20 +00:00
ret["errorType"] = "server"
break
}
fileName := part.FileName()
if fileName == "" {
continue
}
br, err := schema.WriteFileFromReader(ui.root.Storage, fileName, part)
2011-06-09 22:00:20 +00:00
if err == nil {
got = append(got, map[string]interface{}{
"filename": part.FileName(),
"formname": part.FormName(),
"fileref": br.String(),
2011-06-09 22:00:20 +00:00
})
} else {
ret["error"] = "writing to blobserver: " + err.Error()
2011-06-09 22:00:20 +00:00
return
}
}
ret["got"] = got
}