models: Migrated last from model

This commit is contained in:
2018-10-27 10:14:58 +02:00
parent b7d5059d60
commit 4979b02104
7 changed files with 88 additions and 476 deletions
+2
View File
@@ -72,6 +72,8 @@ var allowdMimeTypes = map[string]bool{
"image/jpeg": true,
"image/png": true,
"image/gif": true,
"image/tiff": true,
"image/tga": true,
"text/plain": true,
"application/json": true,
"application/pdf": false,
+5
View File
@@ -9,3 +9,8 @@ import (
func FindID(id string) (models.File, error) {
return find(bson.M{"_id": id})
}
// FindName finds a file by ID
func FindName(kind, name, author string) (models.File, error) {
return find(bson.M{"name": name, "kind": kind, "author": author})
}
+29
View File
@@ -0,0 +1,29 @@
package files
import (
"time"
"git.aiterp.net/rpdata/api/models"
)
// Insert manually inserts file information into the database. This should never, ever be HTTP API accessible
func Insert(name, kind, mimeType, author string, time time.Time, size int64, url string) (models.File, error) {
file := models.File{
ID: makeID(),
Kind: kind,
Time: time,
Public: false,
Author: author,
Name: name,
MimeType: mimeType,
Size: size,
URL: url,
}
err := collection.Insert(file)
if err != nil {
return models.File{}, err
}
return file, nil
}
+52
View File
@@ -0,0 +1,52 @@
package files
import (
"context"
"errors"
"io"
"time"
"git.aiterp.net/rpdata/api/internal/store"
"git.aiterp.net/rpdata/api/models"
)
// Upload adds a file to the space.
func Upload(ctx context.Context, name, mimeType, author string, size int64, input io.Reader) (models.File, error) {
if !allowdMimeTypes[mimeType] {
return models.File{}, errors.New("File type not allowed:" + mimeType)
}
if name == "" {
date := time.Now().UTC().Format("Jan 02 2006 15:04:05 MST")
name = "Unnamed file (" + date + ")"
}
if mimeType == "" {
mimeType = "binary/octet-stream"
}
id := makeID()
path, err := store.UploadFile(ctx, "files", id, mimeType, input, size)
if err != nil {
return models.File{}, err
}
file := models.File{
ID: id,
Kind: "upload",
Time: time.Now(),
Public: false,
Author: author,
Name: name,
MimeType: mimeType,
Size: size,
URL: store.URLFromPath(path),
}
err = collection.Insert(file)
if err != nil {
return models.File{}, err
}
return file, nil
}