1.4.5.2
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package iplibrary
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/ip-library/iplibraryutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "ipLibrary", "index")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct{}) {
|
||||
this.Data["canAccess"] = iplibraryutils.CanAccess()
|
||||
|
||||
// IP库列表
|
||||
artifactsResp, err := this.RPC().IPLibraryArtifactRPC().FindAllIPLibraryArtifacts(this.AdminContext(), &pb.FindAllIPLibraryArtifactsRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
var artifactMaps = []maps.Map{}
|
||||
|
||||
// 按创建时间倒序排序,最新的在前
|
||||
artifacts := artifactsResp.IpLibraryArtifacts
|
||||
for i := 0; i < len(artifacts); i++ {
|
||||
for j := i + 1; j < len(artifacts); j++ {
|
||||
if artifacts[i].CreatedAt < artifacts[j].CreatedAt {
|
||||
artifacts[i], artifacts[j] = artifacts[j], artifacts[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 确保最后一个(最新的)是使用中的,其他都是未使用的
|
||||
// 如果列表不为空,将最新的设为使用中,其他设为未使用
|
||||
if len(artifacts) > 0 {
|
||||
latestId := artifacts[0].Id
|
||||
for _, artifact := range artifacts {
|
||||
var shouldBePublic = (artifact.Id == latestId)
|
||||
if artifact.IsPublic != shouldBePublic {
|
||||
// 需要更新状态(这里只标记,不实际更新数据库,因为上传时已经更新了)
|
||||
// 如果数据库状态不一致,这里会显示不一致,但不会自动修复
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, artifact := range artifacts {
|
||||
var meta = &iplibrary.Meta{}
|
||||
if len(artifact.MetaJSON) > 0 {
|
||||
err = json.Unmarshal(artifact.MetaJSON, meta)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 文件信息
|
||||
var fileMap = maps.Map{
|
||||
"size": 0,
|
||||
}
|
||||
if artifact.File != nil {
|
||||
fileMap = maps.Map{
|
||||
"size": artifact.File.Size,
|
||||
}
|
||||
}
|
||||
|
||||
// 判断是否使用中:如果是列表中的第一个(最新的),则标记为使用中
|
||||
isPublic := artifact.Id == artifacts[0].Id
|
||||
|
||||
// 根据文件名判断IP库类型
|
||||
libraryType := "未知"
|
||||
if artifact.File != nil && len(artifact.File.Filename) > 0 {
|
||||
filename := strings.ToLower(artifact.File.Filename)
|
||||
if strings.Contains(filename, "city") {
|
||||
libraryType = "city库"
|
||||
} else if strings.Contains(filename, "asn") {
|
||||
libraryType = "asn库"
|
||||
}
|
||||
}
|
||||
|
||||
artifactMaps = append(artifactMaps, maps.Map{
|
||||
"id": artifact.Id,
|
||||
"name": artifact.Name,
|
||||
"createdTime": timeutil.FormatTime("Y-m-d H:i:s", artifact.CreatedAt),
|
||||
"isPublic": isPublic,
|
||||
"fileId": artifact.FileId,
|
||||
"code": artifact.Code,
|
||||
"libraryType": libraryType,
|
||||
"countCountries": len(meta.Countries),
|
||||
"countProvinces": len(meta.Provinces),
|
||||
"countCities": len(meta.Cities),
|
||||
"countTowns": len(meta.Towns),
|
||||
"countProviders": len(meta.Providers),
|
||||
"file": fileMap,
|
||||
})
|
||||
}
|
||||
this.Data["artifacts"] = artifactMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
Reference in New Issue
Block a user