Exemplo n.º 1
0
func pullHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	log.Println(r.URL.Path + "(pull)\n")
	result, _ := ioutil.ReadAll(r.Body)
	reqJson := ds.DsPull{}
	var strret string

	if err := json.Unmarshal(result, &reqJson); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(err.Error()))
		return
	}

	reqJson.Repository = ps.ByName("repo")
	reqJson.Dataitem = ps.ByName("item")
	if exist := CheckDataPoolExist(reqJson.Datapool); exist == false {
		strret = reqJson.Datapool + " not found. " + reqJson.Tag + " will be pull into " + g_strDpPath
	} else {
		strret = reqJson.Repository + "/" + reqJson.Dataitem + "/" + reqJson.Tag + " will be pull into " + reqJson.Datapool
	}
	fmt.Println(strret)
	msgret := ds.MsgResp{Msg: strret}
	resp, _ := json.Marshal(msgret)
	w.Write(resp)

	//url := "/transaction/" + ps.ByName("repo") + "/" + ps.ByName("item") + "/" + reqJson.Tag

	//token, entrypoint, err := getAccessToken(url, w)
	/*if err != nil {
		return
	} else {
		url = "/pull/" + ps.ByName("repo") + "/" + ps.ByName("item") + "/" + reqJson.Tag +
			"?token=" + token + "?username="******"/transaction/" + ps.ByName("repo") + "/" + ps.ByName("item") + "/" + reqJson.Tag

		token, entrypoint, err := getAccessToken(url, w)
		if err != nil {
			return
		} else {
			url = "/pull/" + ps.ByName("repo") + "/" + ps.ByName("item") + "/" + reqJson.Tag +
				"?token=" + token + "?username="******"/pull/" + ps.ByName("repo") + "/" + ps.ByName("item") + "/" + reqJson.Tag
	entrypoint := ""
	go dl(url, entrypoint, reqJson)
	return

}
Exemplo n.º 2
0
func pullHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	MetaFile := "meta.txt"
	SampleFile := "sample.txt"
	log.Println(r.URL.Path + "(pull)")
	result, _ := ioutil.ReadAll(r.Body)
	p := ds.DsPull{}
	p.ItemDesc = strings.Trim(p.ItemDesc, "/")
	if strings.Contains(p.ItemDesc, "/") == true {
		log.Println("The path of item can't contain '/'.", p.ItemDesc)
		w.Write([]byte(`{"msg":"The path of item can't contain '/'."}`))
		return
	}

	if err := json.Unmarshal(result, &p); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(err.Error()))
		return
	}

	p.Repository = ps.ByName("repo")
	p.Dataitem = ps.ByName("item")

	dpexist := CheckDataPoolExist(p.Datapool)
	if dpexist == false {
		e := fmt.Sprintf("Datapool '%s' does not exist.", p.Datapool)
		l := log.Error("Code:", cmd.ErrorDatapoolNotExits, e)
		logq.LogPutqueue(l)
		msgret := ds.Result{Code: cmd.ErrorDatapoolNotExits, Msg: e}
		resp, _ := json.Marshal(msgret)
		w.WriteHeader(http.StatusBadRequest)
		w.Write(resp)
		return
	}

	alItemdesc, err := GetItemDesc(p.Repository, p.Dataitem)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(err.Error()))
		return
	}
	if len(alItemdesc) != 0 && p.ItemDesc != alItemdesc {
		p.ItemDesc = alItemdesc
		//TODO add log tishi
	} else if len(p.ItemDesc) == 0 && len(alItemdesc) == 0 {
		p.ItemDesc = p.Repository + "_" + p.Dataitem
	}

	if dpconn := GetDataPoolDpconn(p.Datapool); len(dpconn) == 0 {
		strret = p.Datapool + " not found. " + p.Tag + " will be pulled into " + g_strDpPath + "/" + p.ItemDesc
	} else {
		if len(p.DestName) == 0 {
			strret = p.Repository + "/" + p.Dataitem + ":" + p.Tag + " will be pulled soon and can be found as " +
				strings.Split(dpconn, "##")[0] + "/" + p.ItemDesc + "/" + p.Tag
		} else {
			strret = p.Repository + "/" + p.Dataitem + ":" + p.Tag + " will be pulled soon and can be found as " +
				strings.Split(dpconn, "##")[0] + "/" + p.ItemDesc + "/" + p.DestName
		}
	}

	//add to automatic pull list
	if p.Automatic == true {
		if true == CheckExistInQueue(p) {
			strret = p.Repository + "/" + p.Dataitem + " is being pulled automatically."
		} else {
			AutomaticPullPutqueue(p)
			strret = p.Repository + "/" + p.Dataitem + " will be pulled automatically."
		}

		msgret := ds.MsgResp{Msg: strret}
		resp, _ := json.Marshal(msgret)
		w.WriteHeader(http.StatusOK)
		w.Write(resp)
		return
	}
	if p.CancelAutomatic == true {

		if true == AutomaticPullRmqueue(p) {
			strret = "Cancel the automatical pulling of " + p.Repository + "/" + p.Dataitem + " successfully."
		} else {
			strret = "you have already cancelled the automatical pulling of " + p.Repository + "/" + p.Dataitem
		}
		msgret := ds.MsgResp{Msg: strret}
		resp, _ := json.Marshal(msgret)
		w.WriteHeader(http.StatusOK)
		w.Write(resp)
		return
	}

	uri := "/transaction/" + ps.ByName("repo") + "/" + ps.ByName("item") + "/" + p.Tag

	token, entrypoint, err := getAccessToken(uri, w)
	if err != nil {
		log.Println(err)
		strret = err.Error()
		return
	} else {
		uri = "/pull/" + ps.ByName("repo") + "/" + ps.ByName("item") + "/" + p.Tag +
			"?token=" + token + "&username="******"Metafile and Samplefile is already exist")
		} else {
			dpconn := GetDataPoolDpconn(p.Datapool)
			os.MkdirAll(dpconn+"/"+p.ItemDesc+"/", 0777)

			m, err := os.OpenFile(dpconn+"/"+p.ItemDesc+"/"+MetaFile, os.O_RDWR|os.O_CREATE, 0644)

			if err != nil {
				return
			}
			defer m.Close()
			s, err := os.OpenFile(dpconn+"/"+p.ItemDesc+"/"+SampleFile, os.O_RDWR|os.O_CREATE, 0644)

			if err != nil {
				return
			}
			defer s.Close()
			path := "/api/repositories/" + ps.ByName("repo") + "/" + ps.ByName("item")

			resp, err := commToServerGetRsp("get", path, nil)
			if err != nil {
				log.Error(err)
				return
			}

			body, err := ioutil.ReadAll(resp.Body)

			ms := ds.ItemMs{}
			retResp := ds.Response{Data: &ms}
			json.Unmarshal(body, &retResp)

			m.WriteString(ms.Meta)
			s.WriteString(ms.Sample)

			defer resp.Body.Close()

		}
	}
	log.Println(strret)

	return
}