Added files API, fixed file.List returning all files if both author and public is unset

This commit is contained in:
2018-06-13 21:11:52 +02:00
parent 90a3597602
commit a4fea77047
3 changed files with 57 additions and 8 deletions
+20 -8
View File
@@ -4,6 +4,7 @@ import (
"context"
"crypto/rand"
"encoding/binary"
"errors"
"io"
"strconv"
"time"
@@ -125,19 +126,27 @@ func FindID(id string) (File, error) {
return file, nil
}
// ListFiles lists files according to the standard lookup. By default it's just the author's own files,
// List lists files according to the standard lookup. By default it's just the author's own files,
// but if `public` is true it will alos include files made public by other authors. If `mimeTypes` contains
// any, it will limit the results to that.
func ListFiles(author string, public bool, mimeTypes []string) ([]File, error) {
// any, it will limit the results to that. If `author` is empty, it will only list public files
func List(author string, public bool, mimeTypes []string) ([]File, error) {
query := bson.M{}
if public {
query["$or"] = []bson.M{
bson.M{"author": author},
bson.M{"public": true},
if author != "" {
if public {
query["$or"] = []bson.M{
bson.M{"author": author},
bson.M{"public": true},
}
} else {
query["author"] = author
}
} else {
query["author"] = author
if !public {
return nil, errors.New("No author specified, and public is unset")
}
query["public"] = true
}
if len(mimeTypes) > 0 {
@@ -183,5 +192,8 @@ func init() {
fileCollection = db.C("file.headers")
fileCollection.EnsureIndexKey("author")
fileCollection.EnsureIndexKey("public")
fileCollection.EnsureIndexKey("author", "public")
fileCollection.EnsureIndexKey("kind")
})
}