package format import ( "fmt" "strings" "time" ) type TypeFormatS3KeyName int const ( TypeS3KeyUser TypeFormatS3KeyName = 1 // 登录用户 TypeS3KeyGuest TypeFormatS3KeyName = 2 // 游客 ) type TypeCategory string const TCategoryPersonalization TypeCategory = "personalization" const TCategory3DTools TypeCategory = "3dtools" const TCategoryQuotation TypeCategory = "quotation" const TCategoryRenderMegre TypeCategory = "render_megre" const TCategoryImage TypeCategory = "image" var CategoryMap = map[string]bool{ string(TCategoryPersonalization): true, string(TCategory3DTools): true, string(TCategoryQuotation): true, string(TCategoryRenderMegre): true, string(TCategoryImage): true, } // FormatS3KeyName 需要输入选 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 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] } switch category { case TCategoryImage: return fmt.Sprintf("/%s/%s/%d/%s_%d.%s", env, category, userid, name, now.UnixNano(), ext) case TCategoryPersonalization: // /{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) 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) 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) default: return fmt.Sprintf("/%s/%s/%d/%04d%02d/%s_%d.%s", env, category, userid, year, int(month), name, now.UnixNano(), ext) } } // FormatS3KeyNameGuest 游客的格式化存储 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] } switch category { case TCategoryImage: return fmt.Sprintf("/%s/%s/g/%d/%s_%d.%s", env, category, guestid, name, now.UnixNano(), ext) case TCategoryPersonalization: // /{env}/personalization/g/{guestid}/{filename} return fmt.Sprintf("/%s/%s/g/%d/%s.%s", env, category, guestid, name, ext) case TCategoryRenderMegre: // /{env}/render_megre/{guestid}/{filename} 自动删除 return fmt.Sprintf("/%s/%s/g/%d/%s_%d.%s", env, category, guestid, name, now.UnixNano(), ext) 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) 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) default: return fmt.Sprintf("/%s/%s/g/%d/%04d%02d/%s_%d.%s", env, category, guestid, year, int(month), name, now.UnixNano(), ext) } } // generateS3KeyName 生成