From fe37b80cfad15f997a09c6af755e9607d40fe8af Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Wed, 13 Jun 2018 22:45:07 +0200 Subject: [PATCH] Added file.Insert, added rpdata-wikifileimport command, changed file listing to sort by date descending --- makefile | 1 + model/file/file.go | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/makefile b/makefile index 969bf3a..430719e 100644 --- a/makefile +++ b/makefile @@ -10,6 +10,7 @@ build: go build -ldflags="-s -w" -o $(INSTALL_PATH)/usr/bin/rpdata-graphiql ./cmd/rpdata-graphiql go build -ldflags="-s -w" -o $(INSTALL_PATH)/usr/bin/rpdata-lb2charimport ./cmd/rpdata-lb2charimport go build -ldflags="-s -w" -o $(INSTALL_PATH)/usr/bin/rpdata-lb2logimport ./cmd/rpdata-lb2logimport + go build -ldflags="-s -w" -o $(INSTALL_PATH)/usr/bin/rpdata-wikifileimport ./cmd/rpdata-wikifileimport install: cp $(INSTALL_PATH)/usr/bin/* /usr/local/bin/ \ No newline at end of file diff --git a/model/file/file.go b/model/file/file.go index 026603b..f7d33d3 100644 --- a/model/file/file.go +++ b/model/file/file.go @@ -76,6 +76,28 @@ func (file *File) Delete() error { return nil } +// Insert manually inserts file information into the database. This should never, ever be API accessible +func Insert(name, kind, mimeType, author string, time time.Time, size int64, url string) (File, error) { + file := File{ + ID: makeFileID(), + Kind: kind, + Time: time, + Public: false, + Author: author, + Name: name, + MimeType: mimeType, + Size: size, + URL: url, + } + + err := fileCollection.Insert(file) + if err != nil { + return File{}, err + } + + return file, nil +} + // Upload adds a file to the space. func Upload(ctx context.Context, name, mimeType, author string, size int64, input io.Reader) (File, error) { if name == "" { @@ -126,6 +148,19 @@ func FindID(id string) (File, error) { return file, nil } +// FindName finds a file by kind, name and author +func FindName(kind string, name string, author string) (File, error) { + query := bson.M{"kind": kind, "name": name, "author": author} + file := File{} + + err := fileCollection.Find(query).One(&file) + if err != nil { + return File{}, err + } + + return file, nil +} + // 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. If `author` is empty, it will only list public files @@ -159,7 +194,7 @@ func List(author string, public bool, mimeTypes []string) ([]File, error) { func listFiles(query interface{}) ([]File, error) { list := make([]File, 0, 32) - err := fileCollection.Find(query).All(&list) + err := fileCollection.Find(query).Sort("-time").All(&list) if err != nil { return nil, err } @@ -193,6 +228,7 @@ func init() { fileCollection.EnsureIndexKey("author") fileCollection.EnsureIndexKey("public") + fileCollection.EnsureIndexKey("kind", "name", "author") fileCollection.EnsureIndexKey("author", "public") fileCollection.EnsureIndexKey("kind") })