Ejemplo n.º 1
0
// StoreRelease saves what was uploaded. Buffered in case of large files.
func (r Release) StoreRelease(data multipart.File) {
	f, err := os.Create(r.ReleaseFilePath())
	if err != nil {
		log.Println("Can't open file: " + r.ReleaseFilePath())
		return
	}
	defer f.Close()

	buf := make([]byte, 1024)

	for {
		n, err := data.Read(buf)

		if err != nil && err != io.EOF {
			log.Println("Couldn't write file: " + r.ReleaseFilePath())
			break
		}

		if n == 0 {
			break
		}

		if _, err := f.Write(buf[:n]); err != nil {
			log.Println("Couldn't write file: " + r.ReleaseFilePath())
			break
		}
	}
}
Ejemplo n.º 2
0
Archivo: http_api.go Proyecto: qida/bfs
func (h httpUploadHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
	var (
		v                *Volume
		n                int
		vid, key, cookie int64
		err              error
		buf              []byte
		file             multipart.File
		res              = map[string]interface{}{"ret": RetOK}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), res)
	if err = r.ParseMultipartForm(NeedleMaxSize); err != nil {
		res["ret"] = RetInternalErr
		return
	}
	if vid, err = strconv.ParseInt(r.FormValue("vid"), 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("vid"),
			err)
		res["ret"] = RetParamErr
		return
	}
	if key, err = strconv.ParseInt(r.FormValue("key"), 10, 64); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("key"),
			err)
		res["ret"] = RetParamErr
		return
	}
	if cookie, err = strconv.ParseInt(r.FormValue("cookie"), 10, 64); err !=
		nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("cookie"),
			err)
		res["ret"] = RetParamErr
		return
	}
	if v = h.s.Volumes[int32(vid)]; v == nil {
		res["ret"] = RetNoVolume
		return
	}
	if file, _, err = r.FormFile("file"); err != nil {
		res["ret"] = RetInternalErr
		return
	}
	buf = v.Buffer()
	if n, err = file.Read(buf); err == nil {
		err = v.Add(key, cookie, buf[:n])
	}
	file.Close()
	v.FreeBuffer(buf)
	if err != nil {
		res["ret"] = RetUploadErr
	}
	return
}
Ejemplo n.º 3
0
func judgeContentType(f multipart.File) (string, string, error) {
	f.Seek(0, 0)
	buf := make([]byte, 16)
	count, err := f.Read(buf)
	if err != nil {
		return "", "", err
	}

	if count >= 10 && string(buf[6:10]) == "JFIF" {
		return "image/jpeg", ".jpg", nil
	}
	if count >= 4 && string(buf[0:3]) == "GIF" {
		return "image/gif", ".gif", nil
	}
	if count >= 2 && string(buf[1:4]) == "PNG" {
		return "image/png", ".png", nil
	}

	return "application/octet-stream", "", nil
}
Ejemplo n.º 4
0
func uploadFile(file multipart.File) string {
	clientid := os.Getenv("DROPBOX_KEY")
	clientsecret := os.Getenv("DROPBOX_TOKEN")
	token := os.Getenv("DROPBOX_ACCESS_TOKEN")

	DB := dropbox.NewDropbox()
	DB.SetAppInfo(clientid, clientsecret)
	DB.SetAccessToken(token)

	filename := fmt.Sprintf("%s.png", strconv.FormatInt(rand.Int63(), 32))
	filepath := fmt.Sprintf("/Public/Screenshots/%s", filename)

	var tmpstr []byte
	length, _ := file.Read(tmpstr)
	DB.FilesPut(file, int64(length), filepath, true, "")

	account, _ := DB.GetAccountInfo()
	uid := strconv.FormatInt(int64(account.UID), 10)

	return fmt.Sprintf("https://dl.dropbox.com/u/%s/Screenshots/%s", uid, filename)
}
Ejemplo n.º 5
0
func isSupported(file multipart.File) (bool, string, error) {
	buff := make([]byte, 512)
	_, err := file.Read(buff)

	// Reset the file
	file.Seek(0, 0)

	if err != nil {
		return false, "", err
	}

	filetype := http.DetectContentType(buff)

	for _, i := range supportedFileTypes {
		if i == filetype {
			return true, filetype, nil
		}
	}

	log.Println("File not supported: " + filetype)
	return false, filetype, nil
}
Ejemplo n.º 6
0
Archivo: http_api.go Proyecto: qida/bfs
func (h httpUploadsHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
	var (
		i, wn               int
		v                   *Volume
		n                   *Needle
		buf                 []byte
		str                 string
		err                 error
		vid, key, cookie    int64
		keyStrs, cookieStrs []string
		keys, cookies       []int64
		fh                  *multipart.FileHeader
		fhs                 []*multipart.FileHeader
		file                multipart.File
		res                 = map[string]interface{}{"ret": RetOK}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), res)
	if err = r.ParseMultipartForm(NeedleMaxSize); err != nil {
		res["ret"] = RetInternalErr
		return
	}
	if vid, err = strconv.ParseInt(r.FormValue("vid"), 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("vid"),
			err)
		res["ret"] = RetParamErr
		return
	}
	keyStrs = strings.Split(r.FormValue("keys"), HttpParamSpliter)
	cookieStrs = strings.Split(r.FormValue("cookies"), HttpParamSpliter)
	if len(keyStrs) != len(cookieStrs) {
		log.Errorf("param length not match, keys: %d, cookies: %d",
			len(keyStrs), len(cookieStrs))
		res["ret"] = RetParamErr
		return
	}
	for i, str = range keyStrs {
		if key, err = strconv.ParseInt(str, 10, 64); err != nil {
			res["ret"] = RetParamErr
			return
		}
		if cookie, err = strconv.ParseInt(str, 10, 64); err != nil {
			res["ret"] = RetParamErr
			return
		}
		keys = append(keys, key)
		cookies = append(cookies, cookie)
	}
	if r.MultipartForm != nil {
		if fhs = r.MultipartForm.File["file"]; len(fhs) > HttpMaxUploadFiles {
			res["ret"] = RetUploadMaxFile
			return
		}
	}
	if len(keys) != len(fhs) {
		log.Errorf("param length not match, keys: %d, cookies: %d, files: %d",
			len(keys), len(cookies), len(fhs))
		res["ret"] = RetParamErr
		return
	}
	if v = h.s.Volumes[int32(vid)]; v == nil {
		res["ret"] = RetNoVolume
		return
	}
	// TODO?
	// use a large buffer stored all file buffer
	// this can let the lock without file read and needle parse.
	buf = v.Buffer()
	n = v.Needle()
	v.Lock()
	for i, fh = range fhs {
		if file, err = fh.Open(); err == nil {
			if wn, err = file.Read(buf); err == nil {
				if err = n.Parse(keys[i], cookies[i], buf[:wn]); err == nil {
					err = v.Write(n)
				}
			}
			file.Close()
		}
		if err != nil {
			goto free
		}
	}
	v.Flush()
free:
	v.Unlock()
	v.FreeNeedle(n)
	v.FreeBuffer(buf)
	if err != nil {
		res["ret"] = RetUploadErr
	}
	return
}
Ejemplo n.º 7
0
func (h httpUploadsHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
	var (
		i, rn, tn, nn, nb int
		ok                bool
		buf               []byte
		err               error
		vid               int64
		key               int64
		cookie            int64
		size              int64
		str               string
		keys              []string
		cookies           []string
		sr                sizer
		fr                *os.File
		fi                os.FileInfo
		v                 *Volume
		n                 *needle.Needle
		ns                []needle.Needle
		uerr              errors.Error
		file              multipart.File
		fh                *multipart.FileHeader
		fhs               []*multipart.FileHeader
		res               = map[string]interface{}{"ret": errors.RetOK}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), res)
	// check total content-length
	if size, err = strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64); err != nil {
		res["ret"] = errors.RetInternalErr
		return
	}
	if size > int64(h.c.NeedleMaxSize*h.c.BatchMaxNum) {
		res["ret"] = errors.RetNeedleTooLarge
		return
	}
	str = r.FormValue("vid")
	if vid, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		res["ret"] = errors.RetParamErr
		return
	}
	keys = r.MultipartForm.Value["keys"]
	cookies = r.MultipartForm.Value["cookies"]
	if len(keys) != len(cookies) {
		log.Errorf("param length not match, keys: %d, cookies: %d", len(keys), len(cookies))
		res["ret"] = errors.RetParamErr
		return
	}
	fhs = r.MultipartForm.File["file"]
	nn = len(fhs)
	if len(keys) != nn {
		log.Errorf("param length not match, keys: %d, cookies: %d, files: %d", len(keys), len(cookies), len(fhs))
		res["ret"] = errors.RetParamErr
		return
	}
	nb = int(size-1)/(h.c.NeedleMaxSize) + 1
	buf = h.s.Buffer(nb)
	ns = h.s.Needle(nn)
	for i, fh = range fhs {
		if key, err = strconv.ParseInt(keys[i], 10, 64); err != nil {
			log.Errorf("strconv.ParseInt(\"%s\") error(%v)", keys[i], err)
			err = errors.ErrParam
			break
		}
		if cookie, err = strconv.ParseInt(cookies[i], 10, 32); err != nil {
			log.Errorf("strconv.ParseInt(\"%s\") error(%v)", cookies[i], err)
			err = errors.ErrParam
			break
		}
		file, err = fh.Open()
		file.Close()
		if err != nil {
			log.Errorf("fh.Open() error(%v)", err)
			break
		}
		// check size
		if sr, ok = file.(sizer); ok {
			size = sr.Size()
		} else if fr, ok = file.(*os.File); ok {
			if fi, err = fr.Stat(); err != nil {
				break
			}
			size = fi.Size()
		}
		if size > int64(h.c.NeedleMaxSize) {
			err = errors.ErrNeedleTooLarge
			break
		}
		if rn, err = file.Read(buf[tn:]); err != nil {
			log.Errorf("file.Read() error(%v)", err)
			break
		}
		n = &(ns[i])
		n.Parse(key, int32(cookie), buf[tn:tn+rn])
		tn += rn
	}
	if err == nil {
		h.s.RLockVolume()
		if v = h.s.Volumes[int32(vid)]; v != nil {
			v.Lock()
			for i = 0; i < nn; i++ {
				n = &(ns[i])
				if err = v.Write(n); err != nil {
					break
				}
			}
			if err == nil {
				err = v.Flush()
			}
			v.Unlock()
		} else {
			err = errors.ErrVolumeNotExist
		}
		h.s.RUnlockVolume()
	}
	h.s.FreeBuffer(nb, buf)
	h.s.FreeNeedle(nn, ns)
	if err != nil {
		if uerr, ok = err.(errors.Error); ok {
			res["ret"] = int(uerr)
		} else {
			res["ret"] = errors.RetInternalErr
		}
	}
	return
}
Ejemplo n.º 8
0
func (h httpUploadHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
	var (
		ok     bool
		rn     int
		vid    int64
		key    int64
		cookie int64
		size   int64
		err    error
		str    string
		buf    []byte
		v      *Volume
		n      *needle.Needle
		ns     []needle.Needle
		file   multipart.File
		sr     sizer
		fr     *os.File
		fi     os.FileInfo
		uerr   errors.Error
		res    = map[string]interface{}{"ret": errors.RetOK}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), res)
	// check total content-length
	if size, err = strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64); err != nil {
		res["ret"] = errors.RetInternalErr
		return
	}
	if size > int64(h.c.NeedleMaxSize) {
		res["ret"] = errors.RetNeedleTooLarge
		return
	}
	str = r.FormValue("vid")
	if vid, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		res["ret"] = errors.RetParamErr
		return
	}
	str = r.FormValue("key")
	if key, err = strconv.ParseInt(str, 10, 64); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		res["ret"] = errors.RetParamErr
		return
	}
	str = r.FormValue("cookie")
	if cookie, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		res["ret"] = errors.RetParamErr
		return
	}
	if file, _, err = r.FormFile("file"); err != nil {
		res["ret"] = errors.RetInternalErr
		return
	}
	if sr, ok = file.(sizer); ok {
		size = sr.Size()
	} else if fr, ok = file.(*os.File); ok {
		if fi, err = fr.Stat(); err != nil {
			res["ret"] = errors.RetInternalErr
			return
		}
		size = fi.Size()
	}
	if size > int64(h.c.NeedleMaxSize) {
		res["ret"] = errors.RetNeedleTooLarge
		return
	}
	ns = h.s.Needle(1)
	n = &(ns[0])
	buf = h.s.Buffer(1)
	rn, err = file.Read(buf)
	file.Close()
	if err != nil {
		res["ret"] = errors.RetInternalErr
		return
	}
	n.Parse(key, int32(cookie), buf[:rn])
	h.s.RLockVolume()
	if v = h.s.Volumes[int32(vid)]; v != nil {
		err = v.Add(n)
	} else {
		err = errors.ErrVolumeNotExist
	}
	h.s.RUnlockVolume()
	h.s.FreeBuffer(1, buf)
	h.s.FreeNeedle(1, ns)
	if err != nil {
		if uerr, ok = err.(errors.Error); ok {
			res["ret"] = int(uerr)
		} else {
			res["ret"] = errors.RetInternalErr
		}
	}
	return
}