sunsheng 1 an în urmă
comite
3afa674e0a
12 a modificat fișierele cu 546 adăugiri și 0 ștergeri
  1. 4 0
      .gitignore
  2. 11 0
      db.json
  3. 10 0
      go.mod
  4. 6 0
      go.sum
  5. 112 0
      huawei/main.go
  6. 75 0
      main.go
  7. 48 0
      upload/main.go
  8. 9 0
      uploader.json
  9. 20 0
      utils/copy.go
  10. 76 0
      utils/file.go
  11. 44 0
      utils/utils.go
  12. 131 0
      utils/zip.go

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+dist_electron
+app.json
+logo.png
+package.json

+ 11 - 0
db.json

@@ -0,0 +1,11 @@
+{
+    "label": "Queenter.鲲特",
+    "name": "queenter",
+    "icon": "https://lancher.obs.cn-east-3.myhuaweicloud.com/assets/icon_queenter.png",
+    "type": "智能录入",
+    "version": "1.1.1",
+    "sizeM": 153,
+    "url": "https://lancher.obs.cn-east-3.myhuaweicloud.com/queenter/queenter-v1.1.1.2-windows.zip",
+    "platform": "windows-amd64",
+    "thumbnail": "https://lancher.obs.cn-east-3.myhuaweicloud.com/assets/fm_qr.png"
+}

+ 10 - 0
go.mod

@@ -0,0 +1,10 @@
+module app-uploader
+
+go 1.21.3
+
+require github.com/huaweicloud/huaweicloud-sdk-go-obs v3.23.9+incompatible
+
+require (
+	golang.org/x/net v0.19.0 // indirect
+	golang.org/x/text v0.14.0 // indirect
+)

+ 6 - 0
go.sum

@@ -0,0 +1,6 @@
+github.com/huaweicloud/huaweicloud-sdk-go-obs v3.23.9+incompatible h1:zUhCrGMMpJxZGAB30GbQzluDhQuPENxRQfxss7KlpKU=
+github.com/huaweicloud/huaweicloud-sdk-go-obs v3.23.9+incompatible/go.mod h1:l7VUhRbTKCzdOacdT4oWCwATKyvZqUOlOqr0Ous3k4s=
+golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
+golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
+golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
+golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=

+ 112 - 0
huawei/main.go

@@ -0,0 +1,112 @@
+package huawei
+
+import (
+	"fmt"
+	"os"
+	"path"
+	"path/filepath"
+	"strings"
+
+	"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
+)
+
+const endpoint = "obs.cn-east-3.myhuaweicloud.com"
+const bucketName = "spu3dv1"
+
+func createObsClient() (*obs.ObsClient, error) {
+	return obs.New("RA1D7CFVCVKUFX1FHHPB", "cDrR2NgAF2KaA4MRkcK19r93P3P6hLKOPhHvPu9p", endpoint)
+}
+
+type OssType struct {
+	Url  string
+	Size int64
+}
+
+var _client *obs.ObsClient = nil
+
+func InitConfig() error {
+	c, e := createObsClient()
+	if e != nil {
+		return e
+	}
+	_client = c
+	return nil
+}
+
+func UploadFile(fpath string, obsDir string, name string) (*OssType, error) {
+	if _client == nil {
+		return nil, fmt.Errorf("obs init error")
+	}
+	obsClient := _client
+	keyFormat := "%s/%s"
+	if strings.HasSuffix(obsDir, "/") {
+		keyFormat = "%s%s"
+	}
+	obsname := name
+	if len(name) < 2 {
+		_, obsname = filepath.Split(fpath)
+	}
+	obsKey := fmt.Sprintf(keyFormat, obsDir, obsname)
+	input := &obs.PutFileInput{}
+	input.Bucket = bucketName
+	input.Key = obsKey
+	input.SourceFile = fpath
+
+	fpathExt := path.Ext(fpath)
+	isGzFile := fpathExt == ".gz"
+	if isGzFile {
+		input.ContentType = "text/plain"
+		input.Metadata = map[string]string{"Content-Encoding": "gzip"}
+	}
+	fmt.Println("writing file", fpath, "=>", obsKey)
+	result, err := obsClient.PutFile(input)
+
+	isUploadOk := true
+
+	if err != nil {
+		fmt.Println("upload failed try again", err.Error())
+		isUploadOk = false
+	}
+
+	if result.StatusCode != 200 {
+		isUploadOk = false
+	}
+
+	if !isUploadOk {
+		result, err = obsClient.PutFile(input)
+
+		if err != nil {
+			fmt.Println("upload obs fail2", fpath, err.Error())
+			return nil, err
+		}
+		if result.StatusCode != 200 {
+			return nil, err
+		}
+	}
+
+	if isGzFile {
+		metaRet, err := obsClient.SetObjectMetadata(&obs.SetObjectMetadataInput{
+			Bucket: bucketName,
+			Key:    obsKey,
+			// ContentEncoding: "gzip",
+			// ContentType: "text/plain",
+		})
+		fmt.Println(metaRet, err)
+
+		if err != nil {
+			metaRet, err = obsClient.SetObjectMetadata(&obs.SetObjectMetadataInput{
+				Bucket: bucketName,
+				Key:    obsKey,
+				// ContentEncoding: "gzip",
+				// ContentType:     "text/plain",
+			})
+			fmt.Println(metaRet, err)
+		}
+	}
+	fi, err := os.Stat(fpath)
+	size := int64(1)
+	if err == nil {
+		size = fi.Size()
+	}
+	return &OssType{Url: fmt.Sprintf("//%s.%s/%s", bucketName, endpoint, obsKey), Size: int64(size)}, nil
+}

+ 75 - 0
main.go

@@ -0,0 +1,75 @@
+package main
+
+import (
+	"app-uploader/utils"
+	"fmt"
+	"runtime"
+)
+
+func main() {
+	// 读取package.json版本数据
+	// 复制app.json文件和logo文件
+
+	// 读取配置
+
+	appConf, err := utils.ReadAppConfig()
+	if err != nil {
+		fmt.Println("读取配置文件错误")
+		return
+	}
+	fmt.Println(appConf)
+
+	uploadConf, err := utils.ReadConfig()
+	if err != nil {
+		fmt.Println("读取配置文件错误")
+		return
+	}
+	dest := ""
+	copyFiles := []string{}
+	// 需要修改版本的文件
+	modify := ""
+	// 复制文件到对应目录
+	switch appConf.Key {
+	case "queenter":
+		dest = uploadConf.Queenter.Dest
+		copyFiles = uploadConf.Queenter.Files
+		modify = uploadConf.Queenter.Modify
+	default:
+		return
+	}
+	// 复制文件到目标目录中
+	for _, file := range copyFiles {
+		err := utils.Copy(file, fmt.Sprintf("%s/%s", dest, file))
+		if err != nil {
+			fmt.Println(err)
+			return
+		}
+	}
+	// 获取版本信息
+	version, err := utils.GetVersion()
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	appConf.Version = version
+	appConfDest := fmt.Sprintf("%s/%s", dest, modify)
+	err = utils.WriteAppConfig(appConf, appConfDest)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	// 压缩文件
+	// remoteswift-v1.0.0-x64-windows.zip
+	zipPath := fmt.Sprintf("output/%s-v%s-%s-%s.zip", appConf.Key, version, runtime.GOARCH, runtime.GOOS)
+	fmt.Println("压缩文件中...")
+	utils.Zip(zipPath, dest)
+	if err != nil {
+		fmt.Println("压缩文件出错")
+		return
+	}
+
+	// 上传文件
+
+	// 输出db文件
+
+}

+ 48 - 0
upload/main.go

@@ -0,0 +1,48 @@
+package upload
+
+// import (
+// 	"errors"
+// 	"fmt"
+// 	"os"
+// 	"path/filepath"
+// 	"runtime"
+// 	"strings"
+// )
+
+// func UploadExe() (string, error) {
+// 	// 获取当前执行程序
+// 	// G:\wk\lancher-svc\src\lancher.exe
+// 	appExePath, _ := os.Executable()
+// 	// G:/wk/lancher-svc/src/lancher.exe
+// 	appExePath = strings.Replace(appExePath, "\\", "/", -1)
+// 	// G:/wk/lancher-svc/src/
+// 	appExeDir, _ := filepath.Split(appExePath)
+// 	fmt.Println(appExeDir)
+
+// 	// 压缩当前执行程序
+// 	// 压缩单个文件
+// 	// 压缩文件名 lancher1.0.2.windows-amd64.zip
+// 	zipFile := fmt.Sprintf("%s%s.%s-%s.zip", "lancher", Version, runtime.GOOS, runtime.GOARCH)
+// 	zipPath := fmt.Sprintf("%s%s", appExeDir, zipFile)
+// 	err := ZipFile(zipPath, appExePath)
+// 	if err != nil {
+// 		return "", err
+// 	}
+// 	// 获取操作系统和版本号确定上传文件名
+// 	obsDir := "pkg"
+// 	//上传压缩后的文件到obs
+// 	// http://spu3dv1.obs.cn-east-3.myhuaweicloud.com/pkg/lancher1.0.2.windows-amd64.zip
+// 	huawei.InitConfig()
+// 	obs, err := huawei.UploadFile(zipPath, obsDir, zipFile)
+// 	if err != nil {
+// 		return "", err
+// 	}
+// 	if len(obs.Url) < 1 {
+// 		fmt.Println("上传zipfile错误")
+// 		return "", errors.New("上传zipfile错误")
+// 	}
+// 	fmt.Println("succ uploaded=>", obs.Url)
+
+// 	return zipPath, nil
+
+// }

+ 9 - 0
uploader.json

@@ -0,0 +1,9 @@
+
+{
+    "queenter":{
+        "files": ["app.json","logo.png"],
+        "modify": "app.json",
+        "packName": "queenter",
+        "dest": "dist_electron/win-unpacked"
+    }
+}

+ 20 - 0
utils/copy.go

@@ -0,0 +1,20 @@
+package utils
+
+import (
+	"fmt"
+	"os"
+)
+
+func Copy(source, dest string) error {
+	input, err := os.ReadFile(source)
+	if err != nil {
+		fmt.Println(err)
+		return err
+	}
+	err = os.WriteFile(dest, input, 0644)
+	if err != nil {
+		fmt.Println(err)
+		return err
+	}
+	return nil
+}

+ 76 - 0
utils/file.go

@@ -0,0 +1,76 @@
+package utils
+
+import (
+	"bufio"
+	"encoding/json"
+	"errors"
+	"fmt"
+	"os"
+)
+
+func GetVersion() (string, error) {
+	data, err := os.ReadFile(PackConfPath)
+	if err != nil {
+		fmt.Println(err)
+		return "", errors.New("读取文件失败")
+	}
+
+	packConf := make(map[string]interface{})
+	err = json.Unmarshal(data, &packConf)
+	if err != nil {
+		fmt.Println(err)
+		return "", errors.New("解析文件失败")
+	}
+	if _, ok := packConf["version"]; !ok {
+		return "", errors.New("没有版本信息")
+	}
+
+	return packConf["version"].(string), nil
+}
+
+func ReadConfig() (*Uploader, error) {
+	conf, err := os.Open(ConfigPath)
+	if err != nil {
+		fmt.Println(err)
+		return nil, errors.New("打开文件失败")
+	}
+	reader := bufio.NewReader(conf)
+	decoder := json.NewDecoder(reader)
+	config := &Uploader{}
+	err = decoder.Decode(config)
+	if err != nil {
+		return nil, errors.New("解析失败")
+	}
+	return config, nil
+}
+
+func ReadAppConfig() (*App, error) {
+	conf, err := os.Open(AppConfPath)
+	if err != nil {
+		fmt.Println(err)
+		return nil, errors.New("打开文件失败")
+	}
+
+	reader := bufio.NewReader(conf)
+	decoder := json.NewDecoder(reader)
+	appConf := &App{}
+	err = decoder.Decode(appConf)
+	if err != nil {
+		return nil, errors.New("解析失败")
+	}
+	return appConf, nil
+}
+
+func WriteAppConfig(config *App, dest string) error {
+	data, err := json.MarshalIndent(config, "", "  ")
+	if err != nil {
+		fmt.Println(err)
+		return errors.New("json marshal err")
+	}
+	err = os.WriteFile(dest, data, 0777)
+	if err != nil {
+		fmt.Println(err)
+		return errors.New("写入配置文件出错")
+	}
+	return nil
+}

+ 44 - 0
utils/utils.go

@@ -0,0 +1,44 @@
+package utils
+
+var PackConfPath = "package.json"
+var ConfigPath = "uploader.json"
+var OutputDb = "db.json"
+var AppConfPath = "app.json"
+
+type Uploader struct {
+	Queenter QueenterUp
+}
+
+type QueenterUp struct {
+	Files    []string
+	Modify   string
+	PackName string
+	Dest     string
+}
+
+type Version struct {
+	Label     string
+	Name      string
+	Icon      string
+	Type      string
+	Version   string
+	SizeM     int
+	Url       string
+	Platform  string
+	Thumbnail string
+}
+
+type App struct {
+	Name           string
+	Version        string
+	LastUpdateDate string
+	Icon           string
+	RevAssets      []string
+	Size           string
+	Tags           string
+	Desc           string
+	Entry          string
+	Type           string
+	Key            string
+	InstCount      int
+}

+ 131 - 0
utils/zip.go

@@ -0,0 +1,131 @@
+package utils
+
+import (
+	"archive/zip"
+	"fmt"
+	"io"
+	"os"
+	"path/filepath"
+)
+
+// func Zip(zipPath string, paths ...string) error {
+// 	// Create zip file and it's parent dir.
+// 	if err := os.MkdirAll(filepath.Dir(zipPath), os.ModePerm); err != nil {
+// 		return err
+// 	}
+// 	archive, err := os.Create(zipPath)
+// 	if err != nil {
+// 		return err
+// 	}
+// 	defer archive.Close()
+
+// 	// New zip writer.
+// 	zipWriter := zip.NewWriter(archive)
+// 	defer zipWriter.Close()
+
+// 	// Traverse the file or directory.
+// 	for _, rootPath := range paths {
+// 		// Remove the trailing path separator if path is a directory.
+// 		rootPath = strings.TrimSuffix(rootPath, string(os.PathSeparator))
+
+// 		// Visit all the files or directories in the tree.
+// 		err = filepath.Walk(rootPath, walkFunc(rootPath, zipWriter))
+// 		if err != nil {
+// 			return err
+// 		}
+// 	}
+// 	return nil
+// }
+
+// func walkFunc(rootPath string, zipWriter *zip.Writer) filepath.WalkFunc {
+// 	return func(path string, info fs.FileInfo, err error) error {
+// 		if err != nil {
+// 			return err
+// 		}
+
+// 		// If a file is a symbolic link it will be skipped.
+// 		if info.Mode()&os.ModeSymlink != 0 {
+// 			return nil
+// 		}
+
+// 		// Create a local file header.
+// 		header, err := zip.FileInfoHeader(info)
+// 		if err != nil {
+// 			return err
+// 		}
+
+// 		// Set compression method.
+// 		header.Method = zip.Deflate
+
+// 		// Set relative path of a file as the header name.
+// 		header.Name, err = filepath.Rel(filepath.Dir(rootPath), path)
+// 		if err != nil {
+// 			return err
+// 		}
+// 		if info.IsDir() {
+// 			header.Name += string(os.PathSeparator)
+// 		}
+
+// 		// Create writer for the file header and save content of the file.
+// 		headerWriter, err := zipWriter.CreateHeader(header)
+// 		if err != nil {
+// 			return err
+// 		}
+// 		if info.IsDir() {
+// 			return nil
+// 		}
+// 		f, err := os.Open(path)
+// 		if err != nil {
+// 			return err
+// 		}
+// 		defer f.Close()
+// 		_, err = io.Copy(headerWriter, f)
+// 		return err
+// 	}
+// }
+
+func Zip(zip_file_name string, src_dir string) {
+
+	// 预防:旧文件无法覆盖
+	os.RemoveAll(zip_file_name)
+
+	// 创建:zip文件
+	zipfile, _ := os.Create(zip_file_name)
+	defer zipfile.Close()
+
+	// 打开:zip文件
+	archive := zip.NewWriter(zipfile)
+	defer archive.Close()
+
+	// 遍历路径信息
+	filepath.Walk(src_dir, func(path string, info os.FileInfo, _ error) error {
+
+		// 如果是源路径,提前进行下一个遍历
+		if path == src_dir {
+			return nil
+		}
+
+		// 获取:文件头信息
+		header, _ := zip.FileInfoHeader(info)
+		// path, _ = filepath.Rel(src_dir, path)
+		fmt.Println(path)
+		header.Name, _ = filepath.Rel(src_dir, path)
+
+		// 判断:文件是不是文件夹
+		if info.IsDir() {
+			header.Name += `/`
+		} else {
+			// 设置:zip的文件压缩算法
+			header.Method = zip.Deflate
+		}
+
+		// 创建:压缩包头部信息
+		writer, _ := archive.CreateHeader(header)
+		if !info.IsDir() {
+			file, _ := os.Open(path)
+			defer file.Close()
+			io.Copy(writer, file)
+		}
+		return nil
+	})
+}