// UploadStores get writable stores for http upload func (d *Directory) UploadStores(bucket string, f *meta.File) (n *meta.Needle, stores []string, err error) { var ( key int64 vid int32 svrs []string store string storeMeta *meta.Store ok bool ) if vid, err = d.dispatcher.VolumeId(d.group, d.storeVolume); err != nil { log.Errorf("dispatcher.VolumeId error(%v)", err) err = errors.ErrStoreNotAvailable return } svrs = d.volumeStore[vid] stores = make([]string, 0, len(svrs)) for _, store = range svrs { if storeMeta, ok = d.store[store]; !ok { err = errors.ErrZookeeperDataError return } stores = append(stores, storeMeta.Api) } if key, err = d.genkey.Getkey(); err != nil { log.Errorf("genkey.Getkey() error(%v)", err) err = errors.ErrIdNotAvailable return } n = new(meta.Needle) n.Key = key n.Vid = vid n.Cookie = d.cookie() f.Key = key if err = d.hBase.Put(bucket, f, n); err != nil { if err != errors.ErrNeedleExist { log.Errorf("hBase.Put error(%v)", err) err = errors.ErrHBase } } return }
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 }