コード例 #1
0
ファイル: crud.go プロジェクト: cyc115/rter
// Generic Read handler for reading multiple objects possibly with a query
func ReadWhere(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")

	vars := mux.Vars(r)

	var (
		val interface{} // Generic container for the read objecs
		err error
	)

	whereClause := ""              //We may build up some sort of WHERE clause for the DB request
	args := make([]interface{}, 0) //This WHERE clause would then require some arguments

	// Build a URI like representation of the datatype
	types := []string{vars["datatype"]}

	if childtype, ok := vars["childtype"]; ok {
		types = append(types, childtype)
	}

	// Switch based on that URI like representation and instantiate something in the generic container. Also infer the identifier from the vars and perform validation.
	switch strings.Join(types, "/") {
	case "items":
		items := make([]*data.Item, 0)
		val = &items
	case "items/comments":
		comments := make([]*data.ItemComment, 0)

		// Selecting comments is reliant on the item ID
		whereClause = "WHERE ItemID=?"
		var itemID int64
		itemID, err = strconv.ParseInt(vars["key"], 10, 64)
		args = append(args, itemID)

		val = &comments
	case "items/geolocations":
		geolocations := make([]*data.Geolocation, 0)
		// Selecting geolocations is reliant on the item ID
		whereClause = "WHERE ItemID=?"
		var itemID int64
		itemID, err = strconv.ParseInt(vars["key"], 10, 64)
		args = append(args, itemID)
		val = &geolocations
	case "users":
		users := make([]*data.User, 0)
		val = &users
	case "roles":
		roles := make([]*data.Role, 0)
		val = &roles
	case "taxonomy":
		terms := make([]*data.Term, 0)
		val = &terms
	default:
		http.NotFound(w, r)
		return
	}

	if err != nil {
		log.Println(err)
		http.Error(w, "Malformed key in URI", http.StatusBadRequest)
		return
	}

	// Sadly due to the need to load Term.Count value we must have some custom queries here
	switch val.(type) {
	case *[]*data.Term:
		err = storage.SelectQuery(val, "SELECT t.*, count(r.Term) FROM Terms AS t, TermRelationships AS r WHERE t.Term = r.Term GROUP BY t.Term")
	default:
		err = storage.SelectWhere(val, whereClause, args...)
	}

	if err == storage.ErrZeroAffected {
		http.Error(w, "No matches for query", http.StatusNotFound)
		return
	}

	if err != nil {
		log.Println(err)
		http.Error(w, "Select2 Database error, likely due to malformed request", http.StatusInternalServerError)
		return
	}

	// Important. Let's never send salt/hash out
	switch v := val.(type) {
	case *[]*data.User:
		for _, user := range *v {
			user.Salt = ""
			user.Password = ""
		}
	}

	w.Header().Set("Content-Type", "application/json") // Header are important when GZIP is enabled

	// Return the selected objects
	encoder := json.NewEncoder(w)
	err = encoder.Encode(val)

	if err != nil {
		log.Println(err)
	}
}
コード例 #2
0
ファイル: legacy.go プロジェクト: falahhaprak/rter
// Handle a POST request with an image, phone_id, lat, lng and heading. Return a target heading from the web UI
func MultiUploadHandler(rterDir string, uploadPath string, w http.ResponseWriter, r *http.Request) {
	imageFile, header, err := r.FormFile("image")
	if err != nil {
		return
	}

	user := new(data.User)
	user.Username = r.FormValue("phone_id")

	err = storage.Select(user)

	if err == storage.ErrZeroAffected {
		user.Role = "public"
		storage.Insert(user)
		// log.Println("upload failed, phone_id invalid:", user.Username)
		// http.Error(w, "Invalid credentials.", http.StatusUnauthorized)
		// return
	} else if err != nil {
		log.Println(err)
		http.Error(w, "Database error, likely due to malformed request.", http.StatusInternalServerError)
		return
	}

	os.Mkdir(filepath.Join(uploadPath, user.Username), os.ModeDir|0775)

	matchingItems := make([]*data.Item, 0)
	err = storage.SelectWhere(&matchingItems, "WHERE Type=\"streaming-video-v0\" AND Author=?", user.Username)

	exists := true

	if err == storage.ErrZeroAffected {
		exists = false
	} else if err != nil {
		log.Println(err)
		http.Error(w, "Database error, likely due to malformed request.", http.StatusInternalServerError)
		return
	}

	var item *data.Item

	if exists {
		item = matchingItems[0]
	} else {
		item = new(data.Item)
	}

	item.Author = user.Username

	item.Type = "streaming-video-v0"
	item.Live = true
	item.HasGeo = true
	item.HasHeading = true

	item.Lat, err = strconv.ParseFloat(r.FormValue("lat"), 64)
	if err != nil {
		item.HasGeo = false
	}

	item.Lng, err = strconv.ParseFloat(r.FormValue("lng"), 64)
	if err != nil {
		item.HasGeo = false
	}

	item.Heading, err = strconv.ParseFloat(r.FormValue("heading"), 64)
	if err != nil {
		item.HasHeading = false
	}

	item.StartTime = time.Now()
	item.StopTime = item.StartTime
	path := uploadPath

	if strings.HasSuffix(header.Filename, ".png") {
		path = filepath.Join(path, fmt.Sprintf("%v/%v.png", user.Username, item.StopTime.UnixNano()))
	} else if strings.HasSuffix(header.Filename, ".jpg") || strings.HasSuffix(header.Filename, "jpeg") {
		path = filepath.Join(path, fmt.Sprintf("%v/%v.jpg", user.Username, item.StopTime.UnixNano()))
	}

	outputFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0664)

	if err != nil {
		log.Println(err)
		http.Error(w, "Error, likely due to malformed request.", http.StatusInternalServerError)
	}
	defer outputFile.Close()

	io.Copy(outputFile, imageFile)

	path = path[len(rterDir):]

	item.ContentURI = path
	item.ThumbnailURI = path

	if exists {
		storage.Update(item)
	} else {
		storage.Insert(item)
	}

	userDirection := new(data.UserDirection)
	userDirection.Username = user.Username

	err = storage.Select(userDirection)

	if err != nil {
		log.Println(err)
		http.Error(w, "Database error, likely due to malformed request.", http.StatusInternalServerError)
		return
	}

	w.Write([]byte(strconv.FormatFloat(userDirection.Heading, 'f', 6, 64)))
}