You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.7 KiB
83 lines
1.7 KiB
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,
|
|
}
|