54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
|
package format
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type TypeFormatS3KeyName int
|
||
|
|
||
|
const (
|
||
|
TypeS3KeyUser TypeFormatS3KeyName = 1 // 登录用户
|
||
|
TypeS3KeyGuest TypeFormatS3KeyName = 2 // 游客
|
||
|
)
|
||
|
|
||
|
// FormatS3KeyName 需要输入选
|
||
|
func FormatS3KeyName(keytype TypeFormatS3KeyName, uid int64, now time.Time, env, category, name string) string {
|
||
|
if keytype == TypeS3KeyUser {
|
||
|
return FormatS3KeyNameUser(uid, now, env, category, name)
|
||
|
} else if keytype == TypeS3KeyGuest {
|
||
|
return FormatS3KeyNameGuest(uid, now, env, category, name)
|
||
|
} else {
|
||
|
panic("key type error")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// FormatS3KeyNameUser
|
||
|
func FormatS3KeyNameUser(userid int64, now time.Time, env, category, name string) string {
|
||
|
year, month, _ := now.Date()
|
||
|
names := strings.Split(name, ".")
|
||
|
var ext string
|
||
|
if len(names) == 1 {
|
||
|
name = names[0]
|
||
|
} else if len(names) == 2 {
|
||
|
name = names[0]
|
||
|
ext = names[1]
|
||
|
}
|
||
|
return fmt.Sprintf("/%s/%s/%d/%04d%02d/%s_%d.%s", env, category, userid, year, int(month), name, now.Unix(), ext)
|
||
|
}
|
||
|
|
||
|
// FormatS3KeyNameGuest 游客的格式化存储
|
||
|
func FormatS3KeyNameGuest(guestid int64, now time.Time, env, category, name string) string {
|
||
|
year, month, _ := now.Date()
|
||
|
names := strings.Split(name, ".")
|
||
|
var ext string
|
||
|
if len(names) == 1 {
|
||
|
name = names[0]
|
||
|
} else if len(names) == 2 {
|
||
|
name = names[0]
|
||
|
ext = names[1]
|
||
|
}
|
||
|
return fmt.Sprintf("/%s/guest/%s/%d/%04d%02d/%s_%d.%s", env, category, guestid, year, int(month), name, now.Unix(), ext)
|
||
|
}
|