Use http.Error() when appropriate
git-svn-id: file:///srv/svn/repo/marisa/trunk@22 d6811dac-2434-b64a-9ddc-f563ab233461
This commit is contained in:
17
partage.go
17
partage.go
@@ -41,7 +41,6 @@ var conf struct {
|
|||||||
expiry int64
|
expiry int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func writefile(f *os.File, s io.ReadCloser, contentlength int64) error {
|
func writefile(f *os.File, s io.ReadCloser, contentlength int64) error {
|
||||||
buffer := make([]byte, 4096)
|
buffer := make([]byte, 4096)
|
||||||
eof := false
|
eof := false
|
||||||
@@ -106,7 +105,7 @@ func writemeta(filename string, expiry int64) error {
|
|||||||
func servetemplate(w http.ResponseWriter, f string, d templatedata) {
|
func servetemplate(w http.ResponseWriter, f string, d templatedata) {
|
||||||
t, err := template.ParseFiles(conf.templatedir + "/" + f)
|
t, err := template.ParseFiles(conf.templatedir + "/" + f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
http.Error(w, err, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,8 +118,7 @@ func servetemplate(w http.ResponseWriter, f string, d templatedata) {
|
|||||||
func uploaderPut(w http.ResponseWriter, r *http.Request) {
|
func uploaderPut(w http.ResponseWriter, r *http.Request) {
|
||||||
/* limit upload size */
|
/* limit upload size */
|
||||||
if r.ContentLength > conf.maxsize {
|
if r.ContentLength > conf.maxsize {
|
||||||
w.WriteHeader(http.StatusRequestEntityTooLarge)
|
http.Error(w, "File is too big", http.StatusRequestEntityTooLarge)
|
||||||
w.Write([]byte("File is too big"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(r.URL.Path))
|
tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(r.URL.Path))
|
||||||
@@ -132,7 +130,7 @@ func uploaderPut(w http.ResponseWriter, r *http.Request) {
|
|||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
if err = writefile(f, r.Body, r.ContentLength); err != nil {
|
if err = writefile(f, r.Body, r.ContentLength); err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
http.Error(w, err, http.StatusInternalServerError)
|
||||||
defer os.Remove(tmp.Name())
|
defer os.Remove(tmp.Name())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -149,14 +147,13 @@ func uploaderPost(w http.ResponseWriter, r *http.Request) {
|
|||||||
links := []string{}
|
links := []string{}
|
||||||
for _, h := range r.MultipartForm.File["uck"] {
|
for _, h := range r.MultipartForm.File["uck"] {
|
||||||
if h.Size > conf.maxsize {
|
if h.Size > conf.maxsize {
|
||||||
w.WriteHeader(http.StatusRequestEntityTooLarge)
|
http.Error(w, "File is too big", http.StatusRequestEntityTooLarge)
|
||||||
w.Write([]byte("File is too big"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
post, err := h.Open()
|
post, err := h.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
http.Error(w, err, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer post.Close()
|
defer post.Close()
|
||||||
@@ -164,13 +161,13 @@ func uploaderPost(w http.ResponseWriter, r *http.Request) {
|
|||||||
tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(h.Filename))
|
tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(h.Filename))
|
||||||
f, err := os.Create(tmp.Name())
|
f, err := os.Create(tmp.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
http.Error(w, err, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
if err = writefile(f, post, h.Size); err != nil {
|
if err = writefile(f, post, h.Size); err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
http.Error(w, err, http.StatusInternalServerError)
|
||||||
defer os.Remove(tmp.Name())
|
defer os.Remove(tmp.Name())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user