Ejemplo n.º 1
1
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())
}
Ejemplo n.º 2
1
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)
}
Ejemplo n.º 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"))
}
Ejemplo n.º 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
}
Ejemplo n.º 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)
	}
}
Ejemplo n.º 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
}
Ejemplo n.º 7
0
func NewNeedle(r *http.Request) (n *Needle, fname string, e error) {

	n = new(Needle)
	form, fe := r.MultipartReader()
	if fe != nil {
		fmt.Println("MultipartReader [ERROR]", fe)
		e = fe
		return
	}
	part, _ := form.NextPart()
	fname = part.FileName()
	data, _ := ioutil.ReadAll(part)
	//log.Println("uploading file " + part.FileName())
	dotIndex := strings.LastIndex(fname, ".")
	if dotIndex > 0 {
		ext := fname[dotIndex:]
		mtype := mime.TypeByExtension(ext)
		if IsGzippable(ext, mtype) {
			data = GzipData(data)
		}
	}
	n.Data = data
	n.Checksum = NewCRC(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
}
Ejemplo n.º 8
0
func (i *webInterface) getUploadReader(r *http.Request) (io.ReadCloser, error) {

	mpr, err := r.MultipartReader()
	if err == http.ErrNotMultipart {
		// Not multipart, read raw body data
		return r.Body, nil
	}
	if err != nil {
		return nil, err
	}

	for {
		// Get next part of the upload
		part, err := mpr.NextPart()
		if err == io.EOF {
			return nil, errNoData
		}
		if err != nil {
			return nil, err
		}

		// Search for first file input
		fn := part.FileName()
		if fn != "" {
			return &partReader{
				p: part,
				b: r.Body,
			}, nil
		}
	}
}
Ejemplo n.º 9
0
// You should probably verify the content type is what you expect
// and at least basic validation of the file's contents. This,
// however, is the minimum required to upload a file to the server.
func getForm(w http.ResponseWriter, r *http.Request) {
	mp_rdr, err := r.MultipartReader()
	if err != nil {
		showPage(Page{false, nil}, w, r)
		return
	}
	var page Page
	var frm FormData
	for {
		part, err := mp_rdr.NextPart()
		if err == io.EOF {
			break
		}
		if part.FormName() == "caption" {
			br := new(bytes.Buffer)
			_, err := io.Copy(br, part)
			if err != nil {
				break
			}
			frm.Caption = string(br.Bytes())
		} else if part.FormName() == "image" {
			fileName := saveTempImage(part)
			if fileName == "" {
				break
			}
			frm.ImageFile = fileName
		}
	}
	if frm.ImageFile != "" && frm.Caption != "" {
		page.Processed = true
	}
	page.Image = &frm
	showPage(page, w, r)
}
Ejemplo n.º 10
0
func MultipartRequestReader(r *http.Request) (io.Reader, string, error) {
	reader, err := r.MultipartReader()

	if err != nil {
		err = fmt.Errorf("MultipartRequestReader multipart reader error: %s", err)
		return nil, "", err
	}

	p, err := reader.NextPart()

	if err != nil {
		err = fmt.Errorf("MultipartRequestReader NextPart error: %s", err)
		return nil, "", err
	}

	name := p.FormName()

	if name == "" {
		return nil, "", fmt.Errorf("MultipartRequestReader missing field name")
	}

	filename := p.FileName()

	if filename == "" {
		return nil, "", fmt.Errorf("MultipartRequestReader part is not a file")
	}

	return p, filename, nil
}
Ejemplo n.º 11
0
// upload multiple images
func HandleMultipleImagesUpload(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
	if req.ContentLength > helper.Config.MaxSize {
		http.Error(res, "file too large", http.StatusRequestEntityTooLarge)
		return
	}

	reader, err := req.MultipartReader()
	if err != nil {
		helper.WriteErrorResponse(res, err)
		return
	}
	var imgs []models.ImageInfo
	for {
		part, err := reader.NextPart()
		if err == io.EOF {
			break
		}
		if part.FileName() == "" {
			continue
		}
		info, err := handleSaveSingleImage(part)
		imgs = append(imgs, info)
	}
	helper.WriteResponse(res, imgs)
}
Ejemplo n.º 12
0
func upload(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		uploadTemplate.Execute(w, nil)
	} else {
		part_reader, err := r.MultipartReader()
		if err != nil {
			log.Println("get file:", err)
			w.WriteHeader(http.StatusInternalServerError)
		}
		log.Println("start copy")
		var file_part *multipart.Part
		for {
			if file_part, err = part_reader.NextPart(); err != nil {
				if err == io.EOF {
					err = nil
				}
				break
			}
			if file_part.FormName() == "file" {
				if err = write_file(file_part); err != nil {
					break
				}
			}
			file_part.Close()
		}
		if err != nil {
			log.Println("write file:", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		http.Redirect(w, r, "/upload", 302)
	}
}
Ejemplo n.º 13
0
func LoadTasksFromJson(r *http.Request, tasks *[]Task) (err error) {
	logger.Debug("LoadTasksFromJson(%v)", r.URL.Path)

	mpreader, err := r.MultipartReader()
	if err != nil {
		logger.Warn(err)
		return
	}

	frm, err := mpreader.ReadForm(10000)
	if err != nil {
		logger.Warn(err)
		return
	}

	jsonfile, err := frm.File["jsonfile"][0].Open()
	if err != nil {
		logger.Warn(err)
		return
	}
	defer jsonfile.Close()

	err = json.NewDecoder(jsonfile).Decode(&tasks)
	if err != nil {
		logger.Warn(err)
	}
	return
}
Ejemplo n.º 14
0
// MultipartReader returns a MIME multipart reader if this is a
// multipart/form-data POST request, else returns nil and an error.
// Use this function instead of ParseMultipartForm to
// process the request body as a stream.
func MultipartReader(r *http.Request) (*multipart.Reader, error) {
	_, err := r.MultipartReader()
	if err != nil {
		return nil, err
	}
	return multipartReader(r)
}
Ejemplo n.º 15
0
//This is where the action happens.
func uploadHandler(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	//GET displays the upload form.
	case "GET":
		httplog.HttpLogln(r)
		display(w, "upload", nil)

	//POST takes the uploaded file(s) and saves it to disk.
	case "POST":
		//get the multipart reader for the request.
		httplog.HttpLogln(r)
		reader, err := r.MultipartReader()
		//		r.ParseMultipartForm(0)

		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		//copy each part to destination.
		for {
			part, err := reader.NextPart()
			if err == io.EOF {
				break
			}

			if part.FormName() == "path" {
				j, err := ioutil.ReadAll(part)
				if err != nil {
					http.Error(w, err.Error(), http.StatusInternalServerError) //do something
					return
				}
				log.Println(string(j))
				//log.Println(part)
			}

			//if part.FileName() is empty, skip this iteration.
			if part.FileName() == "" {
				continue
			}
			dst, err := os.Create("uploaded/" + part.FileName())
			defer dst.Close()

			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}

			if _, err := io.Copy(dst, part); err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
		}
		//display success message.
		display(w, "upload", "Upload successful.")
	default:
		w.WriteHeader(http.StatusMethodNotAllowed)
	}
}
Ejemplo n.º 16
0
func (a *UpdateAction) Execute(writer http.ResponseWriter, request *http.Request, parameters map[string]string) (int, string) {
	multipartReader, err := request.MultipartReader()

	/*
	 * Bad request 'cause it's not a multi part reader.
	 */
	if err != nil || multipartReader == nil {
		return 400, "Not a multipart reader."
	}

	/*
	 * Reads the content of the request.
	 */
	form, err := multipartReader.ReadForm(500)
	if err != nil {
		return 500, "unable to parse the content."
	}

	/*
	 * Checks that an image is attached.
	 */
	if len(form.File["image"]) == 0 {
		return 400, "No image provided in the request."
	}
	/*
	 * Reads the file name in parameter.
	 */
	filename := form.Value["filename"][0]
	if len(filename) == 0 {
		return 400, "No filename parameter provided."
	}

	if strings.Contains(filename, "/") {
		return 400, "The filename can't contain the '/' char."
	}

	/*
	 * Only one image.
	 */
	file, err := form.File["image"][0].Open()
	if err != nil {
		fmt.Println(err)
		return 500, "Unable to read the file received."
	}

	/*
	 * Reads the file created by Go.
	 */
	data, err := ioutil.ReadAll(file)
	if err != nil {
		fmt.Println(err)
		return 500, "Unable to read the file received."
	}

	// TODO Saves the image onto the disk.
	ioutil.WriteFile(a.Application.GetDirectory()+filename+".gif", data, 0666)

	return 200, "ok"
}
Ejemplo n.º 17
0
/**
  Demonstrate efficient uploading in the face of any
  crazy request we get.  We can use heuristics such as
  the names of parts to DECIDE whether it's reasonable to
  put the data into memory (json metadata), or to create a
  file handle to drain it off, or to start off in memory
  and then drain it off somewhere if it becomes unreasonably
  large (may be useful for being optimally efficient).
  This is the key to scalability, because we have
  full control over handling HTTP.

  If we have an SLA to handle a certain number of connections,
  putting an upper bound on memory usage per session lets us
  have such a guarantee, where we can use admission control (TBD)
  to limit the number of sessions to amounts within the SLA
  to ensure that sessions started can complete without interference
  from sessions that are doomed to fail from congestion.
*/
func (h uploader) serveHTTPUploadPOST(w http.ResponseWriter, r *http.Request) {
	startTime := time.Now()
	log.Print("handling an upload post")
	multipartReader, err := r.MultipartReader()

	if err != nil {
		log.Printf("failed to get a multipart reader %v", err)
		http.Error(w, "failed to get a multipart reader", 500)
		return
	}

	isAuthorized := false
	partBytes := int64(0)
	partCount := int64(0)
	for {
		//DOS problem .... what if this header is very large?  (Intentionally)
		part, partErr := multipartReader.NextPart()
		if partErr != nil {
			if partErr == io.EOF {
				break //just an eof...not an error
			} else {
				log.Printf("error getting a part %v", partErr)
				http.Error(w, "error getting a part", 500)
				return
			}
		} else {
			if strings.Compare(part.FormName(), "uploadCookie") == 0 {
				if h.checkUploadCookie(part) {
					isAuthorized = true
				}
			} else {
				if len(part.FileName()) > 0 {
					if isAuthorized {
						fileName := h.HomeBucket + "/" + part.FileName()
						//Could take an *indefinite* amount of time!!
						partBytesIncr, partCountIncr := h.serveHTTPUploadPOSTDrain(fileName, w, part)
						partBytes += partBytesIncr
						partCount += partCountIncr
					} else {
						log.Printf("failed authorization for file")
						http.Error(w, "failed authorization for file", 400)
						return
					}
				}
			}
		}
	}
	h.serveHTTPUploadGETMsg("ok", w, r)
	stopTime := time.Now()
	timeDiff := (stopTime.UnixNano()-startTime.UnixNano())/(1000*1000) + 1
	throughput := (1000 * partBytes) / timeDiff
	partSize := int64(0)
	if partCount <= 0 {
		partSize = 0
	} else {
		partSize = partBytes / partCount
	}
	log.Printf("Upload: time = %dms, size = %d B, throughput = %d B/s, partSize = %d B", timeDiff, partBytes, throughput, partSize)
}
Ejemplo n.º 18
0
// Just dump all the context
//
func DebugHttpHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")

	fmt.Fprintf(w, "Method: %s\n", r.Method)
	fmt.Fprintf(w, "Host: %s\n", r.Host)
	fmt.Fprintf(w, "RemoteAddr: %s\n", r.RemoteAddr)
	fmt.Fprint(w, "Header:\n")
	for k, v := range r.Header {
		fmt.Fprintf(w, "  %s: %s\n", k, strings.Join(v, ","))
	}

	fmt.Fprintf(w, "URL: %s\n", r.URL)
	fmt.Fprintf(w, "URL.Path: %s\n", r.URL.Path)
	fmt.Fprintf(w, "URL.Query():\n")
	for k, v := range r.URL.Query() {
		for _, v2 := range v {
			fmt.Fprintf(w, "  %s: %s\n", k, v2)
		}
	}
	fmt.Fprintf(w, "ContentLength: %d\n", r.ContentLength)

	ctype := r.Header.Get("Content-Type")

	if strings.HasPrefix(ctype, "multipart/form-data") {
		fmt.Fprint(w, "Form (multipart):\n")
		reader, err := r.MultipartReader()
		if err == nil {
			for {
				part, err := reader.NextPart()
				if err != nil {
					break
				}
				fmt.Fprintf(w, "  FormName: %s\n", part.FormName())
				fmt.Fprintf(w, "  FileName: %s\n", part.FileName())
				fmt.Fprintf(w, "  Header:\n")
				for k, v := range part.Header {
					for _, v2 := range v {
						fmt.Fprintf(w, "    %s: %s\n", k, v2)
					}
				}
				data, err := ioutil.ReadAll(part)
				if err == nil {
					fmt.Fprintf(w, "  Size: %d bytes\n", len(data))
				}
				part.Close()
			}
		} else {
			fmt.Fprintf(w, "ERROR %v\n", err)
		}
	} else if strings.HasPrefix(ctype, "application/x-www-form-urlencoded") {
		r.ParseForm()
		fmt.Fprint(w, "Form (urlencoded):\n")
		for k, v := range r.Form {
			for _, v2 := range v {
				fmt.Fprintf(w, "  %s: %s", k, v2)
			}
		}
	}
}
Ejemplo n.º 19
0
func UploadHandler(w http.ResponseWriter, r *http.Request) {
	switch r.Method {

	case "GET":
		renderUpload(w, "")

	case "POST":

		reader, err := r.MultipartReader()
		if err != nil {

			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		h := md5.New()
		const layout = "2006-01-02 15:04:05"
		t := time.Now()

		hashedTime = t.Format(layout)

		hashedTime = hex.EncodeToString(h.Sum([]byte(hashedTime)))

		os.Mkdir("data/upload/"+hashedTime+"/", 0700)

		for {
			part, err := reader.NextPart()
			if err == io.EOF {
				break
			}

			if part.FileName() == "" {
				continue
			}
			m := validType.FindStringSubmatch(part.FileName())
			if m == nil {

				continue

			}

			dst, err := os.Create("data/upload/" + hashedTime + "/" + part.FileName())
			defer dst.Close()

			if err != nil {

				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}

			if _, err := io.Copy(dst, part); err != nil {

				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
		}
		http.Redirect(w, r, "/data/upload/"+hashedTime, http.StatusFound)
	}
}
Ejemplo n.º 20
0
func atomicUpdateHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
	gun := vars["imageName"]
	s := ctx.Value("metaStore")
	store, ok := s.(storage.MetaStore)
	if !ok {
		return errors.ErrNoStorage.WithDetail(nil)
	}
	cryptoServiceVal := ctx.Value("cryptoService")
	cryptoService, ok := cryptoServiceVal.(signed.CryptoService)
	if !ok {
		return errors.ErrNoCryptoService.WithDetail(nil)
	}

	reader, err := r.MultipartReader()
	if err != nil {
		return errors.ErrMalformedUpload.WithDetail(nil)
	}
	var updates []storage.MetaUpdate
	for {
		part, err := reader.NextPart()
		if err == io.EOF {
			break
		}
		role := strings.TrimSuffix(part.FileName(), ".json")
		if role == "" {
			return errors.ErrNoFilename.WithDetail(nil)
		} else if !data.ValidRole(role) {
			return errors.ErrInvalidRole.WithDetail(role)
		}
		meta := &data.SignedMeta{}
		var input []byte
		inBuf := bytes.NewBuffer(input)
		dec := json.NewDecoder(io.TeeReader(part, inBuf))
		err = dec.Decode(meta)
		if err != nil {
			return errors.ErrMalformedJSON.WithDetail(nil)
		}
		version := meta.Signed.Version
		updates = append(updates, storage.MetaUpdate{
			Role:    role,
			Version: version,
			Data:    inBuf.Bytes(),
		})
	}
	updates, err = validateUpdate(cryptoService, gun, updates, store)
	if err != nil {
		serializable, serializableError := validation.NewSerializableError(err)
		if serializableError != nil {
			return errors.ErrInvalidUpdate.WithDetail(nil)
		}
		return errors.ErrInvalidUpdate.WithDetail(serializable)
	}
	err = store.UpdateMany(gun, updates)
	if err != nil {
		return errors.ErrUpdating.WithDetail(nil)
	}
	return nil
}
Ejemplo n.º 21
0
// API function to upload images
func apiUploadHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
	userName := authentication.GetUserName(r)
	if userName != "" {
		// Create multipart reader
		reader, err := r.MultipartReader()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		// Slice to hold all paths to the files
		allFilePaths := make([]string, 0)
		// Copy each part to destination.
		for {
			part, err := reader.NextPart()
			if err == io.EOF {
				break
			}
			// If part.FileName() is empty, skip this iteration.
			if part.FileName() == "" {
				continue
			}
			// Folder structure: year/month/randomname
			filePath := filepath.Join(filenames.ImagesFilepath, time.Now().Format("2006"), time.Now().Format("01"))
			if os.MkdirAll(filePath, 0777) != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			dst, err := os.Create(filepath.Join(filePath, strconv.FormatInt(time.Now().Unix(), 10)+"_"+uuid.Formatter(uuid.NewV4(), uuid.Clean)+filepath.Ext(part.FileName())))
			defer dst.Close()
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			if _, err := io.Copy(dst, part); err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			// Rewrite to file path on server
			filePath = strings.Replace(dst.Name(), filenames.ImagesFilepath, "/images", 1)
			// Make sure to always use "/" as path separator (to make a valid url that we can use on the blog)
			filePath = filepath.ToSlash(filePath)
			allFilePaths = append(allFilePaths, filePath)
		}
		json, err := json.Marshal(allFilePaths)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		w.Header().Set("Content-Type", "application/json")
		w.Write(json)
		return
	} else {
		http.Error(w, "Not logged in!", http.StatusInternalServerError)
		return
	}
}
Ejemplo n.º 22
0
// helper function for create & update
func ParseMultipartForm(r *http.Request) (params map[string]string, files ds.FormFiles, err error) {
	params = make(map[string]string)
	files = make(ds.FormFiles)
	md5h := md5.New()
	sha1h := sha1.New()
	reader, err := r.MultipartReader()
	if err != nil {
		return
	}
	for {
		part, err := reader.NextPart()
		if err != nil {
			break
		}
		if part.FileName() == "" {
			buffer := make([]byte, 32*1024)
			n, err := part.Read(buffer)
			if n == 0 || err != nil {
				break
			}
			params[part.FormName()] = fmt.Sprintf("%s", buffer[0:n])
		} else {
			tmpPath := fmt.Sprintf("%s/temp/%d%d", *conf.DATAROOT, rand.Int(), rand.Int())
			files[part.FormName()] = ds.FormFile{Name: part.FileName(), Path: tmpPath, Checksum: make(map[string]string)}
			tmpFile, err := os.Create(tmpPath)
			if err != nil {
				break
			}
			buffer := make([]byte, 32*1024)
			for {
				n, err := part.Read(buffer)
				if n == 0 || err != nil {
					break
				}
				tmpFile.Write(buffer[0:n])
				md5h.Write(buffer[0:n])
				sha1h.Write(buffer[0:n])
			}

			var md5s, sha1s []byte
			md5s = md5h.Sum(md5s)
			sha1s = sha1h.Sum(sha1s)
			files[part.FormName()].Checksum["md5"] = fmt.Sprintf("%x", md5s)
			files[part.FormName()].Checksum["sha1"] = fmt.Sprintf("%x", sha1s)

			tmpFile.Close()
			md5h.Reset()
			sha1h.Reset()
		}
	}
	if err != nil {
		return
	}
	return
}
Ejemplo n.º 23
0
func handleUpload(registry kit.Registry, tmpPath string, r *http.Request) ([]string, apperror.Error) {
	reader, err := r.MultipartReader()
	if err != nil {
		return nil, apperror.Wrap(err, "multipart_error")
	}

	files := make([]string, 0)

	for {
		part, err := reader.NextPart()
		if err != nil {
			if err == io.EOF {
				break
			} else {
				return nil, apperror.Wrap(err, "read_error")
			}
		}

		filename := part.FileName()
		if filename == "" {
			// Not a file?
			continue
		}

		id := utils.UUIdv4()
		path := tmpPath + string(os.PathSeparator) + id

		if err := os.MkdirAll(path, 0777); err != nil {
			return nil, apperror.Wrap(err, "create_dir_failed")
		}

		filename = utils.Canonicalize(filename)
		if filename == "" {
			filename = id
		}

		filePath := path + string(os.PathSeparator) + filename

		file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
		if err != nil {
			return nil, apperror.Wrap(err, "file_create_failed")
		}
		defer file.Close()

		_, err = io.Copy(file, part)
		if err != nil {
			return nil, apperror.Wrap(err, "file_copy_failed")
		}

		files = append(files, id+string(os.PathSeparator)+filename)
	}

	return files, nil
}
Ejemplo n.º 24
0
func uploadProgress(w http.ResponseWriter, r *http.Request) {

	mr, err := r.MultipartReader()
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}
	length := r.ContentLength

	//ticker := time.Tick(time.Millisecond) // <-- use this in production
	ticker := time.Tick(time.Second) // this is for demo purpose with longer delay

	for {

		var read int64
		var p float32
		part, err := mr.NextPart()

		if err == io.EOF {
			fmt.Printf("\nDone!")
			break
		}

		dst, err := os.OpenFile("upload.jpg", os.O_WRONLY|os.O_CREATE, 0666)

		if err != nil {
			return
		}

		for {

			buffer := make([]byte, 100000)
			cBytes, err := part.Read(buffer)
			if err == io.EOF {
				fmt.Printf("\nLast buffer read!")
				break
			}
			read = read + int64(cBytes)

			//fmt.Printf("\r read: %v  length : %v \n", read, length)

			if read > 0 {
				p = float32(read*100) / float32(length)
				//fmt.Printf("progress: %v \n", p)
				<-ticker
				fmt.Printf("\rUploading progress %v", p) // for console
				dst.Write(buffer[0:cBytes])
			} else {
				break
			}

		}
	}
}
Ejemplo n.º 25
0
func readUploadData(outpath string, r *http.Request) (string, int, error) {
	total := 0             // total bytes
	maxbytes := 1024 * 300 // hard limit of 300kb just for test
	fname := ""            // upload file name from header

	// create read buffer
	var bsize int64 = 16 * 1024 // 16 kb
	buffer := make([]byte, bsize)

	// get stream
	mr, err := r.MultipartReader()
	if err != nil {
		return fname, total, err
	}
	in_f, err := mr.NextPart()
	if err != nil {
		return fname, total, err
	}
	defer in_f.Close()

	fname = in_f.FileName()

	// create output file
	out_f, err := os.OpenFile(outpath, os.O_WRONLY|os.O_CREATE, 0777)
	if err != nil {
		return fname, total, err
	}
	defer out_f.Close()

	// start reading/writing

	for {
		// read
		n, err := in_f.Read(buffer)
		if n == 0 {
			break
		}
		if err != nil {
			return fname, total, err
		}
		// update total bytes
		total += n
		if total > maxbytes {
			return fname, total, fmt.Errorf("exceeded maximum file size of %d bytes", maxbytes)
		}
		// write
		n, err = out_f.Write(buffer[:n])
		if err != nil {
			return fname, total, err
		}
	}

	return fname, total, nil
}
Ejemplo n.º 26
0
func (ui *UIHandler) serveUploadHelper(rw http.ResponseWriter, req *http.Request) {
	rollSum := req.URL.Query().Get("rollsum") == "1"

	ret := make(map[string]interface{})
	defer httputil.ReturnJson(rw, ret)

	if ui.Storage == nil {
		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"
		return
	}

	got := make([]map[string]interface{}, 0)
	for {
		part, err := mr.NextPart()
		if err == io.EOF {
			break
		}
		if err != nil {
			ret["error"] = "reading body: " + err.Error()
			ret["errorType"] = "server"
			break
		}
		fileName := part.FileName()
		if fileName == "" {
			continue
		}
		writeFn := schema.WriteFileFromReader
		if rollSum {
			writeFn = schema.WriteFileFromReaderRolling
		}
		br, err := writeFn(ui.Storage, fileName, part)

		if err == nil {
			got = append(got, map[string]interface{}{
				"filename": part.FileName(),
				"formname": part.FormName(),
				"fileref":  br.String(),
			})
		} else {
			ret["error"] = "writing to blobserver: " + err.Error()
			return
		}
	}
	ret["got"] = got
}
Ejemplo n.º 27
0
func PutNoImageProcess(w http.ResponseWriter, r *http.Request) {
	t0 := time.Now()
	if r.Method != "PUT" {
		w.Write(json.Message("ERROR", "Not supported Method"))
		return
	}

	reader, err := r.MultipartReader()
	if err != nil {
		w.Write(json.Message("ERROR", "Client should support multipart/form-data"))
		return
	}

	buf := bytes.NewBufferString("")
	for {
		part, err := reader.NextPart()
		if err == io.EOF {
			break
		}
		if part.FileName() == "" { // if empy skip this iteration
			continue
		}
		_, err = io.Copy(buf, part)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}
	defer r.Body.Close()

	img, _, err := image.Decode(buf)
	if err != nil {
		w.Write(json.Message("ERROR", "Unable to decode your image"))
		return
	}

	fileOrig, err := imgToFile(img, "ACA0AC")
	if err != nil {
		w.Write(json.Message("ERROR", "Unable to save your image"))
	}

	result := json.Result{
		Newborn: fileOrig,
	}
	if err != nil {
		w.Write(json.Message("ERROR", "Unable to save your image meta into db"))
	} else {
		w.Write(json.Message("OK", &result))
	}

	t1 := time.Now()
	log.Printf("The call took %v to run.\n", t1.Sub(t0))
}
Ejemplo n.º 28
0
func HelloServer3(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	_, fe := r.MultipartReader()
	if fe != nil {
		println("HelloServer3", fe)
		return
	}
	println(r.Form == nil)
	val := r.FormValue("key")
	println("HelloServer3", val)
	w.Write([]byte(val))
}
Ejemplo n.º 29
0
//This is where the action happens.
func uploadHandler(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	//GET displays the upload form.
	case "GET":
		display(w, "upload", nil)

	//POST takes the uploaded file(s) and saves it to disk.
	case "POST":
		//get the multipart reader for the request.
		reader, err := r.MultipartReader()

		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		//copy each file to the fs
		for {
			part, err := reader.NextPart()
			//no more files to process when io.EOF is found
			if err == io.EOF {
				break
			}

			//if part.FileName() is empty, skip this iteration.
			if part.FileName() == "" {
				continue
			}
			mediaName := strings.SplitN(part.FileName(), ".", 2)
			//create a timestamp
			ts := time.Now().Unix()
			stamp := fmt.Sprint(ts)
			//write the file to the fs
			dst, err := os.Create("./media/" + mediaName[0] + "_" + stamp + "." + mediaName[1])
			defer dst.Close()

			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}

			if _, err := io.Copy(dst, part); err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
		}
		//display success message.
		display(w, "upload", "Upload successful.")
	default:
		w.WriteHeader(http.StatusMethodNotAllowed)
	}
}
Ejemplo n.º 30
0
func ParseUpload(r *http.Request) (fileName string, data []byte, mimeType string, isGzipped bool, modifiedTime uint64, ttl *TTL, e error) {
	form, fe := r.MultipartReader()
	if fe != nil {
		glog.V(0).Infoln("MultipartReader [ERROR]", fe)
		e = fe
		return
	}
	part, fe := form.NextPart()
	if fe != nil {
		glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
		e = fe
		return
	}
	fileName = part.FileName()
	if fileName != "" {
		fileName = path.Base(fileName)
	}

	data, e = ioutil.ReadAll(part)
	if e != nil {
		glog.V(0).Infoln("Reading Content [ERROR]", e)
		return
	}
	dotIndex := strings.LastIndex(fileName, ".")
	ext, mtype := "", ""
	if dotIndex > 0 {
		ext = strings.ToLower(fileName[dotIndex:])
		mtype = mime.TypeByExtension(ext)
	}
	contentType := part.Header.Get("Content-Type")
	if contentType != "" && mtype != contentType {
		mimeType = contentType //only return mime type if not deductable
		mtype = contentType
	}
	if part.Header.Get("Content-Encoding") == "gzip" {
		isGzipped = true
	} else if IsGzippable(ext, mtype) {
		if data, e = GzipData(data); e != nil {
			return
		}
		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)
	ttl, _ = ReadTTL(r.FormValue("ttl"))
	return
}