Beispiel #1
0
func (s *server) get(wr http.ResponseWriter, r *http.Request) {
	var (
		err      error
		n        *meta.Needle
		bucket   string
		filename string
		res      meta.Response
		ok       bool
		uerr     errors.Error
	)
	if r.Method != "GET" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	if bucket = r.FormValue("bucket"); bucket == "" {
		http.Error(wr, "bad request", http.StatusBadRequest)
		return
	}
	if filename = r.FormValue("filename"); filename == "" {
		http.Error(wr, "bad request", http.StatusBadRequest)
		return
	}
	defer HttpGetWriter(r, wr, time.Now(), &res)
	if n, res.Stores, err = s.d.GetStores(bucket, filename); err != nil {
		log.Errorf("GetStores() error(%v)", err)
		if uerr, ok = err.(errors.Error); ok {
			res.Ret = int(uerr)
		} else {
			res.Ret = errors.RetInternalErr
		}
		return
	}
	res.Ret = errors.RetOK
	res.Key = n.Key
	res.Cookie = n.Cookie
	res.Vid = n.Vid
	return
}
Beispiel #2
0
func (s *server) upload(wr http.ResponseWriter, r *http.Request) {
	var (
		err    error
		n      *meta.Needle
		f      *meta.File
		bucket string
		res    meta.Response
		ok     bool
		uerr   errors.Error
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	f = new(meta.File)
	if bucket = r.FormValue("bucket"); bucket == "" {
		http.Error(wr, "bad request", http.StatusBadRequest)
		return
	}
	if f.Filename = r.FormValue("filename"); f.Filename == "" {
		http.Error(wr, "bad request", http.StatusBadRequest)
		return
	}
	if f.Sha1 = r.FormValue("sha1"); f.Sha1 == "" {
		http.Error(wr, "bad request", http.StatusBadRequest)
		return
	}
	if f.Mine = r.FormValue("mine"); f.Mine == "" {
		http.Error(wr, "bad request", http.StatusBadRequest)
		return
	}
	defer HttpUploadWriter(r, wr, time.Now(), &res)

	res.Ret = errors.RetOK
	if n, res.Stores, err = s.d.UploadStores(bucket, f); err != nil {
		if err == errors.ErrNeedleExist {
			// update file data
			res.Ret = errors.RetNeedleExist
			if n, res.Stores, err = s.d.GetStores(bucket, f.Filename); err != nil {
				log.Errorf("GetStores() error(%v)", err)
				if uerr, ok = err.(errors.Error); ok {
					res.Ret = int(uerr)
				} else {
					res.Ret = errors.RetInternalErr
				}
				return
			}
		} else {
			log.Errorf("UploadStores() error(%v)", err)
			if uerr, ok = err.(errors.Error); ok {
				res.Ret = int(uerr)
			} else {
				res.Ret = errors.RetInternalErr
			}
			return
		}
	}
	res.Key = n.Key
	res.Cookie = n.Cookie
	res.Vid = n.Vid
	return
}