This commit is contained in:
@@ -1,83 +0,0 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.aiterp.net/rpdata/api/internal/store"
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo"
|
||||
)
|
||||
|
||||
var collection *mgo.Collection
|
||||
|
||||
func find(query interface{}) (models.File, error) {
|
||||
file := models.File{}
|
||||
|
||||
err := collection.Find(query).One(&file)
|
||||
if err != nil {
|
||||
return models.File{}, err
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func list(query interface{}) ([]models.File, error) {
|
||||
list := make([]models.File, 0, 32)
|
||||
|
||||
err := collection.Find(query).Sort("-time").All(&list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// makeID makes a random file ID that's 32 characters long
|
||||
func makeID() string {
|
||||
result := "F" + strconv.FormatInt(time.Now().UnixNano(), 36)
|
||||
offset := 0
|
||||
data := make([]byte, 32)
|
||||
|
||||
rand.Read(data)
|
||||
for len(result) < 32 {
|
||||
result += strconv.FormatUint(binary.LittleEndian.Uint64(data[offset:]), 36)
|
||||
offset += 8
|
||||
|
||||
if offset >= 32 {
|
||||
rand.Read(data)
|
||||
offset = 0
|
||||
}
|
||||
}
|
||||
|
||||
return result[:32]
|
||||
}
|
||||
|
||||
func init() {
|
||||
store.HandleInit(func(db *mgo.Database) {
|
||||
collection = db.C("file.headers")
|
||||
|
||||
collection.EnsureIndexKey("author")
|
||||
collection.EnsureIndexKey("public")
|
||||
collection.EnsureIndexKey("kind", "name", "author")
|
||||
collection.EnsureIndexKey("author", "public")
|
||||
collection.EnsureIndexKey("kind")
|
||||
})
|
||||
}
|
||||
|
||||
var allowdMimeTypes = map[string]bool{
|
||||
"": false,
|
||||
"image/jpeg": true,
|
||||
"image/png": true,
|
||||
"image/gif": true,
|
||||
"image/tiff": true,
|
||||
"image/tga": true,
|
||||
"text/plain": true,
|
||||
"application/json": true,
|
||||
"application/pdf": false,
|
||||
"binary/octet-stream": false,
|
||||
"video/mp4": false,
|
||||
"audio/mp3": false,
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// FindID finds a file by ID
|
||||
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})
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// Filter for files.List
|
||||
type Filter struct {
|
||||
Author *string
|
||||
Public *bool
|
||||
MimeType []string
|
||||
}
|
||||
|
||||
// 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
|
||||
func List(filter *Filter) ([]models.File, error) {
|
||||
query := bson.M{}
|
||||
|
||||
if filter != nil {
|
||||
if filter.Author != nil {
|
||||
query["author"] = *filter.Author
|
||||
}
|
||||
|
||||
if filter.Public != nil {
|
||||
query["public"] = *filter.Public
|
||||
}
|
||||
|
||||
if len(filter.MimeType) > 0 {
|
||||
query["mimeTypes"] = bson.M{"$in": filter.MimeType}
|
||||
}
|
||||
}
|
||||
|
||||
return list(query)
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/internal/store"
|
||||
"github.com/globalsign/mgo"
|
||||
)
|
||||
|
||||
var collection *mgo.Collection
|
||||
|
||||
func init() {
|
||||
store.HandleInit(func(db *mgo.Database) {
|
||||
collection = db.C("core.users")
|
||||
})
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo"
|
||||
)
|
||||
|
||||
// Ensure finds a user by id, or makes a new one.
|
||||
func Ensure(id string) (models.User, error) {
|
||||
user := models.User{}
|
||||
err := collection.FindId(id).One(&user)
|
||||
|
||||
if err == mgo.ErrNotFound {
|
||||
user = models.User{
|
||||
ID: id,
|
||||
Nick: "",
|
||||
Permissions: []string{
|
||||
"member",
|
||||
"log.edit",
|
||||
"post.edit",
|
||||
"post.move",
|
||||
"post.remove",
|
||||
"file.upload",
|
||||
},
|
||||
}
|
||||
|
||||
err := collection.Insert(user)
|
||||
if err != nil {
|
||||
return models.User{}, err
|
||||
}
|
||||
}
|
||||
|
||||
return user, err
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
)
|
||||
|
||||
// Find finds a user by id
|
||||
func Find(id string) (models.User, error) {
|
||||
user := models.User{}
|
||||
err := collection.FindId(id).One(&user)
|
||||
|
||||
return user, err
|
||||
}
|
||||
Reference in New Issue
Block a user