fusenapi/utils/format/s3keyname.go

110 lines
3.8 KiB
Go
Raw Normal View History

package format
import (
"fmt"
"strings"
"time"
)
type TypeFormatS3KeyName int
const (
TypeS3KeyUser TypeFormatS3KeyName = 1 // 登录用户
TypeS3KeyGuest TypeFormatS3KeyName = 2 // 游客
)
2023-07-06 11:47:27 +00:00
type TypeCategory string
const TCategoryPersonalization TypeCategory = "personalization"
const TCategory3DTools TypeCategory = "3dtools"
const TCategoryQuotation TypeCategory = "quotation"
const TCategoryRenderMegre TypeCategory = "render_megre"
2023-07-14 11:26:00 +00:00
const TCategoryImage TypeCategory = "image"
2023-07-06 11:47:27 +00:00
var CategoryMap = map[string]bool{
string(TCategoryPersonalization): true,
string(TCategory3DTools): true,
string(TCategoryQuotation): true,
string(TCategoryRenderMegre): true,
2023-07-14 11:26:00 +00:00
string(TCategoryImage): true,
2023-07-06 11:47:27 +00:00
}
// FormatS3KeyName 需要输入选
2023-07-06 11:47:27 +00:00
func FormatS3KeyName(keytype TypeFormatS3KeyName, uid int64, now time.Time, env string, category TypeCategory, 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
2023-07-06 11:47:27 +00:00
func FormatS3KeyNameUser(userid int64, now time.Time, env string, category TypeCategory, 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]
}
2023-07-06 11:47:27 +00:00
switch category {
2023-07-14 11:26:00 +00:00
case TCategoryImage:
return fmt.Sprintf("/%s/%s/%d/%s_%d.%s", env, category, userid, name, now.UnixNano(), ext)
2023-07-06 11:47:27 +00:00
case TCategoryPersonalization:
2023-07-07 06:49:02 +00:00
// /{env}/personalization/{userid}/{filename}
return fmt.Sprintf("/%s/%s/%d/%s.%s", env, category, userid, name, ext)
case TCategoryRenderMegre:
// /{env}/render_megre/{userid}/{filename} 自动删除
return fmt.Sprintf("/%s/%s/%d/%s_%d.%s", env, category, userid, name, now.UnixNano(), ext)
2023-07-07 06:49:02 +00:00
case TCategory3DTools:
// /{env}/3dtools/年月/{userid}/{filename}
return fmt.Sprintf("/%s/%s/%04d%02d/%d/%s_%d.%s", env, category, year, int(month), userid, name, now.UnixNano(), ext)
2023-07-07 06:49:02 +00:00
case TCategoryQuotation:
// /{env}/quotation/年月/{userid}/{filename}
return fmt.Sprintf("/%s/%s/%04d%02d/%d/%s_%d.%s", env, category, year, int(month), userid, name, now.UnixNano(), ext)
2023-07-07 06:49:02 +00:00
default:
return fmt.Sprintf("/%s/%s/%d/%04d%02d/%s_%d.%s", env, category, userid, year, int(month), name, now.UnixNano(), ext)
2023-07-06 11:47:27 +00:00
}
}
// FormatS3KeyNameGuest 游客的格式化存储
2023-07-06 11:47:27 +00:00
func FormatS3KeyNameGuest(guestid int64, now time.Time, env string, category TypeCategory, 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]
}
2023-07-07 06:49:02 +00:00
switch category {
2023-07-14 11:26:00 +00:00
case TCategoryImage:
return fmt.Sprintf("/%s/%s/g/%d/%s_%d.%s", env, category, guestid, name, now.UnixNano(), ext)
2023-07-07 06:49:02 +00:00
case TCategoryPersonalization:
2023-07-14 11:26:00 +00:00
// /{env}/personalization/g/{guestid}/{filename}
return fmt.Sprintf("/%s/%s/g/%d/%s.%s", env, category, guestid, name, ext)
2023-07-07 06:49:02 +00:00
case TCategoryRenderMegre:
// /{env}/render_megre/{guestid}/{filename} 自动删除
2023-07-14 11:26:00 +00:00
return fmt.Sprintf("/%s/%s/g/%d/%s_%d.%s", env, category, guestid, name, now.UnixNano(), ext)
2023-07-07 06:49:02 +00:00
case TCategory3DTools:
panic(TCategory3DTools + "不存在游客")
// /{env}/3dtools/年月/{guestid}/{filename}
// return fmt.Sprintf("/%s/%s/%04d%02d/%d/%s_%d.%s", env, category, year, int(month), guestid, name, now.UnixNano(), ext)
2023-07-07 06:49:02 +00:00
case TCategoryQuotation:
panic(TCategoryQuotation + "不存在游客")
// /{env}/quotation/年月/{guestid}/{filename}
// return fmt.Sprintf("/%s/%s/%04d%02d/%d/%s_%d.%s", env, category, year, int(month), guestid, name, now.UnixNano(), ext)
2023-07-07 06:49:02 +00:00
default:
2023-07-14 11:26:00 +00:00
return fmt.Sprintf("/%s/%s/g/%d/%04d%02d/%s_%d.%s", env, category, guestid, year, int(month), name, now.UnixNano(), ext)
2023-07-07 06:49:02 +00:00
}
}