File model and api is almost complete

- Added "Kind" field to File model
- Added file query
- Added editFile mutation
- Added removeFile mutation
This commit is contained in:
2018-06-12 23:06:35 +02:00
parent 1978883cd7
commit 90a3597602
4 changed files with 232 additions and 0 deletions
+49
View File
@@ -19,6 +19,7 @@ var fileCollection *mgo.Collection
type File struct {
ID string `bson:"_id" json:"id"`
Time time.Time `bson:"time" json:"time"`
Kind string `bson:"kind" json:"kind"`
Public bool `bson:"public" json:"public"`
Name string `bson:"name" json:"name"`
MimeType string `bson:"mimeType" json:"mimeType"`
@@ -27,6 +28,53 @@ type File struct {
URL string `bson:"url,omitempty" json:"url,omitempty"`
}
// Edit edits the file, changing up to both the two mutable properties
func (file *File) Edit(name *string, public *bool) error {
changes := bson.M{}
changedFile := *file
if name != nil && *name != file.Name {
changes["name"] = *name
changedFile.Name = *name
}
if public != nil && *public != file.Public {
changes["public"] = *public
changedFile.Public = *public
}
if len(changes) == 0 {
return nil
}
err := fileCollection.UpdateId(file.ID, bson.M{"$set": changes})
if err != nil {
return err
}
*file = changedFile
return nil
}
// Delete removes the file information from the database, and deletes the file.
func (file *File) Delete() error {
err := fileCollection.RemoveId(file.ID)
if err != nil {
return err
}
if file.Kind == "upload" {
err = store.RemoveFile("files", file.ID)
if err != nil {
return err
}
}
file.URL = ""
return 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 == "" {
@@ -47,6 +95,7 @@ func Upload(ctx context.Context, name, mimeType, author string, size int64, inpu
file := File{
ID: id,
Kind: "upload",
Time: time.Now(),
Public: false,
Author: author,