Golang Request.MultipartReader Examples

Golang Request.MultipartReader - 30 examples found. These are the top rated real world Golang examples of net/http.Request.MultipartReader extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func (r *Resource) POST(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	reader, err := req.MultipartReader()
	if err != nil {
		return ctx.BadRequest(rw, c.T("media.uploadresource.could_not_upload_file"))
	}
	var tempFile *os.File
	for {
		part, err := reader.NextPart()
		if err == io.EOF {
			break
		}

		tempFile, err = ioutil.TempFile(os.TempDir(), "spa")
		if err != nil {
			return ctx.InternalServerError(rw, c.T("media.uploadresource.could_not_create_temp_file"))
		}
		defer tempFile.Close()

		_, err = io.Copy(tempFile, part)
		if err != nil {
			break
		}
	}
	return ctx.Created(rw, tempFile.Name())
}
Example #2
0
func Upload(res http.ResponseWriter, req *http.Request) {
	reader, err := req.MultipartReader()
	if err != nil {
		log.Fatal(err)
	}
	var processed []*rsp
	for {
		part, err := reader.NextPart()
		if err == io.EOF {
			break
		} else if err != nil {
			break
		}
		defer part.Close()
		if part.FileName() == "" {
			continue
		}
		key := strconv.FormatInt(time.Now().Unix(), 10)
		d, err := os.Create("tmp/" + key + "-" + part.FileName())
		if err != nil {
			log.Fatal(err)
		}
		defer d.Close()
		io.Copy(d, part)
		processed = append(processed, &rsp{part.FileName(), d.Name()})
	}
	response, err := json.Marshal(processed)
	if err != nil {
		log.Fatal(err)
	}
	res.Write(response)
}
Example #3
0
func UploadHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id := vars["id"]

	if _, exists := transfers[id]; exists {
		http.Error(w, "internal error", http.StatusBadRequest)
		return
	}

	mr, err := r.MultipartReader()
	if err != nil {
		http.Error(w, "internal error", http.StatusBadRequest)
		return
	}

	transfer := &Transfer{
		mr,
		WAIT,
	}
	transfers[id] = transfer

	timeout := time.After(time.Minute * time.Duration(conf.TimeoutMinutes))
	for transfer.Status == WAIT {
		select {
		case <-timeout:
			http.Error(w, "no receiver found", http.StatusBadRequest)
			transfer.Status = TIMEOUT
		}
	}
	w.Write([]byte("ok"))
}
Example #4
0
func NewNeedle(r *http.Request) (n *Needle, e error) {

	n = new(Needle)
	form, fe := r.MultipartReader()
	if fe != nil {
		log.Error("MultipartReader [ERROR] %s\n", fe)
		e = fe
		return
	}
	part, _ := form.NextPart()
	//log.Println("uploading file " + part.FileName())
	data, _ := ioutil.ReadAll(part)
	n.Data = data

	commaSep := strings.LastIndex(r.URL.Path, ",")
	dotSep := strings.LastIndex(r.URL.Path, ".")
	fid := r.URL.Path[commaSep+1:]
	if dotSep > 0 {
		fid = r.URL.Path[commaSep+1 : dotSep]
	}

	n.ParsePath(fid)

	return
}
Example #5
0
func handle(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		http.ServeFile(w, r, filepath.Join(static, "submit.html"))
		return
	}

	if r.Method == "POST" {
		files, err := r.MultipartReader()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		decks := make([]*Deck, 0)
		for {
			file, err := files.NextPart()
			if err == io.EOF {
				break
			}
			if file.FileName() == "" {
				continue
			}
			deck, err := NewDeck(file)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				fmt.Printf("Deck parse error: %s\n", err.Error())
				return
			}
			decks = append(decks, deck)
		}
		respond(w, decks)
	}
}
Example #6
0
func parseForm(w http.ResponseWriter, r *http.Request) *Upload {
	var upload Upload

	mp_rdr, err := r.MultipartReader()
	if err != nil {
		serverError(w, "error reading multipart: "+err.Error())
		return nil
	}

	for {
		part, err := mp_rdr.NextPart()

		if err == io.EOF {
			break
		}

		switch part.FormName() {
		case "file":
			upload.Data = readPart(part)
		case "filename":
			upload.Name = string(readPart(part))
		case "password":
			upload.Password = string(readPart(part))
		case "mode":
			upload.Mode = string(readPart(part))
		default:
			serverError(w, "invalid form part: "+part.FormName())
			return nil
		}
	}

	return &upload
}