Пример #1
0
func TestReadItem2(t *testing.T) {
	readItem := new(data.Item)

	testRead(t, "/items/"+strconv.FormatInt(item.ID, 10), readItem)

	readItem.StartTime = item.StartTime // hack
	readItem.StopTime = item.StopTime   // hack

	structJSONCompare(t, item, readItem)
}
Пример #2
0
func scanItem(item *data.Item, rows *sql.Rows) error {
	var startTimeString, stopTimeString string

	err := rows.Scan(
		&item.ID,
		&item.Type,
		&item.Author,
		&item.ThumbnailURI,
		&item.ContentURI,
		&item.UploadURI,
		&item.ContentToken,
		&item.HasHeading,
		&item.Heading,
		&item.HasGeo,
		&item.Lat,
		&item.Lng,
		&item.Radius,
		&item.Live,
		&startTimeString,
		&stopTimeString,
	)

	if err != nil {
		return err
	}

	// TODO: this is a hacky fix for null times
	if startTimeString == "0000-00-00 00:00:00" {
		startTimeString = "0001-01-01 00:00:00"
	}
	startTime, err := time.Parse("2006-01-02 15:04:05", startTimeString) // this assumes UTC as timezone

	if err != nil {
		log.Println("Item scanner failed to parse time. " + startTimeString)
		return err
	}

	item.StartTime = startTime

	// TODO: this is a hacky fix for null times
	if stopTimeString == "0000-00-00 00:00:00" {
		stopTimeString = "0001-01-01 00:00:00"
	}
	stopTime, err := time.Parse("2006-01-02 15:04:05", stopTimeString) // this assumes UTC as timezone

	if err != nil {
		log.Println("Item scanner failed to parse time. " + stopTimeString)
		return err
	}

	item.StopTime = stopTime

	return nil
}
Пример #3
0
func TestReadItem3(t *testing.T) {
	readItem := new(data.Item)

	testRead(t, "/items/"+strconv.FormatInt(item.ID, 10), readItem)

	readItem.StartTime = item.StartTime // hack
	readItem.StopTime = item.StopTime   // hack

	if len(readItem.Terms) < 1 {
		t.Error("There should be a term here")
	}

	readItem.Terms[0].UpdateTime = term.UpdateTime

	structJSONCompare(t, item, readItem)
}
Пример #4
0
func TestSelectItem(t *testing.T) {
	selectedItem := new(data.Item)
	selectedItem.ID = item.ID
	err := Select(selectedItem)

	if err != nil {
		t.Error(err)
	}

	t.Log(item.StartTime.UTC())
	t.Log(selectedItem.StartTime.UTC())

	t.Log(item.StopTime.UTC())
	t.Log(selectedItem.StopTime.UTC())

	selectedItem.StartTime = item.StartTime // hack
	selectedItem.StopTime = item.StopTime   // hack

	structJSONCompare(t, item, selectedItem)
}
Пример #5
0
// 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)))
}