|
| 1 | +package lang |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/1Panel-dev/1Panel/backend/global" |
| 11 | + "github.com/1Panel-dev/1Panel/backend/utils/cmd" |
| 12 | + "github.com/1Panel-dev/1Panel/backend/utils/files" |
| 13 | +) |
| 14 | + |
| 15 | +func Init() { |
| 16 | + go initLang() |
| 17 | +} |
| 18 | + |
| 19 | +func initLang() { |
| 20 | + fileOp := files.NewFileOp() |
| 21 | + geoPath := path.Join(global.CONF.System.BaseDir, "1panel/geo/GeoIP.mmdb") |
| 22 | + isLangExist := fileOp.Stat("/usr/local/bin/lang/zh.sh") |
| 23 | + isGeoExist := fileOp.Stat(geoPath) |
| 24 | + if isLangExist && isGeoExist { |
| 25 | + return |
| 26 | + } |
| 27 | + upgradePath := path.Join(global.CONF.System.BaseDir, "1panel/tmp/upgrade") |
| 28 | + tmpPath, err := loadRestorePath(upgradePath) |
| 29 | + upgradeDir := path.Join(upgradePath, tmpPath, "downloads") |
| 30 | + if err != nil || len(tmpPath) == 0 || !fileOp.Stat(upgradeDir) { |
| 31 | + if !isLangExist { |
| 32 | + downloadLangFromRemote(fileOp) |
| 33 | + } |
| 34 | + if !isGeoExist { |
| 35 | + downloadGeoFromRemote(fileOp, geoPath) |
| 36 | + } |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + files, _ := os.ReadDir(upgradeDir) |
| 41 | + if len(files) == 0 { |
| 42 | + tmpPath = "no such file" |
| 43 | + } else { |
| 44 | + for _, item := range files { |
| 45 | + if item.IsDir() && strings.HasPrefix(item.Name(), "1panel-") { |
| 46 | + tmpPath = path.Join(upgradePath, tmpPath, "downloads", item.Name()) |
| 47 | + break |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + if tmpPath == "no such file" || !fileOp.Stat(tmpPath) { |
| 52 | + if !isLangExist { |
| 53 | + downloadLangFromRemote(fileOp) |
| 54 | + } |
| 55 | + if !isGeoExist { |
| 56 | + downloadGeoFromRemote(fileOp, geoPath) |
| 57 | + } |
| 58 | + return |
| 59 | + } |
| 60 | + if !isLangExist { |
| 61 | + if !fileOp.Stat(path.Join(tmpPath, "lang")) { |
| 62 | + downloadLangFromRemote(fileOp) |
| 63 | + return |
| 64 | + } |
| 65 | + std, err := cmd.Execf("cp -r %s %s", path.Join(tmpPath, "lang"), "/usr/local/bin/") |
| 66 | + if err != nil { |
| 67 | + global.LOG.Errorf("load lang from package failed, std: %s, err: %v", std, err) |
| 68 | + return |
| 69 | + } |
| 70 | + global.LOG.Info("init lang successful") |
| 71 | + } |
| 72 | + if !isGeoExist { |
| 73 | + if !fileOp.Stat(path.Join(tmpPath, "GeoIP.mmdb")) { |
| 74 | + downloadGeoFromRemote(fileOp, geoPath) |
| 75 | + return |
| 76 | + } |
| 77 | + std, err := cmd.Execf("cp %s %s", path.Join(tmpPath, "GeoIP.mmdb"), path.Dir(geoPath)) |
| 78 | + if err != nil { |
| 79 | + global.LOG.Errorf("load geo ip from package failed, std: %s, err: %v", std, err) |
| 80 | + return |
| 81 | + } |
| 82 | + global.LOG.Info("init geo ip successful") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func loadRestorePath(upgradeDir string) (string, error) { |
| 87 | + if _, err := os.Stat(upgradeDir); err != nil && os.IsNotExist(err) { |
| 88 | + return "no such file", nil |
| 89 | + } |
| 90 | + files, err := os.ReadDir(upgradeDir) |
| 91 | + if err != nil { |
| 92 | + return "", err |
| 93 | + } |
| 94 | + var folders []string |
| 95 | + for _, file := range files { |
| 96 | + if file.IsDir() { |
| 97 | + folders = append(folders, file.Name()) |
| 98 | + } |
| 99 | + } |
| 100 | + if len(folders) == 0 { |
| 101 | + return "no such file", nil |
| 102 | + } |
| 103 | + sort.Slice(folders, func(i, j int) bool { |
| 104 | + return folders[i] > folders[j] |
| 105 | + }) |
| 106 | + return folders[0], nil |
| 107 | +} |
| 108 | + |
| 109 | +func downloadLangFromRemote(fileOp files.FileOp) { |
| 110 | + path := fmt.Sprintf("%s/language/lang.tar.gz", global.CONF.System.RepoUrl) |
| 111 | + if err := fileOp.DownloadFileWithProxy(path, "/usr/local/bin/lang.tar.gz"); err != nil { |
| 112 | + global.LOG.Errorf("download lang.tar.gz failed, err: %v", err) |
| 113 | + return |
| 114 | + } |
| 115 | + if !fileOp.Stat("/usr/local/bin/lang.tar.gz") { |
| 116 | + global.LOG.Error("download lang.tar.gz failed, no such file") |
| 117 | + return |
| 118 | + } |
| 119 | + std, err := cmd.Execf("tar zxvfC %s %s", "/usr/local/bin/lang.tar.gz", "/usr/local/bin/") |
| 120 | + if err != nil { |
| 121 | + fmt.Printf("decompress lang.tar.gz failed, std: %s, err: %v", std, err) |
| 122 | + return |
| 123 | + } |
| 124 | + _ = os.Remove("/usr/local/bin/lang.tar.gz") |
| 125 | + global.LOG.Info("download lang successful") |
| 126 | +} |
| 127 | +func downloadGeoFromRemote(fileOp files.FileOp, targetPath string) { |
| 128 | + _ = os.MkdirAll(path.Dir(targetPath), os.ModePerm) |
| 129 | + pathItem := fmt.Sprintf("%s/geo/GeoIP.mmdb", global.CONF.System.RepoUrl) |
| 130 | + if err := fileOp.DownloadFileWithProxy(pathItem, targetPath); err != nil { |
| 131 | + global.LOG.Errorf("download geo ip failed, err: %v", err) |
| 132 | + return |
| 133 | + } |
| 134 | + global.LOG.Info("download geo ip successful") |
| 135 | +} |
0 commit comments