/
upload.go
52 lines (43 loc) · 1.13 KB
/
upload.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
package storage
import (
"fmt"
"path/filepath"
"time"
uuid2 "github.com/google/uuid"
)
type Upload struct {
ID int `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
UUID string `gorm:"index;unique"`
TickerID int `gorm:"index"`
Path string
Extension string
ContentType string
}
func NewUpload(filename, contentType string, tickerID int) Upload {
now := time.Now()
uuid := uuid2.New()
ext := filepath.Ext(filename)[1:]
// First version we use a date based directory structure
path := fmt.Sprintf("%d/%d", now.Year(), now.Month())
return Upload{
Path: path,
UUID: uuid.String(),
TickerID: tickerID,
Extension: ext,
ContentType: contentType,
}
}
func (u *Upload) FileName() string {
return fmt.Sprintf("%s.%s", u.UUID, u.Extension)
}
func (u *Upload) FullPath(uploadPath string) string {
return fmt.Sprintf("%s/%s/%s", uploadPath, u.Path, u.FileName())
}
func (u *Upload) URL(uploadPath string) string {
return MediaURL(u.FileName(), uploadPath)
}
func MediaURL(name string, uploadPath string) string {
return fmt.Sprintf("%s/media/%s", uploadPath, name)
}