1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| r.Body = http.MaxBytesReader(w, r.Body, 32<<20+1024) reader, err := r.MultipartReader() if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return }
text := make([]byte, 512) p, err := reader.NextPart()
if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if p.FormName() != "text_field" { http.Error(w, "text_field is expected", http.StatusBadRequest) return } _, err = p.Read(text) if err != nil && err != io.EOF { http.Error(w, err.Error(), http.StatusInternalServerError) return }
p, err = reader.NextPart() if err != nil && err != io.EOF { http.Error(w, err.Error(), http.StatusInternalServerError) return } if p.FormName() != "file_field" { http.Error(w, "file_field is expected", http.StatusBadRequest) return } buf := bufio.NewReader(p) sniff, _ := buf.Peek(512) contentType := http.DetectContentType(sniff) if contentType != "application/zip" { http.Error(w, "file type not allowed", http.StatusBadRequest) return } f, err := ioutil.TempFile("", "") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } defer f.Close() var maxSize int64 = 32 << 20 lmt := io.MultiReader(buf, io.LimitReader(p, maxSize - 511)) written, err := io.Copy(f, lmt) if err != nil && err != io.EOF { http.Error(w, err.Error(), http.StatusInternalServerError) return } if written > maxSize { os.Remove(f.Name()) http.Error(w, "file size over limit", http.StatusBadRequest) return }
|