func getHTML(ctx context.Context, markdown string) (search.HTML, error) { client := urlfetch.Client(ctx) resp, err := client.Post("https://api.github.com/markdown/raw", "text/plain", strings.NewReader(markdown)) if err != nil { return "", err } defer resp.Body.Close() bts, err := ioutil.ReadAll(resp.Body) return search.HTML(bts), err }
func handleNewMovie(res http.ResponseWriter, req *http.Request) { ctx := appengine.NewContext(req) model := &newMovieModel{} if req.Method == "POST" { title := req.FormValue("title") summary := req.FormValue("summary") poster, _, err := req.FormFile("poster") if err != nil { http.Error(res, err.Error(), 500) return } defer poster.Close() posterID, _ := uuid.NewV4() err = putFile(ctx, posterID.String(), poster) if err != nil { http.Error(res, err.Error(), 500) return } summary, err = renderMarkdown(ctx, summary) if err != nil { http.Error(res, err.Error(), 500) return } index, err := search.Open("movies") if err != nil { http.Error(res, err.Error(), 500) return } movie := &Movie{ Title: title, Summary: search.HTML(summary), PosterID: posterID.String(), } id, err := index.Put(ctx, "", movie) if err != nil { http.Error(res, err.Error(), 500) return } model.CreatedID = id } err := tpl.ExecuteTemplate(res, "new-movie", model) if err != nil { http.Error(res, err.Error(), 500) return } }
func createSearchItem(con *data.Context, values url.Values, namespace string, URL string) (*search.Item, error) { DSKey := values["DSKey"][0] // We can't do anything without a DSKey if DSKey == "" || namespace == "" || URL == "" { con.Log.Errorf("Something is wrong with the input data. DSKey: %v, Namespace: %v, URL: %v.", DSKey, namespace, URL) return nil, fmt.Errorf("Something is wrong with the input data. DSKey: %v, Namespace: %v, URL: %v.", DSKey, namespace, URL) } memI, err := memcache.Get(con.C, DSKey) _ = memcache.Delete(con.C, DSKey) // ignore possible errors var searchItem search.Item searchItem.Description = values["Caption"][0] + " " + values["Description"][0] if err == nil { searchItem.HTMLforSearch = gaesearch.HTML(string(memI.Value)) } else { // ok, no data in the memcache. // we need to re-query the URL to get the HTML data c := urlextractclient.Get(con) info, err := c.Extract(URL) if err != nil { con.Log.Errorf("Error in URLextract.Extract(). Error: %v", err) return nil, err } var i data.Item i = data.ItemFromWebpageInfo(info) searchItem.HTMLforSearch = gaesearch.HTML(i.HTMLforSearch) } if s, err := strconv.ParseInt(values["CreatedAt"][0], 10, 64); err == nil { searchItem.CreatedAt = time.Unix(s, 0) } else { searchItem.CreatedAt = time.Now() } return &searchItem, nil }
// AddToSearchIndexTask is the implementation of the task described above. // This adds an item to the search index. func AddToSearchIndexTask(con *data.Context, searchItem *Item, namespace string, URL string) error { index, err := search.Open("items_" + namespace) if err != nil { con.Log.Errorf("Error while opening the item index %v", err) return err } searchItem.HTMLforSearch = search.HTML(optimizeSearchInput(string(searchItem.HTMLforSearch))) searchItem.Description = optimizeSearchInput(searchItem.Description) _, err = index.Put(con.C, strconv.QuoteToASCII(URL), searchItem) if err != nil { con.Log.Errorf("Error while puting the search item in the index %v", err) return err } return nil }