Added files API, fixed file.List returning all files if both author and public is unset
This commit is contained in:
+20
-8
@@ -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")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -26,6 +26,40 @@ func (r *QueryResolver) File(ctx context.Context, args *FileArgs) (*FileResolver
|
||||
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
|
||||
type FileEditInput struct {
|
||||
ID string
|
||||
|
||||
@@ -25,6 +25,9 @@ type Query {
|
||||
# Find file by ID
|
||||
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
|
||||
session: Session!
|
||||
|
||||
Reference in New Issue
Block a user