コード例 #1
0
ファイル: labelgraph.go プロジェクト: hanslovsky/dvid
// ExtractGraph takes the client's supplied JSON, verifies that it conforms to the
// schema, and loads it into the LabelGraph data structure
func (d *Data) ExtractGraph(r *http.Request, disableSchema bool) (*LabelGraph, error) {
	labelgraph := new(LabelGraph)
	if r.Body == nil {
		return labelgraph, nil
	}

	// read json
	data, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return labelgraph, err
	}
	if len(data) == 0 {
		return labelgraph, nil
	}

	// load data labelgraph and generic string-inteface{} map
	err = json.Unmarshal(data, labelgraph)

	if err != nil {
		return labelgraph, err
	}

	if !disableSchema {
		// check schema
		var schema_data interface{}
		json.Unmarshal([]byte(graphSchema), &schema_data)

		schema, err := gojsonschema.NewJsonSchemaDocument(schema_data)
		if err != nil {
			err = fmt.Errorf("JSON schema did not build")
			return labelgraph, err
		}

		var json_data map[string]interface{}
		err = json.Unmarshal(data, &json_data)
		if err != nil {
			return labelgraph, err
		}

		validationResult := schema.Validate(json_data)
		if !validationResult.Valid() {
			err = fmt.Errorf("JSON did not pass validation")
			return labelgraph, err
		}
	}

	return labelgraph, err
}
コード例 #2
0
// formHandler handles post request to "/formhandler" from the web interface
func (s *Server) formHandler(w http.ResponseWriter, r *http.Request) {
	pathlist, requestType, err := parseURI(r, "/formhandler/")
	if err != nil || len(pathlist) != 0 {
		badRequest(w, "Error: incorrectly formatted request")
		return
	}
	if requestType != "post" {
		badRequest(w, "only supports posts")
		return
	}

	session_id := randomHex()
	tstamp := int(time.Now().Unix())
	session_id = session_id + "-" + strconv.Itoa(tstamp)
	session_dir := s.outputDir + "/" + session_id
	err = os.MkdirAll(session_dir, 0755)
	if err != nil {
		badRequest(w, "Cannot create temporary directory")
		return
	}

	h5data, _, err := r.FormFile("h5file")
	if err != nil {
		badRequest(w, "gzip h5 file not provided")
	}
	//bytes, err := ioutil.ReadAll(h5data)
	zreader, err := gzip.NewReader(h5data)
	if err != nil {
		badRequest(w, "Could not read gzip h5 file")
		return
	}
	defer zreader.Close()

	//bytes := make([]byte, 0)
	//nread, err := zreader.Read(bytes)
	bytes, err := ioutil.ReadAll(zreader)
	if err != nil {
		badRequest(w, "Could not read gzip h5 file")
		os.RemoveAll(session_dir)
		return
	}

	ioutil.WriteFile(session_dir+"/"+h5name, bytes, 0644)

	graphdata, _, err := r.FormFile("graphfile")
	if err != nil {
		badRequest(w, "json file not provided")
		os.RemoveAll(session_dir)
		return
	}
	bytes2, err := ioutil.ReadAll(graphdata)
	if err != nil {
		badRequest(w, "Could not be read graph json")
		os.RemoveAll(session_dir)
		return
	}

	// convert schema to json data
	var schema_data interface{}
	json.Unmarshal([]byte(graphSchema), &schema_data)

	// convert json bytes to json object
	var graph_json interface{}
	json.Unmarshal(bytes2, &graph_json)

	// validate json schema
	schema, err := gojsonschema.NewJsonSchemaDocument(schema_data)
	validationResult := schema.Validate(graph_json)
	if !validationResult.Valid() {
		badRequest(w, "JSON did not pass validation")
		os.RemoveAll(session_dir)
		return
	}
	ioutil.WriteFile(session_dir+"/"+graphname, bytes2, 0644)

	// write initial status
	status := make(map[string]interface{})
	status["status"] = "Started"
	s.transactions.updateTran(session_id, status)

	// launch job
	go s.launchJob(session_id, session_dir, r.RemoteAddr)

	// dump json callback
	w.Header().Set("Content-Type", "application/json")
	jsondata, _ := json.Marshal(map[string]interface{}{
		"status-callback": "/status/" + session_id,
	})
	fmt.Fprintf(w, string(jsondata))

}