Browse Source

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

1.0
Gisle Aune 7 years ago
parent
commit
a4fea77047
  1. 28
      model/file/file.go
  2. 34
      resolver/file.go
  3. 3
      schema/root.graphql

28
model/file/file.go

@ -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.
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{} 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 { } 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
resolver/file.go

@ -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
schema/root.graphql

@ -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!

Loading…
Cancel
Save