forked from getfider/fider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob.go
53 lines (45 loc) · 1.3 KB
/
blob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package blob
import (
"errors"
"path/filepath"
"strings"
"github.com/getfider/fider/app/models"
"github.com/gosimple/slug"
)
// Blob is a file persisted somewhere
type Blob struct {
Key string
Object []byte
Size int64
ContentType string
}
// ErrNotFound is returned when given blob is not found
var ErrNotFound = errors.New("Blob not found")
// ErrInvalidKeyFormat is returned when blob key is in invalid format
var ErrInvalidKeyFormat = errors.New("Blob key is in invalid format")
// SanitizeFileName replaces invalid characters from given filename
func SanitizeFileName(fileName string) string {
fileName = strings.TrimSpace(fileName)
ext := filepath.Ext(fileName)
if ext != "" {
return slug.Make(fileName[0:len(fileName)-len(ext)]) + ext
}
return slug.Make(fileName)
}
// ValidateKey checks if key is is valid format
func ValidateKey(key string) error {
if len(key) == 0 || len(key) > 512 || strings.Contains(key, " ") {
return ErrInvalidKeyFormat
}
if strings.HasPrefix(key, "/") || strings.HasSuffix(key, "/") {
return ErrInvalidKeyFormat
}
return nil
}
// Storage is how Fider persists blobs
type Storage interface {
SetCurrentTenant(tenant *models.Tenant)
Get(key string) (*Blob, error)
Delete(key string) error
Put(key string, content []byte, contentType string) error
}