同步代码
This commit is contained in:
@@ -0,0 +1 @@
|
||||
this a test file
|
||||
@@ -0,0 +1 @@
|
||||
this a test file
|
||||
81
lib/file.go
Normal file
81
lib/file.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//BJ/HX/RJ/OMC/FM/告警文件生成时间
|
||||
///FTP根目录/省份简称/专业简称/厂家编码/OMC名称/数据类别/日期或时间/
|
||||
//<省份简称>-<数据类别>-<网元类型>[-网元子类]-<主机编号>-<数据版本>-<数据时间>[-登录用户名][-同步请求标识][-Ri][-统计周期] [-序列号].<后缀>
|
||||
//BJ-FM-OMC-主机编码-v0-告警文件生成时间-001.txt
|
||||
|
||||
type FileMeta struct {
|
||||
DirRoot string `json:"dir_root"`
|
||||
Province string `json:"province"` //网元所在省份
|
||||
DeviceCode string `json:"device_code"` //主机编码 四位,每1位可用0-9、A-Z编码
|
||||
Time string `json:"time"` //文件生成时间
|
||||
Index string `json:"index"` //文件标识
|
||||
Compress bool `json:"compress"` //文件是否压缩
|
||||
ReqId string `json:"req_id"`
|
||||
}
|
||||
|
||||
// HasDir 判断文件夹是否存在
|
||||
func HasDir(path string) (bool, error) {
|
||||
_, _err := os.Stat(path)
|
||||
if _err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(_err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, _err
|
||||
}
|
||||
|
||||
func CreateDir(meta *FileMeta) (string, error) {
|
||||
dir := strings.Join([]string{meta.DirRoot, meta.Province, "HX", "RJ", "OMC", "FM", meta.Time}, "/")
|
||||
exist, err := HasDir(dir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !exist {
|
||||
err := os.MkdirAll(dir, os.ModePerm)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return dir, err
|
||||
}
|
||||
|
||||
func GetName(meta *FileMeta) string {
|
||||
fileName := strings.Join([]string{meta.Province, "FM", "OMC", meta.DeviceCode, "v0", meta.Time, meta.Index}, "-")
|
||||
return strings.ToUpper(fileName)
|
||||
}
|
||||
|
||||
func GenFile(meta *FileMeta, content []byte) (string, error) {
|
||||
// 创建文件夹
|
||||
dir, err := CreateDir(meta)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
//创建文件
|
||||
fileName := dir + "/" + GetName(meta) + ".txt"
|
||||
err = os.WriteFile(fileName, content, 0666)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// 创建一个新的ZIP文件
|
||||
fileName = fileName + ".zip"
|
||||
zipFile, err := os.Create(fileName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer zipFile.Close()
|
||||
|
||||
// 创建一个ZIP写入器
|
||||
zipWriter := zip.NewWriter(zipFile)
|
||||
defer zipWriter.Close()
|
||||
|
||||
return fileName, nil
|
||||
}
|
||||
20
lib/file_test.go
Normal file
20
lib/file_test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFile(t *testing.T) {
|
||||
var meta FileMeta
|
||||
meta.DirRoot = "FTP"
|
||||
meta.Province = "BJ"
|
||||
meta.DeviceCode = "0001"
|
||||
meta.Index = "001"
|
||||
meta.Time = time.Now().Format("20060102150405")
|
||||
meta.Compress = false
|
||||
content := "this a test file"
|
||||
f, err := GenFile(&meta, []byte(content))
|
||||
fmt.Println(f, err)
|
||||
}
|
||||
14
lib/password.go
Normal file
14
lib/password.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package lib
|
||||
|
||||
import "golang.org/x/crypto/bcrypt"
|
||||
|
||||
// Encrypt 加密明文密码
|
||||
func Encrypt(password string) (string, error) {
|
||||
hashedBytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
return string(hashedBytes), err
|
||||
}
|
||||
|
||||
// Compare 密文校验
|
||||
func Compare(hashedPassword, password string) error {
|
||||
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
||||
}
|
||||
Reference in New Issue
Block a user