Example #1
0
// getCommand takes an actionid and a commandid and returns a command
func getCommand(respWriter http.ResponseWriter, request *http.Request) {
	var err error
	opid := mig.GenID()
	resource := cljs.New(request.URL.String())
	defer func() {
		if e := recover(); e != nil {
			ctx.Channels.Log <- mig.Log{OpID: opid, Desc: fmt.Sprintf("%v", e)}.Err()
			resource.SetError(cljs.Error{Code: fmt.Sprintf("%d", opid), Message: fmt.Sprintf("%v", e)})
			respond(500, resource, respWriter, request, opid)
		}
		ctx.Channels.Log <- mig.Log{OpID: opid, Desc: "leaving getCommand()"}.Debug()
	}()
	var actionID, commandID int
	aid := request.URL.Query()["actionid"][0]
	if aid != "" {
		actionID, err = strconv.Atoi(aid)
		if err != nil {
			panic(err)
		}
	}
	cmdid := request.URL.Query()["commandid"][0]
	if cmdid != "" {
		commandID, err = strconv.Atoi(cmdid)
		if err != nil {
			panic(err)
		}
	}

	// retrieve the action
	cmds := []mig.Command{}
	var iter *mgo.Iter
	if commandID > 0 {
		if actionID > 0 {
			iter = ctx.DB.Col.Cmd.Find(bson.M{"id": commandID, "action.id": actionID}).Iter()
		} else {
			iter = ctx.DB.Col.Cmd.Find(bson.M{"id": commandID}).Iter()
		}
	} else {
		// nothing to search for, return empty resource
		respond(200, resource, respWriter, request, opid)
	}
	err = iter.All(&cmds)
	if err != nil {
		panic(err)
	}
	if len(cmds) == 0 {
		resource.SetError(cljs.Error{Code: fmt.Sprintf("%d", opid), Message: "No command found"})
		respond(404, resource, respWriter, request, opid)
	}
	// store the results in the resource
	for _, cmd := range cmds {
		commandItem, err := commandToItem(cmd)
		if err != nil {
			panic(err)
		}
		resource.AddItem(commandItem)
	}
	respond(200, resource, respWriter, request, opid)
}
Example #2
0
// get an action ID and returns a list of commands that have positive results
func findPositiveResults(actionID int) (cmds []mig.Command, err error) {
	// retrieve the commands that have positive results
	cmds = []mig.Command{}
	var iter *mgo.Iter
	iter = ctx.DB.Col.Cmd.Find(bson.M{"action.id": actionID, "results.0.foundanything": true}).Iter()
	err = iter.All(&cmds)
	return
}
Example #3
0
/*
  GET /k/
  GET /k/:name
  GET /k/:name/r

  Show a page of all the keyword tags, and then the images
  If /k/:name/r then show a random image by keyword name
  Otherwise 404
*/
func routeKeywords(w http.ResponseWriter, r *http.Request) {
	uriChunks := chunkURI(r.URL.Path)
	if r.Method != "GET" ||
		len(uriChunks) > 3 ||
		(len(uriChunks) == 3 && uriChunks[2] != "r") {
		httplog.LogRequest(r, 404)
		http.NotFound(w, r)
		return
	} else if len(uriChunks) == 1 || (len(uriChunks) == 2 && len(uriChunks[1]) == 0) {
		// Path: /k/
		// show a tag cloud!
		kc, err := du.GetKeywords()
		if err != nil {
			serverErr(w, r, err)
			return
		}
		err = ListTagCloudPage(w, kc)
		if err != nil {
			serverErr(w, r, err)
		}
		return
	}

	log.Printf("K: %s (%d)", uriChunks, len(uriChunks))

	var iter *mgo.Iter
	if uriChunks[len(uriChunks)-1] == "r" {
		// Path: /k/
		// TODO determine how to show a random image by keyword ...
		log.Println("random isn't built yet")
		httplog.LogRequest(r, 404)
		return
	} else if len(uriChunks) == 2 {
		// Path: /k/:name
		log.Println(uriChunks[1])
		iter = gfs.Find(bson.M{"metadata.keywords": uriChunks[1]}).Sort("-metadata.timestamp").Limit(defaultPageLimit).Iter()
	}

	files := []types.File{}
	err := iter.All(&files)
	if err != nil {
		serverErr(w, r, err)
		return
	}
	log.Printf("collected %d files", len(files))
	err = ListFilesPage(w, files)
	if err != nil {
		log.Printf("error: %s", err)
	}

	httplog.LogRequest(r, 200)
}
Example #4
0
// Articles given a categoryID and count returns a list of articles.
// Count of 0 means all
func (d *DB) Articles(categoryID uint16, count int, user *User) ([]*Article, error) {
	results := []*Article{}
	query := bson.M{"categoryid": categoryID}

	var iter *mgo.Iter
	switch count {
	case 0:
		iter = d.Items.Find(query).Iter()
	default:
		iter = d.Items.Find(bson.M{"categoryid": categoryID}).Limit(count).Iter()
	}
	err := iter.All(&results)
	return results, err
}
Example #5
0
// GetCategories gets a list of all currently loaded
// categories.
func (d *DB) GetCategories() ([]*Category, error) {
	results := []*Category{}
	var iter *mgo.Iter = d.Categories.Find(nil).Iter()
	err := iter.All(&results)
	return results, err
}
Example #6
0
File: mongo.go Project: pkar/pfftdb
// Triples
func (m *Mongo) Triples(graph, sub, pred string, obj interface{}, options *Options) []*Triple {
	g, ok := m.Graphs[graph]
	if !ok {
		log.Error("missing graph ", graph)
		return nil
	}
	sessionCopy := m.Session.Copy()
	defer sessionCopy.Close()
	col := sessionCopy.DB(m.DBName).C(g.ColName)

	var query bson.M
	if options != nil {
		query = m.BuildQuery(graph, sub, pred, obj, options.TripleOverrides)
	} else {
		query = m.BuildQuery(graph, sub, pred, obj, nil)
	}
	// Note that skip only makes sense in the case of sorted results, so if
	// no orderby is given a default subject is used.
	var iter *mgo.Iter
	switch {
	default:
		// no options
		iter = col.Find(query).Iter()
	case options == nil:
		// no options
		iter = col.Find(query).Iter()
	case options.Limit == 0 && options.Offset == 0 && options.OrderBy == "":
		// no options
		iter = col.Find(query).Iter()
	case options.Limit != 0 && options.Offset != 0 && options.OrderBy != "":
		// limit,  orderby
		iter = col.Find(query).Limit(int(options.Limit)).Skip(int(options.Offset)).Sort(options.OrderBy).Iter()
	case options.Limit != 0 && options.Offset != 0 && options.OrderBy == "":
		// limit, skip
		iter = col.Find(query).Limit(int(options.Limit)).Skip(int(options.Offset)).Sort("s").Iter()
	case options.Limit != 0 && options.Offset == 0 && options.OrderBy != "":
		// limit, orderby
		iter = col.Find(query).Limit(int(options.Limit)).Sort(options.OrderBy).Iter()
	case options.Limit != 0 && options.Offset == 0 && options.OrderBy == "":
		// limit
		iter = col.Find(query).Limit(int(options.Limit)).Iter()
	case options.Limit == 0 && options.Offset != 0 && options.OrderBy != "":
		// skip, orderby
		iter = col.Find(query).Skip(int(options.Offset)).Sort(options.OrderBy).Iter()
	case options.Limit == 0 && options.Offset != 0 && options.OrderBy == "":
		// skip
		iter = col.Find(query).Skip(int(options.Offset)).Sort("s").Iter()
	case options.Limit == 0 && options.Offset == 0 && options.OrderBy != "":
		// orderby
		iter = col.Find(query).Sort(options.OrderBy).Iter()
	}

	tripleDocs := []*TripleDoc{}
	err := iter.All(&tripleDocs)
	if err != nil {
		log.Error(err)
		return nil
	}
	results := []*Triple{}
	for _, res := range tripleDocs {
		results = append(results, &Triple{res.Sub, res.Pred, res.Obj})
	}

	return results
}