2011-03-13 23:38:32 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package search
|
|
|
|
|
|
|
|
import (
|
2011-07-03 00:16:30 +00:00
|
|
|
"bytes"
|
2013-02-08 05:55:17 +00:00
|
|
|
"errors"
|
2011-03-13 23:38:32 +00:00
|
|
|
"fmt"
|
2011-03-14 03:51:58 +00:00
|
|
|
"log"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2011-03-14 00:14:48 +00:00
|
|
|
"os"
|
2011-03-14 03:51:58 +00:00
|
|
|
"sort"
|
2011-08-25 13:15:38 +00:00
|
|
|
"strconv"
|
2011-05-30 22:44:25 +00:00
|
|
|
"strings"
|
2011-03-14 02:27:59 +00:00
|
|
|
"sync"
|
2011-03-14 00:14:48 +00:00
|
|
|
"time"
|
2011-05-30 05:52:31 +00:00
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/blobref"
|
|
|
|
"camlistore.org/pkg/blobserver"
|
|
|
|
"camlistore.org/pkg/httputil"
|
|
|
|
"camlistore.org/pkg/jsonconfig"
|
2011-03-13 23:38:32 +00:00
|
|
|
)
|
|
|
|
|
2011-07-06 00:27:10 +00:00
|
|
|
const buffered = 32 // arbitrary channel buffer size
|
2012-11-09 18:00:47 +00:00
|
|
|
const maxPermanodes = 50 // arbitrary limit on the number of permanodes fetched
|
2011-07-02 15:15:00 +00:00
|
|
|
|
2011-05-30 05:52:31 +00:00
|
|
|
func init() {
|
|
|
|
blobserver.RegisterHandlerConstructor("search", newHandlerFromConfig)
|
|
|
|
}
|
|
|
|
|
2011-06-17 03:45:47 +00:00
|
|
|
type Handler struct {
|
2011-05-30 05:52:31 +00:00
|
|
|
index Index
|
|
|
|
owner *blobref.BlobRef
|
|
|
|
}
|
|
|
|
|
2013-02-08 05:34:25 +00:00
|
|
|
// IGetRecentPermanodes is the interface encapsulating the GetRecentPermanodes query.
|
|
|
|
type IGetRecentPermanodes interface {
|
|
|
|
// GetRecentPermanodes returns recently-modified permanodes.
|
|
|
|
// This is a higher-level query returning more metadata than the index.GetRecentPermanodes,
|
|
|
|
// which only scans the blobrefs but doesn't return anything about the permanodes.
|
|
|
|
// TODO: rename this one?
|
|
|
|
GetRecentPermanodes(*RecentRequest) (*RecentResponse, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ IGetRecentPermanodes = (*Handler)(nil)
|
|
|
|
)
|
|
|
|
|
2011-07-06 18:20:25 +00:00
|
|
|
func NewHandler(index Index, owner *blobref.BlobRef) *Handler {
|
2012-11-05 14:48:13 +00:00
|
|
|
return &Handler{index: index, owner: owner}
|
2011-07-06 18:20:25 +00:00
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func newHandlerFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (http.Handler, error) {
|
2011-05-30 05:52:31 +00:00
|
|
|
indexPrefix := conf.RequiredString("index") // TODO: add optional help tips here?
|
|
|
|
ownerBlobStr := conf.RequiredString("owner")
|
2012-11-07 21:40:17 +00:00
|
|
|
devBlockStartupPrefix := conf.OptionalString("devBlockStartupOn", "")
|
2011-05-30 05:52:31 +00:00
|
|
|
if err := conf.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2012-11-07 21:40:17 +00:00
|
|
|
if devBlockStartupPrefix != "" {
|
|
|
|
_, err := ld.GetHandler(devBlockStartupPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("search handler references bogus devBlockStartupOn handler %s: %v", devBlockStartupPrefix, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-30 05:52:31 +00:00
|
|
|
indexHandler, err := ld.GetHandler(indexPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("search config references unknown handler %q", indexPrefix)
|
2011-03-13 23:38:32 +00:00
|
|
|
}
|
2011-05-30 05:52:31 +00:00
|
|
|
indexer, ok := indexHandler.(Index)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("search config references invalid indexer %q (actually a %T)", indexPrefix, indexHandler)
|
|
|
|
}
|
|
|
|
ownerBlobRef := blobref.Parse(ownerBlobStr)
|
|
|
|
if ownerBlobRef == nil {
|
|
|
|
return nil, fmt.Errorf("search 'owner' has malformed blobref %q; expecting e.g. sha1-xxxxxxxxxxxx",
|
|
|
|
ownerBlobStr)
|
|
|
|
}
|
2011-06-17 03:45:47 +00:00
|
|
|
return &Handler{
|
2011-05-30 05:52:31 +00:00
|
|
|
index: indexer,
|
|
|
|
owner: ownerBlobRef,
|
|
|
|
}, nil
|
2011-03-13 23:38:32 +00:00
|
|
|
}
|
|
|
|
|
2011-06-23 19:11:01 +00:00
|
|
|
// TODO: figure out a plan for an owner having multiple active public keys, or public
|
|
|
|
// key rotation
|
|
|
|
func (h *Handler) Owner() *blobref.BlobRef {
|
|
|
|
return h.owner
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) Index() Index {
|
|
|
|
return h.index
|
|
|
|
}
|
2011-03-14 00:14:48 +00:00
|
|
|
|
2011-06-09 19:55:38 +00:00
|
|
|
func jsonMap() map[string]interface{} {
|
|
|
|
return make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonMapList() []map[string]interface{} {
|
|
|
|
return make([]map[string]interface{}, 0)
|
|
|
|
}
|
2011-03-14 02:27:59 +00:00
|
|
|
|
2011-06-17 03:45:47 +00:00
|
|
|
func (sh *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2011-06-30 23:16:08 +00:00
|
|
|
ret := jsonMap()
|
2011-05-30 22:44:25 +00:00
|
|
|
_ = req.Header.Get("X-PrefixHandler-PathBase")
|
|
|
|
suffix := req.Header.Get("X-PrefixHandler-PathSuffix")
|
|
|
|
|
2011-06-09 19:55:38 +00:00
|
|
|
if req.Method == "GET" {
|
|
|
|
switch suffix {
|
2011-06-23 04:59:17 +00:00
|
|
|
case "camli/search/recent":
|
2011-06-09 19:55:38 +00:00
|
|
|
sh.serveRecentPermanodes(rw, req)
|
|
|
|
return
|
2011-09-03 22:48:49 +00:00
|
|
|
case "camli/search/permanodeattr":
|
|
|
|
sh.servePermanodesWithAttr(rw, req)
|
2011-08-25 13:15:38 +00:00
|
|
|
return
|
2011-06-09 19:55:38 +00:00
|
|
|
case "camli/search/describe":
|
|
|
|
sh.serveDescribe(rw, req)
|
|
|
|
return
|
|
|
|
case "camli/search/claims":
|
|
|
|
sh.serveClaims(rw, req)
|
|
|
|
return
|
|
|
|
case "camli/search/files":
|
|
|
|
sh.serveFiles(rw, req)
|
|
|
|
return
|
2011-06-23 05:21:18 +00:00
|
|
|
case "camli/search/signerattrvalue":
|
|
|
|
sh.serveSignerAttrValue(rw, req)
|
|
|
|
return
|
2011-06-26 00:50:38 +00:00
|
|
|
case "camli/search/signerpaths":
|
|
|
|
sh.serveSignerPaths(rw, req)
|
|
|
|
return
|
2012-11-05 14:09:34 +00:00
|
|
|
case "camli/search/edgesto":
|
|
|
|
sh.serveEdgesTo(rw, req)
|
|
|
|
return
|
2011-06-09 19:55:38 +00:00
|
|
|
}
|
2011-05-30 22:44:25 +00:00
|
|
|
}
|
|
|
|
|
2011-06-09 19:55:38 +00:00
|
|
|
// TODO: discovery for the endpoints & better error message with link to discovery info
|
|
|
|
ret["error"] = "Unsupported search path or method"
|
|
|
|
ret["errorType"] = "input"
|
2012-07-28 22:42:56 +00:00
|
|
|
httputil.ReturnJSON(rw, ret)
|
2011-05-30 22:44:25 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 05:34:25 +00:00
|
|
|
// RecentRequest is a request to get a RecentResponse.
|
|
|
|
type RecentRequest struct {
|
|
|
|
N int // if zero, default number of results
|
|
|
|
Before time.Time // if zero, now
|
|
|
|
ThumbnailSize int // if zero, no thumbnails
|
|
|
|
}
|
|
|
|
|
2013-02-08 05:55:17 +00:00
|
|
|
func (r *RecentRequest) URLSuffix() string {
|
|
|
|
// TODO: Before
|
|
|
|
return fmt.Sprintf("camli/search/recent?n=%d&thumbnails=%d", r.n(), r.thumbnailSize())
|
|
|
|
}
|
|
|
|
|
2013-02-08 05:34:25 +00:00
|
|
|
func (r *RecentRequest) FromHTTP(req *http.Request) error {
|
|
|
|
r.N, _ = strconv.Atoi(req.FormValue("n"))
|
|
|
|
r.ThumbnailSize = thumbnailSize(req)
|
|
|
|
// TODO: populate Before
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// n returns the sanitized number of search results.
|
|
|
|
func (r *RecentRequest) n() int {
|
|
|
|
if r.N <= 0 || r.N > 1000 {
|
|
|
|
return 50
|
|
|
|
}
|
|
|
|
return r.N
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RecentRequest) thumbnailSize() int {
|
|
|
|
v := r.ThumbnailSize
|
|
|
|
if v == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if v < minThumbSize || v > maxThumbSize {
|
|
|
|
return defThumbSize
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2013-02-08 17:27:23 +00:00
|
|
|
// WithAttrRequest is a request to get a WithAttrResponse.
|
|
|
|
type WithAttrRequest struct {
|
|
|
|
N int // max number of results
|
|
|
|
Signer *blobref.BlobRef // if nil, search will return index.ErrNotFound
|
|
|
|
// Requested attribute. If blank, all attributes are searched (for Value)
|
|
|
|
// as fulltext.
|
|
|
|
Attr string
|
|
|
|
// Value of the requested attribute. If blank, permanodes which have
|
|
|
|
// request.Attribute as an attribute are searched.
|
|
|
|
Value string
|
|
|
|
Fuzzy bool // fulltext search (if supported).
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *WithAttrRequest) FromHTTP(req *http.Request) error {
|
|
|
|
v := req.FormValue("signer")
|
|
|
|
if v == "" {
|
|
|
|
return errors.New("missing required parameter: \"signer\"")
|
|
|
|
}
|
|
|
|
r.Signer = blobref.Parse(v)
|
|
|
|
if r.Signer == nil {
|
|
|
|
return fmt.Errorf("failed to parse signer blobref: %v", v)
|
|
|
|
}
|
|
|
|
r.Value = req.FormValue("value")
|
|
|
|
fuzzy := req.FormValue("fuzzy") // exact match if empty
|
|
|
|
fuzzyMatch := false
|
|
|
|
if fuzzy != "" {
|
|
|
|
lowered := strings.ToLower(fuzzy)
|
|
|
|
if lowered == "true" || lowered == "t" {
|
|
|
|
fuzzyMatch = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.Attr = req.FormValue("attr") // all attributes if empty
|
|
|
|
if r.Attr == "" { // and force fuzzy in that case.
|
|
|
|
fuzzyMatch = true
|
|
|
|
}
|
|
|
|
r.Fuzzy = fuzzyMatch
|
|
|
|
maxResults := maxPermanodes
|
|
|
|
max := req.FormValue("max")
|
|
|
|
if max != "" {
|
|
|
|
maxR, err := strconv.Atoi(max)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid specified max results 'max': " + err.Error())
|
|
|
|
}
|
|
|
|
if maxR < maxResults {
|
|
|
|
maxResults = maxR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.N = maxResults
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-02-08 05:34:25 +00:00
|
|
|
// A MetaMap is a map from blobref to a DescribedBlob.
|
|
|
|
type MetaMap map[string]*DescribedBlob
|
|
|
|
|
|
|
|
func (m MetaMap) Get(br *blobref.BlobRef) *DescribedBlob {
|
2013-02-08 06:43:50 +00:00
|
|
|
if br == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2013-02-08 05:34:25 +00:00
|
|
|
return m[br.String()]
|
|
|
|
}
|
|
|
|
|
2013-02-07 17:33:00 +00:00
|
|
|
// RecentResponse is the JSON response from $searchRoot/camli/search/recent.
|
|
|
|
type RecentResponse struct {
|
2013-02-08 05:34:25 +00:00
|
|
|
Recent []*RecentItem `json:"recent"`
|
|
|
|
Meta MetaMap `json:"meta"`
|
2013-02-08 05:55:17 +00:00
|
|
|
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
ErrorType string `json:"errorType,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RecentResponse) Err() error {
|
|
|
|
if r.Error != "" || r.ErrorType != "" {
|
|
|
|
if r.ErrorType != "" {
|
|
|
|
return fmt.Errorf("%s: %s", r.ErrorType, r.Error)
|
|
|
|
}
|
|
|
|
return errors.New(r.Error)
|
|
|
|
}
|
|
|
|
return nil
|
2013-02-07 17:33:00 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 17:27:23 +00:00
|
|
|
// DescribeResponse is the JSON response from $searchRoot/camli/search/describe.
|
|
|
|
type DescribeResponse struct {
|
|
|
|
Meta MetaMap `json:"meta"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAttrResponse is the JSON response from $searchRoot/camli/search/permanodeattr.
|
|
|
|
type WithAttrResponse struct {
|
|
|
|
WithAttr []*WithAttrItem `json:"withAttr"`
|
|
|
|
Meta MetaMap `json:"meta"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignerPathsResponse is the JSON response from $searchRoot/camli/search/signerpaths.
|
|
|
|
type SignerPathsResponse struct {
|
|
|
|
Paths []*SignerPathsItem `json:"paths"`
|
|
|
|
Meta MetaMap `json:"meta"`
|
|
|
|
}
|
|
|
|
|
2013-02-07 16:05:31 +00:00
|
|
|
// A RecentItem is an item returned from $searchRoot/camli/search/recent in the "recent" list.
|
|
|
|
type RecentItem struct {
|
|
|
|
BlobRef *blobref.BlobRef `json:"blobref"`
|
2013-02-08 17:27:23 +00:00
|
|
|
// TODO: add a type with concrete type of time.Time that implements
|
|
|
|
// encode/decodeJSON hooks to RFC3339 strings
|
2013-02-07 16:05:31 +00:00
|
|
|
ModTime string `json:"modtime"` // RFC3339
|
|
|
|
Owner *blobref.BlobRef `json:"owner"`
|
|
|
|
}
|
|
|
|
|
2013-02-08 17:27:23 +00:00
|
|
|
// A WithAttrItem is an item returned from $searchRoot/camli/search/permanodeattr.
|
|
|
|
type WithAttrItem struct {
|
|
|
|
Permanode *blobref.BlobRef `json:"permanode"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// A SignerPathsItem is an item returned from $searchRoot/camli/search/signerpaths.
|
|
|
|
type SignerPathsItem struct {
|
|
|
|
ClaimRef *blobref.BlobRef `json:"claimRef"`
|
|
|
|
BaseRef *blobref.BlobRef `json:"baseRef"`
|
|
|
|
Suffix string `json:"suffix"`
|
|
|
|
}
|
|
|
|
|
2013-02-07 17:40:59 +00:00
|
|
|
func thumbnailSize(r *http.Request) int {
|
|
|
|
return thumbnailSizeStr(r.FormValue("thumbnails"))
|
|
|
|
}
|
|
|
|
|
2013-02-08 05:34:25 +00:00
|
|
|
const (
|
|
|
|
minThumbSize = 25
|
|
|
|
defThumbSize = 50
|
|
|
|
maxThumbSize = 800
|
|
|
|
)
|
|
|
|
|
2013-02-07 17:40:59 +00:00
|
|
|
func thumbnailSizeStr(s string) int {
|
|
|
|
if s == "" {
|
|
|
|
return 0
|
|
|
|
}
|
2013-02-08 05:34:25 +00:00
|
|
|
if i, _ := strconv.Atoi(s); i >= minThumbSize && i <= maxThumbSize {
|
2013-02-07 17:40:59 +00:00
|
|
|
return i
|
|
|
|
}
|
2013-02-08 05:34:25 +00:00
|
|
|
return defThumbSize
|
2013-02-07 17:40:59 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 05:34:25 +00:00
|
|
|
func (sh *Handler) GetRecentPermanodes(req *RecentRequest) (*RecentResponse, error) {
|
2011-03-14 00:14:48 +00:00
|
|
|
ch := make(chan *Result)
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
errch := make(chan error)
|
2011-03-14 00:14:48 +00:00
|
|
|
go func() {
|
2013-02-08 05:34:25 +00:00
|
|
|
errch <- sh.index.GetRecentPermanodes(ch, sh.owner, req.n())
|
2011-03-14 00:14:48 +00:00
|
|
|
}()
|
2011-03-14 02:27:59 +00:00
|
|
|
|
2011-06-30 23:14:58 +00:00
|
|
|
dr := sh.NewDescribeRequest()
|
2011-06-12 07:20:57 +00:00
|
|
|
|
2013-02-07 16:05:31 +00:00
|
|
|
var recent []*RecentItem
|
2011-03-14 00:14:48 +00:00
|
|
|
for res := range ch {
|
2011-06-30 23:14:58 +00:00
|
|
|
dr.Describe(res.BlobRef, 2)
|
2013-02-07 16:05:31 +00:00
|
|
|
recent = append(recent, &RecentItem{
|
|
|
|
BlobRef: res.BlobRef,
|
|
|
|
Owner: res.Signer,
|
|
|
|
ModTime: time.Unix(res.LastModTime, 0).UTC().Format(time.RFC3339),
|
|
|
|
})
|
2011-03-14 00:14:48 +00:00
|
|
|
}
|
2011-03-14 02:27:59 +00:00
|
|
|
|
2013-02-08 05:34:25 +00:00
|
|
|
if err := <-errch; err != nil {
|
|
|
|
return nil, err
|
2011-03-14 02:27:59 +00:00
|
|
|
}
|
2011-06-12 07:20:57 +00:00
|
|
|
|
2013-02-08 05:34:25 +00:00
|
|
|
metaMap, err := dr.metaMap(req.thumbnailSize())
|
2013-02-07 17:33:00 +00:00
|
|
|
if err != nil {
|
2013-02-08 05:34:25 +00:00
|
|
|
return nil, err
|
2013-02-07 17:33:00 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 05:34:25 +00:00
|
|
|
res := &RecentResponse{
|
2013-02-07 17:33:00 +00:00
|
|
|
Recent: recent,
|
|
|
|
Meta: metaMap,
|
2013-02-08 05:34:25 +00:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveJSONError(rw http.ResponseWriter, err error) {
|
|
|
|
ret := jsonMap()
|
|
|
|
ret["error"] = err.Error()
|
|
|
|
httputil.ReturnJSON(rw, ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *Handler) serveRecentPermanodes(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
var rr RecentRequest
|
|
|
|
if err := rr.FromHTTP(req); err != nil {
|
|
|
|
serveJSONError(rw, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res, err := sh.GetRecentPermanodes(&rr)
|
|
|
|
if err != nil {
|
|
|
|
serveJSONError(rw, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
httputil.ReturnJSON(rw, res)
|
2011-05-30 22:44:25 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 17:27:23 +00:00
|
|
|
func (sh *Handler) GetPermanodesWithAttr(req *WithAttrRequest) (*WithAttrResponse, error) {
|
2011-08-25 13:15:38 +00:00
|
|
|
ch := make(chan *blobref.BlobRef, buffered)
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
errch := make(chan error)
|
2011-08-25 13:15:38 +00:00
|
|
|
go func() {
|
2011-09-03 22:48:49 +00:00
|
|
|
errch <- sh.index.SearchPermanodesWithAttr(ch,
|
2013-02-08 17:27:23 +00:00
|
|
|
&PermanodeByAttrRequest{Attribute: req.Attr,
|
|
|
|
Query: req.Value,
|
|
|
|
Signer: req.Signer,
|
|
|
|
FuzzyMatch: req.Fuzzy,
|
|
|
|
MaxResults: req.N})
|
2011-08-25 13:15:38 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
dr := sh.NewDescribeRequest()
|
|
|
|
|
2013-02-08 17:27:23 +00:00
|
|
|
var withAttr []*WithAttrItem
|
2011-08-25 13:15:38 +00:00
|
|
|
for res := range ch {
|
|
|
|
dr.Describe(res, 2)
|
2013-02-08 17:27:23 +00:00
|
|
|
withAttr = append(withAttr, &WithAttrItem{
|
|
|
|
Permanode: res,
|
|
|
|
})
|
2011-08-25 13:15:38 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 17:27:23 +00:00
|
|
|
metaMap, err := dr.metaMap(0)
|
2011-08-25 13:15:38 +00:00
|
|
|
if err != nil {
|
2013-02-08 17:27:23 +00:00
|
|
|
return nil, err
|
2011-08-25 13:15:38 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 17:27:23 +00:00
|
|
|
res := &WithAttrResponse{
|
|
|
|
WithAttr: withAttr,
|
|
|
|
Meta: metaMap,
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// servePermanodesWithAttr uses the indexer to search for the permanodes matching
|
|
|
|
// the request.
|
|
|
|
// The valid values for the "attr" key in the request (i.e the only attributes
|
|
|
|
// for a permanode which are actually indexed as such) are "tag" and "title".
|
|
|
|
func (sh *Handler) servePermanodesWithAttr(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
var wr WithAttrRequest
|
|
|
|
if err := wr.FromHTTP(req); err != nil {
|
|
|
|
serveJSONError(rw, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res, err := sh.GetPermanodesWithAttr(&wr)
|
|
|
|
if err != nil {
|
|
|
|
serveJSONError(rw, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
httputil.ReturnJSON(rw, res)
|
2011-08-25 13:15:38 +00:00
|
|
|
}
|
|
|
|
|
2011-06-17 03:45:47 +00:00
|
|
|
func (sh *Handler) serveClaims(rw http.ResponseWriter, req *http.Request) {
|
2011-06-09 19:55:38 +00:00
|
|
|
ret := jsonMap()
|
2011-06-03 22:23:23 +00:00
|
|
|
|
|
|
|
pn := blobref.Parse(req.FormValue("permanode"))
|
|
|
|
if pn == nil {
|
|
|
|
http.Error(rw, "Missing or invalid 'permanode' param", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: rename GetOwnerClaims to GetClaims?
|
|
|
|
claims, err := sh.index.GetOwnerClaims(pn, sh.owner)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error getting claims of %s: %v", pn.String(), err)
|
|
|
|
} else {
|
|
|
|
sort.Sort(claims)
|
2011-06-09 19:55:38 +00:00
|
|
|
jclaims := jsonMapList()
|
2011-06-03 22:23:23 +00:00
|
|
|
|
|
|
|
for _, claim := range claims {
|
2011-06-09 19:55:38 +00:00
|
|
|
jclaim := jsonMap()
|
2011-06-03 22:23:23 +00:00
|
|
|
jclaim["blobref"] = claim.BlobRef.String()
|
|
|
|
jclaim["signer"] = claim.Signer.String()
|
|
|
|
jclaim["permanode"] = claim.Permanode.String()
|
|
|
|
jclaim["date"] = claim.Date.Format(time.RFC3339)
|
|
|
|
jclaim["type"] = claim.Type
|
|
|
|
if claim.Attr != "" {
|
|
|
|
jclaim["attr"] = claim.Attr
|
|
|
|
}
|
|
|
|
if claim.Value != "" {
|
|
|
|
jclaim["value"] = claim.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
jclaims = append(jclaims, jclaim)
|
|
|
|
}
|
|
|
|
ret["claims"] = jclaims
|
|
|
|
}
|
|
|
|
|
2012-07-28 22:42:56 +00:00
|
|
|
httputil.ReturnJSON(rw, ret)
|
2011-06-03 22:23:23 +00:00
|
|
|
}
|
|
|
|
|
2011-06-30 23:14:58 +00:00
|
|
|
type DescribeRequest struct {
|
2011-06-17 03:45:47 +00:00
|
|
|
sh *Handler
|
2011-06-12 07:20:57 +00:00
|
|
|
|
2012-11-09 18:00:47 +00:00
|
|
|
mu sync.Mutex // protects following:
|
2013-02-08 05:34:25 +00:00
|
|
|
m MetaMap
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
done map[string]bool // blobref -> described
|
|
|
|
errs map[string]error // blobref -> error
|
2011-06-12 07:20:57 +00:00
|
|
|
|
|
|
|
wg *sync.WaitGroup // for load requests
|
|
|
|
}
|
|
|
|
|
2011-06-30 23:25:18 +00:00
|
|
|
type DescribedBlob struct {
|
2013-02-07 17:33:00 +00:00
|
|
|
Request *DescribeRequest `json:"-"`
|
2011-07-03 00:16:30 +00:00
|
|
|
|
2013-02-07 17:33:00 +00:00
|
|
|
BlobRef *blobref.BlobRef `json:"blobRef"`
|
|
|
|
MimeType string `json:"mimeType"`
|
|
|
|
CamliType string `json:"camliType"`
|
|
|
|
Size int64 `json:"size,"`
|
2011-06-30 23:25:18 +00:00
|
|
|
|
2011-07-01 21:33:15 +00:00
|
|
|
// if camliType "permanode"
|
2013-02-07 17:33:00 +00:00
|
|
|
Permanode *DescribedPermanode `json:"permanode,omitempty"`
|
2011-07-01 21:33:15 +00:00
|
|
|
|
|
|
|
// if camliType "file"
|
2013-02-07 17:33:00 +00:00
|
|
|
File *FileInfo `json:"file,omitempty"`
|
2013-01-09 15:58:20 +00:00
|
|
|
// if camliType "directory"
|
2013-02-07 17:33:00 +00:00
|
|
|
Dir *FileInfo `json:"dir,omitempty"`
|
|
|
|
|
|
|
|
Thumbnail string `json:"thumbnailSrc,omitempty"`
|
|
|
|
ThumbnailWidth int `json:"thumbnailWidth,omitempty"`
|
|
|
|
ThumbnailHeight int `json:"thumbnailHeight,omitempty"`
|
2011-07-03 00:16:30 +00:00
|
|
|
|
2013-02-07 17:33:00 +00:00
|
|
|
// Stub is set if this is not loaded, but referenced.
|
|
|
|
Stub bool `json:"-"`
|
2011-07-03 00:16:30 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 06:32:42 +00:00
|
|
|
// PermanodeFile returns the blobref path from this permanode to its
|
|
|
|
// File camliContent, else (nil, false)
|
|
|
|
func (b *DescribedBlob) PermanodeFile() (path []*blobref.BlobRef, fi *FileInfo, ok bool) {
|
|
|
|
if b == nil || b.Permanode == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if contentRef := b.Permanode.Attr.Get("camliContent"); contentRef != "" {
|
|
|
|
if cdes := b.Request.DescribedBlobStr(contentRef); cdes != nil && cdes.File != nil {
|
|
|
|
return []*blobref.BlobRef{b.BlobRef, cdes.BlobRef}, cdes.File, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-07-06 00:27:10 +00:00
|
|
|
func (b *DescribedBlob) DomID() string {
|
|
|
|
if b == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return b.BlobRef.DomID()
|
|
|
|
}
|
|
|
|
|
2011-07-03 00:16:30 +00:00
|
|
|
func (b *DescribedBlob) Title() string {
|
|
|
|
if b == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if b.Permanode != nil {
|
|
|
|
if t := b.Permanode.Attr.Get("title"); t != "" {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
if contentRef := b.Permanode.Attr.Get("camliContent"); contentRef != "" {
|
|
|
|
return b.Request.DescribedBlobStr(contentRef).Title()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if b.File != nil {
|
|
|
|
return b.File.FileName
|
|
|
|
}
|
2013-01-09 15:58:20 +00:00
|
|
|
if b.Dir != nil {
|
|
|
|
return b.Dir.FileName
|
|
|
|
}
|
2011-07-03 00:16:30 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *DescribedBlob) Description() string {
|
|
|
|
if b == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if b.Permanode != nil {
|
|
|
|
return b.Permanode.Attr.Get("description")
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *DescribedBlob) Members() []*DescribedBlob {
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
m := make([]*DescribedBlob, 0)
|
|
|
|
if b.Permanode != nil {
|
|
|
|
for _, bstr := range b.Permanode.Attr["camliMember"] {
|
|
|
|
if br := blobref.Parse(bstr); br != nil {
|
2011-07-03 19:28:39 +00:00
|
|
|
m = append(m, b.PeerBlob(br))
|
2011-07-03 00:16:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2011-07-06 00:27:10 +00:00
|
|
|
func (b *DescribedBlob) ContentRef() (br *blobref.BlobRef, ok bool) {
|
|
|
|
if b != nil && b.Permanode != nil {
|
|
|
|
if cref := b.Permanode.Attr.Get("camliContent"); cref != "" {
|
|
|
|
br = blobref.Parse(cref)
|
|
|
|
return br, br != nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-11-09 18:00:47 +00:00
|
|
|
// Given a blobref string returns a Description or nil.
|
|
|
|
// dr may be nil itself.
|
|
|
|
func (dr *DescribeRequest) DescribedBlobStr(blobstr string) *DescribedBlob {
|
|
|
|
if dr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dr.mu.Lock()
|
|
|
|
defer dr.mu.Unlock()
|
|
|
|
return dr.m[blobstr]
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeerBlob returns a DescribedBlob for the provided blobref.
|
|
|
|
//
|
|
|
|
// Unlike DescribedBlobStr, the returned DescribedBlob is never nil.
|
|
|
|
//
|
|
|
|
// If the blob was never loaded along with the the receiver (or if the
|
|
|
|
// receiver is nil), a stub DescribedBlob is returned with its Stub
|
|
|
|
// field set true.
|
2011-07-03 19:28:39 +00:00
|
|
|
func (b *DescribedBlob) PeerBlob(br *blobref.BlobRef) *DescribedBlob {
|
2011-07-03 00:16:30 +00:00
|
|
|
if b.Request == nil {
|
|
|
|
return &DescribedBlob{BlobRef: br, Stub: true}
|
|
|
|
}
|
2012-11-09 18:00:47 +00:00
|
|
|
b.Request.mu.Lock()
|
|
|
|
defer b.Request.mu.Unlock()
|
|
|
|
return b.peerBlob(br)
|
|
|
|
}
|
|
|
|
|
|
|
|
// version of PeerBlob when b.Request.mu is already held.
|
|
|
|
func (b *DescribedBlob) peerBlob(br *blobref.BlobRef) *DescribedBlob {
|
2011-07-03 00:16:30 +00:00
|
|
|
if peer, ok := b.Request.m[br.String()]; ok {
|
|
|
|
return peer
|
|
|
|
}
|
|
|
|
return &DescribedBlob{Request: b.Request, BlobRef: br, Stub: true}
|
2011-06-30 23:25:18 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 19:28:39 +00:00
|
|
|
// HasSecureLinkTo returns true if there's a valid link from this blob
|
|
|
|
// to the other blob. This is used in access control (hence the
|
|
|
|
// somewhat redundant "Secure" in the name) and should be paranoid
|
|
|
|
// against e.g. random user/attacker-control attributes making links
|
|
|
|
// to other blobs.
|
2011-07-07 01:38:27 +00:00
|
|
|
//
|
|
|
|
// TODO: don't linear scan here. rewrite this in terms of ResolvePrefixHop,
|
|
|
|
// passing down some policy perhaps? or maybe that's enough.
|
2011-07-03 19:28:39 +00:00
|
|
|
func (b *DescribedBlob) HasSecureLinkTo(other *blobref.BlobRef) bool {
|
|
|
|
if b == nil || other == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ostr := other.String()
|
|
|
|
if b.Permanode != nil {
|
|
|
|
if b.Permanode.Attr.Get("camliContent") == ostr {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, mstr := range b.Permanode.Attr["camliMember"] {
|
|
|
|
if mstr == ostr {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2012-11-05 15:32:56 +00:00
|
|
|
func (b *DescribedBlob) isPermanode() bool {
|
|
|
|
return b.Permanode != nil
|
|
|
|
}
|
|
|
|
|
2012-11-05 16:23:42 +00:00
|
|
|
// returns a path relative to the UI handler.
|
2012-11-09 18:00:47 +00:00
|
|
|
//
|
|
|
|
// Locking: requires that DescribedRequest is done loading or that
|
2013-02-08 17:27:23 +00:00
|
|
|
// Request.mu is held (as it is from metaMap)
|
2012-11-05 16:23:42 +00:00
|
|
|
func (b *DescribedBlob) thumbnail(thumbSize int) (path string, width, height int, ok bool) {
|
|
|
|
if thumbSize <= 0 || !b.isPermanode() {
|
|
|
|
return
|
|
|
|
}
|
2012-11-05 17:01:03 +00:00
|
|
|
if b.Stub {
|
|
|
|
return "node.png", thumbSize, thumbSize, true
|
|
|
|
}
|
|
|
|
pnAttr := b.Permanode.Attr
|
|
|
|
|
|
|
|
if members := pnAttr["camliMember"]; len(members) > 0 {
|
|
|
|
return "folder.png", thumbSize, thumbSize, true
|
|
|
|
}
|
|
|
|
|
|
|
|
if content, ok := b.ContentRef(); ok {
|
2012-11-09 18:00:47 +00:00
|
|
|
peer := b.peerBlob(content)
|
2012-12-07 01:36:23 +00:00
|
|
|
if peer.File != nil {
|
|
|
|
if peer.File.IsImage() {
|
|
|
|
image := fmt.Sprintf("thumbnail/%s/%s?mw=%d&mh=%d", peer.BlobRef,
|
|
|
|
url.QueryEscape(peer.File.FileName), thumbSize, thumbSize)
|
|
|
|
// TODO: return the correct thumbSizes here,
|
|
|
|
// once we know from the indexer the
|
|
|
|
// dimensions (after correction for exif
|
|
|
|
// orientation). For now the thumbnails will
|
|
|
|
// all be stretched fat squares.
|
|
|
|
return image, thumbSize, thumbSize, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: different thumbnails based on peer.File.MimeType.
|
|
|
|
return "file.png", thumbSize, thumbSize, true
|
2012-11-05 17:01:03 +00:00
|
|
|
}
|
2013-01-09 15:58:20 +00:00
|
|
|
if peer.Dir != nil {
|
|
|
|
return "folder.png", thumbSize, thumbSize, true
|
|
|
|
}
|
2012-11-05 17:01:03 +00:00
|
|
|
}
|
2012-11-05 16:23:42 +00:00
|
|
|
|
|
|
|
return "node.png", thumbSize, thumbSize, true
|
|
|
|
}
|
|
|
|
|
2013-02-07 17:33:00 +00:00
|
|
|
// TODO: delete this function
|
2011-07-01 21:33:15 +00:00
|
|
|
func (b *DescribedBlob) jsonMap() map[string]interface{} {
|
|
|
|
m := jsonMap()
|
|
|
|
m["blobRef"] = b.BlobRef.String()
|
|
|
|
if b.MimeType != "" {
|
|
|
|
m["mimeType"] = b.MimeType
|
|
|
|
}
|
|
|
|
if b.CamliType != "" {
|
|
|
|
m["camliType"] = b.CamliType
|
|
|
|
}
|
|
|
|
m["size"] = b.Size
|
|
|
|
if b.Permanode != nil {
|
|
|
|
m["permanode"] = b.Permanode.jsonMap()
|
|
|
|
}
|
|
|
|
if b.File != nil {
|
|
|
|
m["file"] = b.File
|
|
|
|
}
|
2013-01-09 15:58:20 +00:00
|
|
|
if b.Dir != nil {
|
|
|
|
m["dir"] = b.Dir
|
|
|
|
}
|
2011-07-01 21:33:15 +00:00
|
|
|
return m
|
2011-06-30 23:25:18 +00:00
|
|
|
}
|
|
|
|
|
2011-07-01 21:33:15 +00:00
|
|
|
type DescribedPermanode struct {
|
2013-02-07 17:33:00 +00:00
|
|
|
Attr url.Values `json:"attr"` // a map[string][]string
|
2011-06-30 23:25:18 +00:00
|
|
|
}
|
|
|
|
|
2011-07-01 21:33:15 +00:00
|
|
|
func (dp *DescribedPermanode) jsonMap() map[string]interface{} {
|
|
|
|
m := jsonMap()
|
|
|
|
|
|
|
|
am := jsonMap()
|
|
|
|
m["attr"] = am
|
|
|
|
for k, vv := range dp.Attr {
|
|
|
|
if len(vv) > 0 {
|
|
|
|
vl := make([]string, len(vv))
|
|
|
|
copy(vl[:], vv[:])
|
|
|
|
am[k] = vl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
2011-07-02 02:36:22 +00:00
|
|
|
|
|
|
|
// NewDescribeRequest returns a new DescribeRequest holding the state
|
|
|
|
// of blobs and their summarized descriptions. Use DescribeBlob
|
2013-02-07 17:33:00 +00:00
|
|
|
// one or more times before calling Result.
|
2011-06-30 23:14:58 +00:00
|
|
|
func (sh *Handler) NewDescribeRequest() *DescribeRequest {
|
|
|
|
return &DescribeRequest{
|
2011-06-30 23:25:18 +00:00
|
|
|
sh: sh,
|
2013-02-08 05:34:25 +00:00
|
|
|
m: make(MetaMap),
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
errs: make(map[string]error),
|
2011-06-30 23:25:18 +00:00
|
|
|
wg: new(sync.WaitGroup),
|
2011-06-30 23:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 01:38:27 +00:00
|
|
|
// Given a blobref and a few hex characters of the digest of the next hop, return the complete
|
|
|
|
// blobref of the prefix, if that's a valid next hop.
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (sh *Handler) ResolvePrefixHop(parent *blobref.BlobRef, prefix string) (child *blobref.BlobRef, err error) {
|
2011-07-06 00:27:10 +00:00
|
|
|
// TODO: this is a linear scan right now. this should be
|
|
|
|
// optimized to use a new database table of members so this is
|
|
|
|
// a quick lookup. in the meantime it should be in memcached
|
|
|
|
// at least.
|
|
|
|
if len(prefix) < 8 {
|
|
|
|
return nil, fmt.Errorf("Member prefix %q too small", prefix)
|
|
|
|
}
|
|
|
|
dr := sh.NewDescribeRequest()
|
|
|
|
dr.Describe(parent, 1)
|
|
|
|
res, err := dr.Result()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
des, ok := res[parent.String()]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Failed to describe member %q in parent %q", prefix, parent)
|
|
|
|
}
|
2011-07-07 01:38:27 +00:00
|
|
|
if des.Permanode != nil {
|
|
|
|
if cr, ok := des.ContentRef(); ok && strings.HasPrefix(cr.Digest(), prefix) {
|
|
|
|
return cr, nil
|
|
|
|
}
|
|
|
|
for _, member := range des.Members() {
|
|
|
|
if strings.HasPrefix(member.BlobRef.Digest(), prefix) {
|
|
|
|
return member.BlobRef, nil
|
|
|
|
}
|
2011-07-06 00:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Member prefix %q not found in %q", prefix, parent)
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
type DescribeError map[string]error
|
2011-07-02 02:36:22 +00:00
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (de DescribeError) Error() string {
|
2011-07-03 00:16:30 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
for b, err := range de {
|
|
|
|
fmt.Fprintf(&buf, "%s: %v; ", b, err)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Errors (%d) describing blobs: %s", len(de), buf.String())
|
2011-07-02 02:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Result waits for all outstanding lookups to complete and
|
|
|
|
// returns the map of blobref (strings) to their described
|
|
|
|
// results. The returned error is non-nil if any errors
|
2011-07-03 00:16:30 +00:00
|
|
|
// occured, and will be of type DescribeError.
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (dr *DescribeRequest) Result() (desmap map[string]*DescribedBlob, err error) {
|
2011-07-02 02:36:22 +00:00
|
|
|
dr.wg.Wait()
|
|
|
|
// TODO: set "done" / locked flag, so no more DescribeBlob can
|
|
|
|
// be called.
|
|
|
|
if len(dr.errs) > 0 {
|
2011-07-03 00:16:30 +00:00
|
|
|
return dr.m, DescribeError(dr.errs)
|
2011-07-02 02:36:22 +00:00
|
|
|
}
|
2011-07-03 00:16:30 +00:00
|
|
|
return dr.m, nil
|
2011-07-02 02:36:22 +00:00
|
|
|
}
|
|
|
|
|
2013-02-07 17:33:00 +00:00
|
|
|
func (dr *DescribeRequest) metaMap(thumbSize int) (map[string]*DescribedBlob, error) {
|
2013-02-08 17:27:23 +00:00
|
|
|
// thumbSize of zero means to not include the thumbnails.
|
2013-02-07 17:33:00 +00:00
|
|
|
dr.wg.Wait()
|
|
|
|
dr.mu.Lock()
|
|
|
|
defer dr.mu.Unlock()
|
|
|
|
for k, err := range dr.errs {
|
|
|
|
// TODO: include all?
|
|
|
|
return nil, fmt.Errorf("error populating %s: %v", k, err)
|
|
|
|
}
|
|
|
|
m := make(map[string]*DescribedBlob)
|
|
|
|
for k, desb := range dr.m {
|
|
|
|
m[k] = desb
|
|
|
|
if src, w, h, ok := desb.thumbnail(thumbSize); ok {
|
|
|
|
desb.Thumbnail = src
|
|
|
|
desb.ThumbnailWidth = w
|
|
|
|
desb.ThumbnailHeight = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2011-07-01 21:33:15 +00:00
|
|
|
func (dr *DescribeRequest) describedBlob(b *blobref.BlobRef) *DescribedBlob {
|
2012-11-09 18:00:47 +00:00
|
|
|
dr.mu.Lock()
|
|
|
|
defer dr.mu.Unlock()
|
2011-06-12 07:20:57 +00:00
|
|
|
bs := b.String()
|
2011-07-01 21:33:15 +00:00
|
|
|
if des, ok := dr.m[bs]; ok {
|
|
|
|
return des
|
2011-06-12 07:20:57 +00:00
|
|
|
}
|
2011-07-03 00:16:30 +00:00
|
|
|
des := &DescribedBlob{Request: dr, BlobRef: b}
|
2011-07-01 21:33:15 +00:00
|
|
|
dr.m[bs] = des
|
|
|
|
return des
|
2011-06-12 07:20:57 +00:00
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (dr *DescribeRequest) DescribeSync(br *blobref.BlobRef) (*DescribedBlob, error) {
|
2011-07-06 00:27:10 +00:00
|
|
|
dr.Describe(br, 1)
|
|
|
|
res, err := dr.Result()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res[br.String()], nil
|
|
|
|
}
|
|
|
|
|
2011-06-30 23:14:58 +00:00
|
|
|
func (dr *DescribeRequest) Describe(br *blobref.BlobRef, depth int) {
|
2011-06-12 07:20:57 +00:00
|
|
|
if depth <= 0 {
|
|
|
|
return
|
|
|
|
}
|
2012-11-09 18:00:47 +00:00
|
|
|
dr.mu.Lock()
|
|
|
|
defer dr.mu.Unlock()
|
2011-06-12 07:20:57 +00:00
|
|
|
if dr.done == nil {
|
|
|
|
dr.done = make(map[string]bool)
|
|
|
|
}
|
|
|
|
if dr.done[br.String()] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dr.done[br.String()] = true
|
|
|
|
dr.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer dr.wg.Done()
|
|
|
|
dr.describeReally(br, depth)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (dr *DescribeRequest) addError(br *blobref.BlobRef, err error) {
|
2011-07-01 21:33:15 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2012-11-09 18:00:47 +00:00
|
|
|
dr.mu.Lock()
|
|
|
|
defer dr.mu.Unlock()
|
2011-07-01 21:33:15 +00:00
|
|
|
// TODO: append? meh.
|
|
|
|
dr.errs[br.String()] = err
|
|
|
|
}
|
|
|
|
|
2011-06-30 23:14:58 +00:00
|
|
|
func (dr *DescribeRequest) describeReally(br *blobref.BlobRef, depth int) {
|
2011-06-12 07:20:57 +00:00
|
|
|
mime, size, err := dr.sh.index.GetBlobMimeType(br)
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
if err == os.ErrNotExist {
|
2011-06-12 07:20:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2011-07-01 21:33:15 +00:00
|
|
|
dr.addError(br, err)
|
2011-06-12 07:20:57 +00:00
|
|
|
return
|
|
|
|
}
|
2011-06-30 23:25:18 +00:00
|
|
|
|
|
|
|
// TODO: convert all this in terms of
|
|
|
|
// DescribedBlob/DescribedPermanode/DescribedFile, not json
|
|
|
|
// maps. Then add JSON marhsallers to those types. Add tests.
|
2011-07-01 21:33:15 +00:00
|
|
|
des := dr.describedBlob(br)
|
|
|
|
des.setMimeType(mime)
|
|
|
|
des.Size = size
|
|
|
|
|
|
|
|
switch des.CamliType {
|
|
|
|
case "permanode":
|
|
|
|
des.Permanode = new(DescribedPermanode)
|
|
|
|
dr.populatePermanodeFields(des.Permanode, br, dr.sh.owner, depth)
|
|
|
|
case "file":
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
var err error
|
2011-07-01 21:33:15 +00:00
|
|
|
des.File, err = dr.sh.index.GetFileInfo(br)
|
|
|
|
if err != nil {
|
2013-01-20 19:12:32 +00:00
|
|
|
if os.IsNotExist(err) {
|
2013-02-08 03:20:37 +00:00
|
|
|
log.Printf("index.GetFileInfo(file %s) failed; index stale?", br)
|
2013-01-20 19:12:32 +00:00
|
|
|
} else {
|
|
|
|
dr.addError(br, err)
|
|
|
|
}
|
2011-07-01 21:33:15 +00:00
|
|
|
}
|
2013-01-09 15:58:20 +00:00
|
|
|
case "directory":
|
|
|
|
var err error
|
|
|
|
des.Dir, err = dr.sh.index.GetFileInfo(br)
|
|
|
|
if err != nil {
|
2013-01-20 19:12:32 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Printf("index.GetFileInfo(directory %s) failed; index stale?", br)
|
|
|
|
} else {
|
|
|
|
dr.addError(br, err)
|
|
|
|
}
|
2013-01-09 15:58:20 +00:00
|
|
|
}
|
2011-06-12 07:20:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-17 03:45:47 +00:00
|
|
|
func (sh *Handler) serveDescribe(rw http.ResponseWriter, req *http.Request) {
|
2011-05-30 22:44:25 +00:00
|
|
|
br := blobref.Parse(req.FormValue("blobref"))
|
|
|
|
if br == nil {
|
2013-02-08 17:27:23 +00:00
|
|
|
// TODO: make this be a 400 Bad Request HTTP type.
|
|
|
|
serveJSONError(rw, errors.New("input: missing or invalid 'blobref' param"))
|
2011-05-30 22:44:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-06-30 23:14:58 +00:00
|
|
|
dr := sh.NewDescribeRequest()
|
|
|
|
dr.Describe(br, 4)
|
2013-02-08 17:27:23 +00:00
|
|
|
metaMap, err := dr.metaMap(thumbnailSize(req))
|
|
|
|
if err != nil {
|
|
|
|
serveJSONError(rw, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
httputil.ReturnJSON(rw, &DescribeResponse{metaMap})
|
2011-03-13 23:38:32 +00:00
|
|
|
}
|
2011-03-14 02:27:59 +00:00
|
|
|
|
2011-06-17 03:45:47 +00:00
|
|
|
func (sh *Handler) serveFiles(rw http.ResponseWriter, req *http.Request) {
|
2011-06-09 19:55:38 +00:00
|
|
|
ret := jsonMap()
|
2012-07-28 22:42:56 +00:00
|
|
|
defer httputil.ReturnJSON(rw, ret)
|
2011-06-09 19:55:38 +00:00
|
|
|
|
2011-09-14 02:56:58 +00:00
|
|
|
br := blobref.Parse(req.FormValue("wholedigest"))
|
2011-06-09 19:55:38 +00:00
|
|
|
if br == nil {
|
2011-09-14 02:56:58 +00:00
|
|
|
ret["error"] = "Missing or invalid 'wholedigest' param"
|
2011-06-09 19:55:38 +00:00
|
|
|
ret["errorType"] = "input"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := sh.index.ExistingFileSchemas(br)
|
|
|
|
if err != nil {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
ret["error"] = err.Error()
|
2011-06-09 19:55:38 +00:00
|
|
|
ret["errorType"] = "server"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
strList := []string{}
|
|
|
|
for _, br := range files {
|
|
|
|
strList = append(strList, br.String())
|
|
|
|
}
|
|
|
|
ret["files"] = strList
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-07-01 21:33:15 +00:00
|
|
|
func (dr *DescribeRequest) populatePermanodeFields(pi *DescribedPermanode, pn, signer *blobref.BlobRef, depth int) {
|
2011-08-25 15:14:47 +00:00
|
|
|
pi.Attr = make(url.Values)
|
2011-07-01 21:33:15 +00:00
|
|
|
attr := pi.Attr
|
2011-03-14 03:51:58 +00:00
|
|
|
|
2011-06-12 07:20:57 +00:00
|
|
|
claims, err := dr.sh.index.GetOwnerClaims(pn, signer)
|
2011-03-14 03:51:58 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error getting claims of %s: %v", pn.String(), err)
|
2011-07-01 21:33:15 +00:00
|
|
|
dr.addError(pn, fmt.Errorf("Error getting claims of %s: %v", pn.String(), err))
|
2011-06-11 16:51:08 +00:00
|
|
|
return
|
|
|
|
}
|
2011-06-12 07:20:57 +00:00
|
|
|
|
2011-06-11 16:51:08 +00:00
|
|
|
sort.Sort(claims)
|
|
|
|
claimLoop:
|
|
|
|
for _, cl := range claims {
|
|
|
|
switch cl.Type {
|
|
|
|
case "del-attribute":
|
|
|
|
if cl.Value == "" {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
delete(attr, cl.Attr)
|
2011-06-11 16:51:08 +00:00
|
|
|
} else {
|
2011-07-01 21:33:15 +00:00
|
|
|
sl := attr[cl.Attr]
|
|
|
|
filtered := make([]string, 0, len(sl))
|
|
|
|
for _, val := range sl {
|
|
|
|
if val != cl.Value {
|
|
|
|
filtered = append(filtered, val)
|
2011-06-04 17:18:38 +00:00
|
|
|
}
|
2011-06-11 16:51:08 +00:00
|
|
|
}
|
2011-07-01 21:33:15 +00:00
|
|
|
attr[cl.Attr] = filtered
|
2011-06-11 16:51:08 +00:00
|
|
|
}
|
|
|
|
case "set-attribute":
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
delete(attr, cl.Attr)
|
2011-06-11 16:51:08 +00:00
|
|
|
fallthrough
|
|
|
|
case "add-attribute":
|
|
|
|
if cl.Value == "" {
|
|
|
|
continue
|
|
|
|
}
|
2011-07-01 21:33:15 +00:00
|
|
|
sl, ok := attr[cl.Attr]
|
2011-06-11 16:51:08 +00:00
|
|
|
if ok {
|
|
|
|
for _, exist := range sl {
|
|
|
|
if exist == cl.Value {
|
|
|
|
continue claimLoop
|
|
|
|
}
|
2011-03-14 03:51:58 +00:00
|
|
|
}
|
2011-06-11 16:51:08 +00:00
|
|
|
} else {
|
|
|
|
sl = make([]string, 0, 1)
|
|
|
|
attr[cl.Attr] = sl
|
2011-03-14 03:51:58 +00:00
|
|
|
}
|
2011-06-11 16:51:08 +00:00
|
|
|
attr[cl.Attr] = append(sl, cl.Value)
|
2011-03-14 03:51:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the content permanode is now known, look up its type
|
2011-07-01 21:33:15 +00:00
|
|
|
if content, ok := attr["camliContent"]; ok && len(content) > 0 {
|
2011-06-11 16:51:08 +00:00
|
|
|
cbr := blobref.Parse(content[len(content)-1])
|
2011-06-30 23:14:58 +00:00
|
|
|
dr.Describe(cbr, depth-1)
|
2011-05-30 22:44:25 +00:00
|
|
|
}
|
2011-06-11 17:12:39 +00:00
|
|
|
|
|
|
|
// Resolve children
|
2011-07-01 21:33:15 +00:00
|
|
|
if members, ok := attr["camliMember"]; ok {
|
|
|
|
for _, member := range members {
|
2011-06-12 07:20:57 +00:00
|
|
|
membr := blobref.Parse(member)
|
|
|
|
if membr != nil {
|
2011-06-30 23:14:58 +00:00
|
|
|
dr.Describe(membr, depth-1)
|
2011-06-11 17:12:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-30 22:44:25 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 17:27:23 +00:00
|
|
|
// TODO(mpl): we probably want to remove most of the calls on that one,
|
|
|
|
// or make it an error instead of a panic, when req is an unfiltered user input.
|
2011-06-23 05:21:18 +00:00
|
|
|
func mustGet(req *http.Request, param string) string {
|
|
|
|
v := req.FormValue(param)
|
|
|
|
if v == "" {
|
|
|
|
panic(fmt.Sprintf("missing required parameter %q", param))
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2013-02-07 17:33:00 +00:00
|
|
|
// TODO: convert this to have two *string pointers to error and errorType, and
|
|
|
|
// then add error and errType (embedded? if that works in Go 1.0) in all the other
|
|
|
|
// JSON response message struct types.
|
2011-06-23 05:21:18 +00:00
|
|
|
func setPanicError(m map[string]interface{}) {
|
|
|
|
p := recover()
|
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m["error"] = p.(string)
|
|
|
|
m["errorType"] = "input"
|
|
|
|
}
|
|
|
|
|
2013-02-07 17:33:00 +00:00
|
|
|
// SignerAttrValueResponse is the JSON response to $search/camli/search/signerattrvalue
|
|
|
|
type SignerAttrValueResponse struct {
|
2013-02-08 05:34:25 +00:00
|
|
|
Permanode *blobref.BlobRef `json:"permanode"`
|
|
|
|
Meta MetaMap `json:"meta"`
|
2013-02-07 17:33:00 +00:00
|
|
|
}
|
|
|
|
|
2011-06-23 05:21:18 +00:00
|
|
|
func (sh *Handler) serveSignerAttrValue(rw http.ResponseWriter, req *http.Request) {
|
2013-02-07 17:33:00 +00:00
|
|
|
done := false // get to success at the end?
|
2011-06-23 05:21:18 +00:00
|
|
|
ret := jsonMap()
|
2013-02-07 17:33:00 +00:00
|
|
|
defer func() {
|
|
|
|
if !done {
|
|
|
|
httputil.ReturnJSON(rw, ret)
|
|
|
|
}
|
|
|
|
}()
|
2011-06-23 05:21:18 +00:00
|
|
|
defer setPanicError(ret)
|
|
|
|
|
|
|
|
signer := blobref.MustParse(mustGet(req, "signer"))
|
|
|
|
attr := mustGet(req, "attr")
|
|
|
|
value := mustGet(req, "value")
|
|
|
|
pn, err := sh.index.PermanodeOfSignerAttrValue(signer, attr, value)
|
|
|
|
if err != nil {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
ret["error"] = err.Error()
|
2013-02-07 17:33:00 +00:00
|
|
|
return
|
|
|
|
}
|
2011-06-23 05:21:18 +00:00
|
|
|
|
2013-02-07 17:33:00 +00:00
|
|
|
dr := sh.NewDescribeRequest()
|
|
|
|
dr.Describe(pn, 2)
|
|
|
|
metaMap, err := dr.metaMap(0)
|
|
|
|
if err != nil {
|
|
|
|
ret["error"] = err.Error()
|
|
|
|
return
|
2011-06-23 05:21:18 +00:00
|
|
|
}
|
2013-02-07 17:33:00 +00:00
|
|
|
|
|
|
|
done = true
|
|
|
|
httputil.ReturnJSON(rw, &SignerAttrValueResponse{
|
|
|
|
Permanode: pn,
|
|
|
|
Meta: metaMap,
|
|
|
|
})
|
2011-06-23 05:21:18 +00:00
|
|
|
}
|
|
|
|
|
2012-11-05 14:09:34 +00:00
|
|
|
// Unlike the index interface's EdgesTo method, the "edgesto" Handler
|
|
|
|
// here additionally filters out since-deleted permanode edges.
|
|
|
|
func (sh *Handler) serveEdgesTo(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
ret := jsonMap()
|
|
|
|
defer httputil.ReturnJSON(rw, ret)
|
|
|
|
defer setPanicError(ret)
|
|
|
|
|
|
|
|
toRef := blobref.MustParse(mustGet(req, "blobref"))
|
|
|
|
toRefStr := toRef.String()
|
|
|
|
blobInfo := jsonMap()
|
|
|
|
ret[toRefStr] = blobInfo
|
|
|
|
|
|
|
|
jsonEdges := jsonMapList()
|
|
|
|
|
|
|
|
edges, err := sh.index.EdgesTo(toRef, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mapOrError struct {
|
|
|
|
m map[string]interface{} // nil if no map
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
resc := make(chan mapOrError)
|
|
|
|
verify := func(edge *Edge) {
|
|
|
|
fromStr := edge.From.String()
|
|
|
|
db, err := sh.NewDescribeRequest().DescribeSync(edge.From)
|
|
|
|
if err != nil {
|
|
|
|
resc <- mapOrError{err: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
if db.Permanode != nil {
|
|
|
|
for attr, vv := range db.Permanode.Attr {
|
|
|
|
if IsBlobReferenceAttribute(attr) {
|
|
|
|
for _, v := range vv {
|
|
|
|
if v == toRefStr {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var em map[string]interface{}
|
|
|
|
if found {
|
|
|
|
em = jsonMap()
|
|
|
|
em["from"] = fromStr
|
|
|
|
em["fromType"] = "permanode"
|
|
|
|
}
|
|
|
|
resc <- mapOrError{m: em}
|
|
|
|
}
|
|
|
|
verifying := 0
|
|
|
|
for _, edge := range edges {
|
|
|
|
if edge.FromType == "permanode" {
|
|
|
|
verifying++
|
|
|
|
go verify(edge)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
em := jsonMap()
|
|
|
|
em["from"] = edge.From.String()
|
|
|
|
em["fromType"] = edge.FromType
|
|
|
|
jsonEdges = append(jsonEdges, em)
|
|
|
|
}
|
|
|
|
for i := 0; i < verifying; i++ {
|
|
|
|
res := <-resc
|
|
|
|
if res.err != nil {
|
|
|
|
panic(res.err) // caught and put in JSON response
|
|
|
|
}
|
|
|
|
if res.m != nil {
|
|
|
|
jsonEdges = append(jsonEdges, res.m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
blobInfo["edgesTo"] = jsonEdges
|
|
|
|
}
|
|
|
|
|
2011-06-26 00:50:38 +00:00
|
|
|
func (sh *Handler) serveSignerPaths(rw http.ResponseWriter, req *http.Request) {
|
2013-02-08 17:27:23 +00:00
|
|
|
// TODO: finish breaking this function up into a pure query half
|
|
|
|
// and an HTTP half which uses the pure querying half.
|
|
|
|
vs := req.FormValue("signer")
|
|
|
|
if vs == "" {
|
|
|
|
serveJSONError(rw, errors.New("missing required parameter: \"signer\""))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
signer := blobref.Parse(vs)
|
|
|
|
if signer == nil {
|
|
|
|
serveJSONError(rw, fmt.Errorf("failed to parse signer blobref: %v", vs))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vt := req.FormValue("target")
|
|
|
|
if vt == "" {
|
|
|
|
serveJSONError(rw, errors.New("missing required parameter: \"target\""))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
target := blobref.Parse(vt)
|
|
|
|
if target == nil {
|
|
|
|
serveJSONError(rw, fmt.Errorf("failed to parse target blobref: %v", vt))
|
|
|
|
return
|
|
|
|
}
|
2011-06-26 00:50:38 +00:00
|
|
|
paths, err := sh.index.PathsOfSignerTarget(signer, target)
|
|
|
|
if err != nil {
|
2013-02-08 17:27:23 +00:00
|
|
|
serveJSONError(rw, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var jpaths []*SignerPathsItem
|
|
|
|
for _, path := range paths {
|
|
|
|
jpaths = append(jpaths, &SignerPathsItem{
|
|
|
|
ClaimRef: path.Claim,
|
|
|
|
BaseRef: path.Base,
|
|
|
|
Suffix: path.Suffix,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
dr := sh.NewDescribeRequest()
|
|
|
|
for _, path := range paths {
|
|
|
|
dr.Describe(path.Base, 2)
|
2011-06-26 00:50:38 +00:00
|
|
|
}
|
2013-02-08 17:27:23 +00:00
|
|
|
|
|
|
|
metaMap, err := dr.metaMap(0)
|
|
|
|
if err != nil {
|
|
|
|
serveJSONError(rw, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := &SignerPathsResponse{
|
|
|
|
Paths: jpaths,
|
|
|
|
Meta: metaMap,
|
|
|
|
}
|
|
|
|
httputil.ReturnJSON(rw, res)
|
2011-06-26 00:50:38 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 22:44:25 +00:00
|
|
|
const camliTypePrefix = "application/json; camliType="
|
|
|
|
|
2011-07-01 21:33:15 +00:00
|
|
|
func (d *DescribedBlob) setMimeType(mime string) {
|
|
|
|
d.MimeType = mime
|
2011-05-30 22:44:25 +00:00
|
|
|
if strings.HasPrefix(mime, camliTypePrefix) {
|
2011-07-01 21:33:15 +00:00
|
|
|
d.CamliType = mime[len(camliTypePrefix):]
|
2011-03-14 03:51:58 +00:00
|
|
|
}
|
2011-03-14 02:27:59 +00:00
|
|
|
}
|