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" "context"
"crypto/rand" "crypto/rand"
"encoding/binary" "encoding/binary"
"errors"
"io" "io"
"strconv" "strconv"
"time" "time"
@@ -125,19 +126,27 @@ func FindID(id string) (File, error) {
return file, nil 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 // 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. // any, it will limit the results to that. If `author` is empty, it will only list public files
func ListFiles(author string, public bool, mimeTypes []string) ([]File, error) { func List(author string, public bool, mimeTypes []string) ([]File, error) {
query := bson.M{} query := bson.M{}
if public { if author != "" {
query["$or"] = []bson.M{ if public {
bson.M{"author": author}, query["$or"] = []bson.M{
bson.M{"public": true}, bson.M{"author": author},
bson.M{"public": true},
}
} else {
query["author"] = author
} }
} else { } else {
query["author"] = author if !public {
return nil, errors.New("No author specified, and public is unset")
}
query["public"] = true
} }
if len(mimeTypes) > 0 { if len(mimeTypes) > 0 {
@@ -183,5 +192,8 @@ func init() {
fileCollection = db.C("file.headers") fileCollection = db.C("file.headers")
fileCollection.EnsureIndexKey("author") fileCollection.EnsureIndexKey("author")
fileCollection.EnsureIndexKey("public")
fileCollection.EnsureIndexKey("author", "public")
fileCollection.EnsureIndexKey("kind")
}) })
} }
+34
View File
@@ -26,6 +26,40 @@ func (r *QueryResolver) File(ctx context.Context, args *FileArgs) (*FileResolver
return &FileResolver{F: file}, nil return &FileResolver{F: file}, nil
} }
// FilesArgs is an arg
type FilesArgs struct {
IncludePublic *bool
MimeTypes *[]string
}
// Files implements the file query
func (r *QueryResolver) Files(ctx context.Context, args *FilesArgs) ([]*FileResolver, error) {
user := session.FromContext(ctx).User()
author := ""
if user != nil {
author = user.ID
}
public := args.IncludePublic != nil && *args.IncludePublic == true
mimeTypes := []string(nil)
if args.MimeTypes != nil {
mimeTypes = *args.MimeTypes
}
files, err := file.List(author, public, mimeTypes)
if err != nil {
return nil, err
}
resolvers := make([]*FileResolver, len(files))
for i := range files {
resolvers[i] = &FileResolver{F: files[i]}
}
return resolvers, nil
}
// FileEditInput is an input for the editFile mutation // FileEditInput is an input for the editFile mutation
type FileEditInput struct { type FileEditInput struct {
ID string ID string
+3
View File
@@ -25,6 +25,9 @@ type Query {
# Find file by ID # Find file by ID
file(id: String!): File file(id: String!): File
# Files get all available files. If includePublic is set, it will return public files as well
files(includePublic: Boolean, mimeTypes: [String!]): [File!]!
# Find current session # Find current session
session: Session! session: Session!