Пример #1
0
//DispatchJSON returns the json view of a namespace
func DispatchJSON(w http.ResponseWriter, r *http.Request) {
	con := data.MakeContext(r)

	// get namespace
	namespace := mux.Vars(r)["namespace"]
	if namespace == "" {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	is, _, err := data.GetNewestItems(con, namespace, 1, "")
	if err != nil {
		con.Log.Errorf("Error while getting 1 item for update/json. Error %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	var returnee jsonReturn
	if len(is) == 0 {
		returnee.LastUpdate = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC).Unix()
	} else {
		returnee.LastUpdate = is[0].CreatedAt.Unix()
	}

	s, err := json.Marshal(returnee)
	if err != nil {
		con.Log.Errorf("Error at mashaling in update/json. Error: %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	con.Log.Infof("returning: %v", returnee)

	w.Write(s)
}
Пример #2
0
// DispatchAddToIndex creates the search index for an item passed vie POST
// Used in a Queue.
func DispatchAddToIndex(w http.ResponseWriter, r *http.Request) {
	con := data.MakeContext(r)

	if err := r.ParseForm(); err != nil {
		con.Log.Errorf("Error at in DispatchAddToIndex @ ParseForm. Error: %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	namespace := r.Form["Namespace"][0]
	URL := r.Form["URL"][0]

	si, err := createSearchItem(con, r.Form, namespace, URL)
	if err != nil {
		con.Log.Errorf("Error while creating data.Search.Item from URL parameters.")
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	err = search.AddToSearchIndexTask(con, si, namespace, URL)
	if err != nil {
		con.Log.Errorf("Error while adding the Item to the seach index %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusOK)
}
Пример #3
0
// DispatchJSON receives an extension json request
func DispatchJSON(w http.ResponseWriter, r *http.Request) {
	con := data.MakeContext(r)

	// get namespace
	namespace := mux.Vars(r)["namespace"]
	if namespace == "" {
		startpage.Dispatch(w, r)
		return
	}

	shareURL := r.URL.Query().Get("url")

	if err := share.URL(shareURL, namespace, con); err != nil {
		con.Log.Errorf("Error while sharing an URL. URL: %v. Error: %v", shareURL, err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	w.Write(statusOk)
}
Пример #4
0
// DispatchEmail handles incoming emails
func DispatchEmail(w http.ResponseWriter, r *http.Request) {
	con := data.MakeContext(r)

	em, err := emailparse.Get(con, r.Body)
	if err != nil {
		con.Log.Errorf("Error at parsing email. Error: %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	if len(em.Images) > 0 {
		err2 := handleImages(con, em)
		if err2 != nil {
			con.Log.Errorf("%v", err2)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.WriteHeader(http.StatusOK)
		return
	}

	if len(em.Texts) > 0 {
		err2 := handleText(con, em)
		if err2 != nil {
			con.Log.Errorf("%v", err2)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.WriteHeader(http.StatusOK)
		return
	}

	con.Log.Errorf("Found no images and no URLs?.")
	w.WriteHeader(http.StatusInternalServerError)
}
Пример #5
0
// DispatchJSON is called when a json request with an url is posted
func DispatchJSON(w http.ResponseWriter, r *http.Request) {
	con := data.MakeContext(r)

	// get namespace
	namespace := mux.Vars(r)["namespace"]
	if namespace == "" {
		startpage.Dispatch(w, r)
		return
	}

	empty, err := data.NamespaceIsEmpty(con, namespace)

	if err == nil {

		if empty {
			w.Write(statusOk)
		} else {
			w.Write(statusInUse)
		}

	} else {
		w.Write(statusError)
	}
}
Пример #6
0
//DispatchJSON returns the json view of a namespace
func DispatchJSON(w http.ResponseWriter, r *http.Request) {
	con := data.MakeContext(r)

	// get namespace
	namespace := mux.Vars(r)["namespace"]
	if namespace == "" {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	if err := r.ParseForm(); err != nil {
		con.Log.Errorf("Error at in /show/json @ ParseForm. Error: %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	cursor := r.FormValue("cursor")

	// no cursos == first elements, may be in the cache
	if cursor == "" {
		cache, err := data.ReadJSONCache(con, namespace)
		if err == nil {
			w.Write(cache)
			return
		}

		if err == memcache.ErrCacheMiss {
			con.Log.Infof("Cache miss for namespace %v", namespace)
		} else {
			con.Log.Errorf("Error at in rss.dispatch while reading the cache. Error: %v", err)
		}
	}

	var returnee jsonReturn
	var err error

	returnee.Items, returnee.Cursor, err = data.GetNewestItems(con, namespace, 20, cursor)
	if err != nil {
		con.Log.Errorf("Error at in /show/json @ GetNewestItem. Error: %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	con.Log.Infof("items: %v", returnee.Items)
	con.Log.Infof("cursor: %v", returnee.Cursor)

	s, err := json.Marshal(returnee)
	if err != nil {
		con.Log.Errorf("Error at mashaling in www.json.dispatch. Error: %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	// only store values in cache for the first entries
	if cursor == "" {
		if err := data.CacheJSON(con, namespace, s); err != nil {
			con.Log.Errorf("Error at storing the JSON in the cache. Error: %v", err)
		}
	}

	w.Write(s)
}
Пример #7
0
//DispatchRSS returns the rss feed of namespace
func DispatchRSS(w http.ResponseWriter, r *http.Request) {
	con := data.MakeContext(r)

	w.Header().Set("Content-Type", "application/rss+xml")
	w.Header().Set("Cache-Control", "public, max-age=1800") // 30 minutes
	w.Header().Set("Pragma", "Public")

	// get namespace
	namespace := mux.Vars(r)["namespace"]
	if namespace == "" {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	cache, err := data.ReadRSSCache(con, namespace)
	if err == nil {
		w.Write([]byte(cache))
		return
	}
	if err == memcache.ErrCacheMiss {
		con.Log.Infof("Cache miss for namespace %v", namespace)
	} else {
		con.Log.Errorf("Error at in rss.dispatch while reading the cache. Error: %v", err)
	}

	t := time.Now()
	t = t.Add(-24 * time.Hour * config.RSSTimeRangeinDays)
	is, _, err := data.GetNewestItemsByTime(con, namespace, 100, t, "")
	if err != nil {
		con.Log.Errorf("Error at in rss.dispatch @ GetNewestItem. Error: %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	con.Log.Infof("items: %v", is)

	feed := &feeds.Feed{
		Title: namespace + " - Kaffeeshare",
		Link:  &feeds.Link{Href: r.URL.String()},
	}

	for _, i := range is {
		rssI := feeds.Item{
			Title:   i.Caption,
			Link:    &feeds.Link{Href: i.URL},
			Created: i.CreatedAt,
		}

		if i.ImageURL != "" {
			rssI.Description += "<div style=\"float:left; margin-right:16px; margin-bottom:16px;\"><img width=\"200\" src=\"" + i.ImageURL + "\" alt=\"\"/></div>"
		}

		rssI.Description += "<p>" + i.Description + "</p><br/><br/>"
		rssI.Description += "<a href=\"" + i.URL + "\">&raquo; " + i.URL + "</a>"

		feed.Items = append(feed.Items, &rssI)
	}

	s, err := feed.ToRss()
	if err != nil {
		con.Log.Errorf("Error at mashaling in www.dispatch. Error: %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	if err := data.CacheRSS(con, namespace, s); err != nil {
		con.Log.Errorf("Error at storing the RSS Feed in the cache. Error: %v", err)
	}

	w.Write([]byte(s))
}