Exemplo n.º 1
0
Arquivo: mp.go Projeto: a2n/MP
func (mp *MovableProperty) getPage() string {
    urlStr := fmt.Sprintf("http://www.tpkonsale.moj.gov.tw/sale/sale1QryM.asp?pageno=%d", mp.PageNo)
    resp, err := http.DefaultClient.Get(urlStr)
    if err != nil {
	log.Printf("%s has error, %s", alu.Caller(), err.Error())
	return ""
    }

    b, err := ioutil.ReadAll(resp.Body)
    if err != nil {
	log.Printf("%s has error, %s", alu.Caller(), err.Error())
	return ""
    }
    resp.Body.Close()

    cd, err := iconv.Open("utf-8//ignore", "big5")
    if err != nil {
	    log.Printf("%s has error, %s", alu.Caller(), err.Error())
	    return ""
    }

    utf8 := cd.ConvString(string(b))

    return utf8
}
Exemplo n.º 2
0
func main() {
	response, err := http.Get("http://14.18.207.155:8000/dev/raw-attachment/wiki/策划/数值数据/装备定义表.csv")
	if err != nil {
		fmt.Printf("%s", err)
		os.Exit(1)
	} else {
		defer response.Body.Close()
		contents, err := ioutil.ReadAll(response.Body)
		if err != nil {
			fmt.Printf("%s", err)
			os.Exit(1)
		}
		//fmt.Printf("%s\n", string(contents));

		cd, err := iconv.Open("utf-8", "gbk")
		if err != nil {
			fmt.Printf("iconv open failed!")
			return
		}
		defer cd.Close()

		//gbk := cd.ConvString(string(contents));
		//fmt.Printf("%s\n", gbk);

		fout, err := os.Create("./tmp.csv")
		defer fout.Close()
		if err != nil {
			fmt.Println("创建临时文件失败: ", err)
			return
		}
		fout.WriteString(cd.ConvString(string(contents)))

		dump2sql()
	}
}
Exemplo n.º 3
0
//抓取美名堂数据(http://www.sheup.net/mingzi_boy_5.php?page=40&id=139)
func MeiMingTang() {
	arr := make([]string, 5000)

	//	初始化转码
	cv, err := iconv.Open("utf-8", "gbk") // convert utf-8 to gbk
	if err != nil {
		log.Println("iconv.Open failed!")
		return
	}
	defer cv.Close()

	m := 0
	for i := 1; i <= 40; i++ {
		doc, err := goquery.NewDocument("http://www.sheup.net/mingzi_boy_5.php?page=" + strconv.Itoa(i) + "&id=139")
		if err != nil {
			log.Fatal(err)
		}
		doc.Find(".mingzi_s ul li").Each(func(j int, contentSelection *goquery.Selection) {
			m++
			title := contentSelection.Text()
			arr[m] = cv.ConvString(title)
			log.Println("第", m, "个名字:", arr[m])
		})
	}
}
Exemplo n.º 4
0
func init() {
	var err error
	ic, err = iconv.Open("utf-8", "gbk")
	if err != nil {
		glog.Fatal("iconv.Open failed!")
	}
}
Exemplo n.º 5
0
func main() {
	// request := gorequest.New()
	// _, body, errs := request.Get("http://mm.howkuai.com/wp-content/uploads/2015a/11/20/01.jpg").End()

	// if errs == nil {
	// 	image := []byte(body)
	// 	ioutil.WriteFile("1.jpg", image, 0644)
	// }

	doc, err := goquery.NewDocument("http://www.meizitu.com/")
	if err != nil {
		fmt.Println(err.Error())
	}

	doc.Find(".tags a").Each(func(_ int, s *goquery.Selection) {
		url, _ := s.Attr("href")

		title := s.Text()
		cd, err := iconv.Open("utf-8", "gb2312")
		if err != nil {
			fmt.Println("iconv.Open failed!")
		}
		defer cd.Close()

		title = cd.ConvString(title)
		fmt.Println(title)
	})
}
Exemplo n.º 6
0
func processFile(filename, outputFilename string) {
	lesson, err := parseLessonXml(filename)
	if err != nil {
		fmt.Printf("err: %v", err)
	}

	var path, content string
	newXmlContent := &Lesson{}

	cd, _ := iconv.Open("UTF-8", "big5")
	defer cd.Close()

	for i := 0; i < len(lesson.Files); i++ {
		path = lesson.Files[i].Path
		if !isUsefulFileType(path) {
			continue
		}

		content = DecodeStr(lesson.Files[i].Content)
		if strings.Contains(path, ".cond") {
			content = cd.ConvString(content)
		}
		newXmlContent.Files = append(newXmlContent.Files, File{path, content})
	}

	err = outputNewXml("output/"+outputFilename, newXmlContent)
	if err != nil {
		fmt.Printf("err: %v", err)
	}

	defer wg.Done()
}
Exemplo n.º 7
0
func update(db *(sql.DB), wiki_name string, table_name string) {
	response, err := http.Get("http://14.18.207.155:8000/dev/raw-attachment/wiki/策划/数值数据/" + wiki_name + ".csv")
	if err != nil {
		fmt.Printf("%s", err)
		os.Exit(1)
	} else {
		defer response.Body.Close()
		contents, err := ioutil.ReadAll(response.Body)
		if err != nil {
			fmt.Printf("%s", err)
			os.Exit(1)
		}

		cd, err := iconv.Open("utf-8", "gbk")
		if err != nil {
			fmt.Printf("iconv open failed!")
			return
		}
		defer cd.Close()

		fout, err := os.Create("./tmp.csv")
		defer fout.Close()
		if err != nil {
			fmt.Println("创建临时文件失败: ", err)
			return
		}
		fout.WriteString(cd.ConvString(string(contents)))

		load2db(db, table_name)
	}
}
Exemplo n.º 8
0
func main() {
	urls = make(map[string]int)
	itemReg = regexp.MustCompile(`http://item\.taobao\.com/item\.htm\?id=\d+`)
	itemIdReg = regexp.MustCompile(`\d+`)
	itemLevelReg = regexp.MustCompile(`level:&#34;(\d+)&#34;,count:&#34;(\d+)&#34;`)

	cd, _ = iconv.Open("gbk", "utf-8")
	getShop("http://feiliao888.taobao.com/")
	getShop("http://daisystyle.taobao.com/")
	getShop("http://molixiang.taobao.com/")
	getShop("http://shop103755843.taobao.com/")
	getShop("http://tutu0.taobao.com/")
	getShop("http://liv-fashion.taobao.com/")
	getShop("http://shop35430112.taobao.com/")
	getShop("http://rumere.taobao.com/")
	getShop("http://iamfino.taobao.com/")
	getShop("http://heyahua.taobao.com/")
	getShop("http://pinyitang.taobao.com/")
	getShop("http://lhzh.taobao.com/")
	getShop("http://fanxiongmengxiang.taobao.com/")
	getShop("http://mygod1.taobao.com/")
	getShop("http://tywf.taobao.com/")
	getShop("http://zhujiazhuang.taobao.com/")
	getShop("http://quanjuner.taobao.com/")
	getShop("http://wmbzp.taobao.com/")
	getShop("http://fanfan0427.taobao.com/")
	getShop("http://lecoor.taobao.com/")
	getShop("http://coffeeandcigarettes.taobao.com/")
	getShop("http://happy123.taobao.com/")
	getShop("http://hzjrfs.taobao.com/")
	getShop("http://jmjgroup.taobao.com/")
	getShop("http://jmjgroup.taobao.com/")
	getShop("http://jmjgroup.taobao.com/")
}
Exemplo n.º 9
0
func UploadDevice(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	w.Header().Add("Content-type", "text/html; charset=utf-8")
	r.ParseForm()
	file, handle, err := r.FormFile("file")
	if err != nil {
		w.Write([]byte("{\"Message\":\"" + err.Error() + "\",\"Status\":\"error\"}"))
		return
	}

	cd, err := iconv.Open("UTF-8", "GBK")
	if err != nil {
		w.Write([]byte("{\"Message\":\"" + err.Error() + "\",\"Status\":\"error\"}"))
		return
	}
	defer cd.Close()

	dir := "/tmp/cloudboot-server/"
	if !util.FileExist(dir) {
		err := os.MkdirAll(dir, 0777)
		if err != nil {
			w.Write([]byte("{\"Message\":\"" + err.Error() + "\",\"Status\":\"error\"}"))
			return
		}
	}

	list := strings.Split(handle.Filename, ".")
	fix := list[len(list)-1]

	h := md5.New()
	h.Write([]byte(fmt.Sprintf("%s", time.Now().UnixNano()) + handle.Filename))
	cipherStr := h.Sum(nil)
	md5 := fmt.Sprintf("%s", hex.EncodeToString(cipherStr))
	filename := "osinstall-upload-" + md5 + "." + fix

	result := make(map[string]interface{})
	result["result"] = filename

	if util.FileExist(dir + filename) {
		os.Remove(dir + filename)
	}

	f, err := os.OpenFile(dir+filename, os.O_WRONLY|os.O_CREATE, 0666)
	io.Copy(f, file)
	if err != nil {
		w.Write([]byte("{\"Message\":\"" + err.Error() + "\",\"Status\":\"error\"}"))
		return
	}
	defer f.Close()
	defer file.Close()

	data := map[string]interface{}{"Status": "success", "Message": "操作成功", "Content": result}
	json, err := json.Marshal(data)
	if err != nil {
		w.Write([]byte("{\"Message\":\"" + err.Error() + "\",\"Status\":\"error\"}"))
		return
	}
	w.Write([]byte(json))
	return
}
Exemplo n.º 10
0
func Encoding_U82G(src string) string {
	cd, err := iconv.Open("GBK", "UTF-8//IGNORE")
	if err != nil {
		fmt.Println("open iconv error")
		return ""
	}
	defer cd.Close()

	return cd.ConvString(src)
}
Exemplo n.º 11
0
func utf8ToGbk(s string) (string, error) {
	cd, err := iconv.Open("gbk", "utf-8")
	if err != nil {
		return "", err
	}
	defer cd.Close()
	gbk := cd.ConvString(s)

	return gbk, nil
}
Exemplo n.º 12
0
// WriteGBK write a string to the printer with GBK encode
func (e *Escpos) WriteGBK(data string) (int, error) {
	cd, err := iconv.Open("gbk", "utf-8")
	if err != nil {
		beelog.Critical("iconv.Open failed!")
		return 0, err
	}
	defer cd.Close()
	gbk := cd.ConvString(data)
	return e.WriteRaw([]byte(gbk))
}
Exemplo n.º 13
0
func Encoding_U82U(src string) string {
	cd, err := iconv.Open("UTF8", "UNICODE")
	if err != nil {
		fmt.Println("open iconv error")
		return ""
	}
	defer cd.Close()

	return cd.ConvString(src)
}
Exemplo n.º 14
0
func Encoding_G2U8(src string) string {
	cd, err := iconv.Open("UTF-8", "GBK")
	if err != nil {
		fmt.Println("open iconv error")
		return ""
	}
	defer cd.Close()

	return cd.ConvString(src)
}
Exemplo n.º 15
0
func new_utf8_reader(charset string, in io.Reader) (io.ReadCloser, error) {
	if charset == "utf-8" || charset == "" {
		return ioutil.NopCloser(in), nil
	}
	converter, err := iconv.Open("utf-8", charset)
	if err != nil {
		return nil, err
	}
	ireader := iconv.NewReader(converter, in, 0)
	return &utf8_readcloser{converter, ireader}, nil
}
Exemplo n.º 16
0
func main() {
	bufLocal := make([]byte, 512)
	serverAddr, _ := net.ResolveUDPAddr("udp", "0:53")
	server, _ := net.ListenUDP("udp", serverAddr)
	for {

		fd, err := sysfd(server)
		if err != nil {
			log.Panic()
		}

		// this only gets the server TTL
		// ttl, _ := syscall.GetsockoptInt(int(f.Fd()), syscall.IPPROTO_IP, syscall.IP_TTL)

		err = syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_RECVTTL, 1)
		if err != nil {
			log.Panic()
		}

		// see msg_control/cmsghdr from `man 2  recvmsg`
		oob := make([]byte, 32) // actually 16
		n, oobn, _, addr, _ := server.ReadMsgUDP(bufLocal, oob)
		dataReq := bufLocal[:n]
		ttl := binary.LittleEndian.Uint32(oob[12:16])
		log.Printf(
			"Q: %v xid=%v fd=%v ttl=%v\n",
			addr, binary.BigEndian.Uint16(dataReq[0:2]), fd, ttl)
		fmt.Println(oob[:oobn])
		go func() {
			dataRsp := bufLocal[:n]
			if false {
				binary.BigEndian.PutUint16(dataRsp[2:4], 0x8183)
				server.WriteTo(dataRsp, addr)
			} else {
				twitterName := parseName(dataReq)
				fmt.Println(twitterName)
				tweetData := getTwitter(twitterName) // could be `printf` in bash
				if ttl > 64 {
					cd, err := iconv.Open("gbk", "utf-8")
					if err != nil {
						fmt.Println("iconv.Open failed!")
						return
					}
					defer cd.Close()
					var buf [512]byte
					tweetData, _, _ = cd.Conv(tweetData, buf[:])

				}
				dataRsp = setAnswer(dataRsp, tweetData, 0x05)
				server.WriteTo(dataRsp, addr)
			}
		}()
	}
}
Exemplo n.º 17
0
func main() {
	str := "Aujourd'hui, je fais le ménage dans une maison de retraite."

	cd, err := iconv.Open("latin1", "utf-8")
	if err != nil {
		panic(err)
	}
	defer cd.Close()

	bla := cd.ConvString(str)

	fmt.Println(bla)
}
Exemplo n.º 18
0
func main() {

	cd, err := iconv.Open("gbk", "utf-8")
	if err != nil {
		fmt.Println("iconv.Open failed!")
		return
	}
	defer cd.Close()

	gbk := cd.ConvString("你好,世界!")

	fmt.Println(gbk)
}
Exemplo n.º 19
0
func UploadDevice(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	r.ParseForm()
	file, handle, err := r.FormFile("file")
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	cd, err := iconv.Open("UTF-8", "GBK")
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}
	defer cd.Close()

	dir := "./upload/"
	if !util.FileExist(dir) {
		err := os.MkdirAll(dir, 0777)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
			return
		}
	}

	list := strings.Split(handle.Filename, ".")
	fix := list[len(list)-1]

	h := md5.New()
	h.Write([]byte(fmt.Sprintf("%s", time.Now().UnixNano()) + handle.Filename))
	cipherStr := h.Sum(nil)
	md5 := fmt.Sprintf("%s", hex.EncodeToString(cipherStr))
	filename := md5 + "." + fix

	result := make(map[string]interface{})
	result["result"] = filename

	if util.FileExist(dir + filename) {
		os.Remove(dir + filename)
	}

	f, err := os.OpenFile(dir+filename, os.O_WRONLY|os.O_CREATE, 0666)
	io.Copy(f, file)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}
	defer f.Close()
	defer file.Close()
	w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功", "Content": result})
	return
}
Exemplo n.º 20
0
func charsetReader(charset string, r io.Reader) (io.Reader, error) {
	switch charset {
	case "ISO-8859-1", "iso-8859-1":
		return r, nil
	default:
		cd, err := iconv.Open("utf-8", charset)
		if err != nil {
			break
		}
		r := iconv.NewReader(cd, r, 1024)
		return r, nil
	}
	return nil, errors.New("Unsupported character set encoding: " + charset)
}
Exemplo n.º 21
0
func main() {
	cd, err := iconv.Open("gbk", "utf-8") // utf8 => gbk
	if err != nil {
		fmt.Println("iconv.Open failed!")
		return
	}
	defer cd.Close()

	autoSync := false
	w := iconv.NewWriter(cd, os.Stdout, 0, autoSync)

	fmt.Fprintln(w,
		`		你好,世界!你好,世界!你好,世界!你好,世界!
		你好,世界!你好,世界!你好,世界!你好,世界!`)

	w.Sync() // call it by yourself if autoSync == false
}
Exemplo n.º 22
0
func main() {

	cd, err := iconv.Open("utf-8", "gbk") // gbk => utf8
	if err != nil {
		fmt.Println("iconv.Open failed!")
		return
	}
	defer cd.Close()

	r := iconv.NewReader(cd, os.Stdin, 0)

	_, err = io.Copy(os.Stdout, r)
	if err != nil {
		fmt.Println("\nio.Copy failed:", err)
		return
	}
}
Exemplo n.º 23
0
func toUTF8(file []byte, tempfile io.Writer) (string, error) {
	charset, err := getFileEncoding(file)
	if err != nil {
		return "", err
	}

	cd, err := iconv.Open("utf-8", charset)
	if err != nil {
		return charset, err
	}
	defer cd.Close()

	bufSize := 0
	reader := iconv.NewReader(cd, bytes.NewBuffer(file), bufSize)

	io.Copy(tempfile, reader)
	return charset, nil
}
Exemplo n.º 24
0
func main() {
	cd, err := iconv.Open("utf-8", "latin1")
	if err != nil {
		fmt.Println("iconv.Open failed!")
		return
	}
	defer cd.Close()

	output, _ := os.OpenFile("utf8.csv", os.O_RDWR|os.O_CREATE, 0)
	input, err := os.OpenFile("./data/163/chddata/sh/20131101/600004.csv", os.O_RDWR|os.O_CREATE, 0)
	bufSize := 0
	r := iconv.NewReader(cd, input, bufSize)

	_, err = io.Copy(output, r)
	if err != nil {
		fmt.Println("\nio.Copy failed:", err)
		return
	}
}
Exemplo n.º 25
0
func (c *Crawler) run() error {
	defer func() {
		close(c.tagQueue)
		close(c.albumQueue)
		close(c.imageQueue)
		close(c.stop)
	}()

	startUrl := "http://www.meizitu.com/"
	doc, err := goquery.NewDocument(startUrl)
	if err != nil {
		fmt.Println(err.Error())
	}

	doc.Find(".tags a").Each(func(_ int, s *goquery.Selection) {
		url, _ := s.Attr("href")
		title := s.Text()
		cd, err := iconv.Open("utf-8", "gb2312")
		if err != nil {
			fmt.Println("iconv.Open failed!")
		}
		defer cd.Close()
		title = cd.ConvString(title)

		tag := NewAsset(title, url)
		c.tagQueue <- tag
	})

	for {
		select {
		case tag := <-c.tagQueue:
			go c.downloadTag(tag)
		case album := <-c.albumQueue:
			go c.downloadAlbum(album)
		case image := <-c.imageQueue:
			go c.downloadImage(image)
		case <-c.stop:
			return nil
		}
	}
}
Exemplo n.º 26
0
//抓取大米饭网数据(http://www.damifan.cn/dong.asp)
func DaMiFan() {
	arr := make([]string, 2000)
	//	抓取数据
	doc, err := goquery.NewDocument("http://www.damifan.cn/dong.asp")
	if err != nil {
		log.Fatal(err)
	}
	//	初始化转码
	cv, err := iconv.Open("utf-8", "gbk") // convert utf-8 to gbk
	if err != nil {
		log.Println("iconv.Open failed!")
		return
	}
	defer cv.Close()

	doc.Find("#apDiv1 a").Each(func(i int, contentSelection *goquery.Selection) {
		title := contentSelection.Text()
		arr[i] = cv.ConvString(title)
		log.Println("第", i+1, "个名字:", arr[i])
	})
}
Exemplo n.º 27
0
func (this *HttpClient) IconvHtml(html []byte, destEncoding string) ([]byte, error) {

	strReader := bytes.NewReader(html)
	scanner := bufio.NewScanner(strReader)
	re, err := regexp.Compile("charset=([\\w-]+)")

	srcEncoding := ""
	for scanner.Scan() {
		matches := re.FindStringSubmatch(scanner.Text())
		if matches != nil {
			srcEncoding = matches[1]
			break
		}
	}

	if srcEncoding != "" && srcEncoding != destEncoding {
		codec, err := iconv.Open(destEncoding, srcEncoding)
		defer codec.Close()
		u8str := codec.ConvString(string(html))
		return []byte(u8str), err
	}
	return html, err
}
Exemplo n.º 28
0
func NewUTF8ReadCloser(r io.ReadSeeker, encoding string) (io.ReadCloser, error) {
	// rewind Seeker after encoding detection
	defer r.Seek(0, 0)

	// validate encoding for reading
	var found bool
	for _, v := range validEncodings {
		if v == encoding {
			found = true
			break
		}
	}

	if !found {
		return nil, fmt.Errorf(`detected file encoding:"%s", but there is no valid reader`, encoding)
	}

	cd, err := iconv.Open("utf-8", encoding)
	if err != nil {
		return nil, err
	}

	return &UTF8ReadCloser{r: iconv.NewReader(cd, r, 0), c: cd}, nil
}
Exemplo n.º 29
0
func ImportPriview(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
		return
	}
	var info struct {
		Filename string
		Limit    uint
		Offset   uint
	}
	if err := r.DecodeJSONPayload(&info); err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}

	file := "./upload/" + info.Filename

	cd, err := iconv.Open("utf-8", "gbk") // convert gbk to utf8
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}
	defer cd.Close()

	input, err := os.Open(file)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}
	bufSize := 1024 * 1024
	read := iconv.NewReader(cd, input, bufSize)

	r2 := csv.NewReader(read)
	ra, err := r2.ReadAll()
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}

	length := len(ra)

	type Device struct {
		ID              uint
		BatchNumber     string
		Sn              string
		Hostname        string
		Ip              string
		NetworkID       uint
		OsID            uint
		HardwareID      uint
		SystemID        uint
		Location        string
		LocationID      uint
		AssetNumber     string
		Status          string
		InstallProgress float64
		InstallLog      string
		NetworkName     string
		OsName          string
		HardwareName    string
		SystemName      string
		Content         string
	}
	var success []Device
	var failure []Device
	//var result []string
	for i := 1; i < length; i++ {
		//result = append(result, ra[i][0])
		var row Device
		if len(ra[i]) != 8 {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "导入文件格式错误!"
			failure = append(failure, row)
			continue
		}

		row.Sn = strings.TrimSpace(ra[i][0])
		row.Hostname = strings.TrimSpace(ra[i][1])
		row.Ip = strings.TrimSpace(ra[i][2])
		row.OsName = strings.TrimSpace(ra[i][3])
		row.HardwareName = strings.TrimSpace(ra[i][4])
		row.SystemName = strings.TrimSpace(ra[i][5])
		row.Location = strings.TrimSpace(ra[i][6])
		row.AssetNumber = strings.TrimSpace(ra[i][7])

		if len(row.Sn) > 255 {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "SN长度超过255限制!"
		}

		if len(row.Hostname) > 255 {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "主机名长度超过255限制!"
		}

		if len(row.Location) > 255 {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "位置长度超过255限制!"
		}

		if len(row.AssetNumber) > 255 {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "财编长度超过255限制!"
		}

		if row.Sn == "" {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "SN不能为空!"
		}

		if row.Hostname == "" {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "主机名不能为空!"
		}

		if row.Ip == "" {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "IP不能为空!"
		}

		if row.OsName == "" {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "操作系统不能为空!"
		}

		if row.SystemName == "" {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "系统安装模板不能为空!"
		}

		if row.Location == "" {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "位置不能为空!"
		}

		countDevice, err := repo.CountDeviceBySn(row.Sn)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
			return
		}

		if countDevice > 0 {
			ID, err := repo.GetDeviceIdBySn(row.Sn)
			row.ID = ID
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
				return
			}

			//hostname
			countHostname, err := repo.CountDeviceByHostnameAndId(row.Hostname, ID)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误:" + err.Error()})
				return
			}
			if countHostname > 0 {
				var br string
				if row.Content != "" {
					br = "<br />"
				}
				row.Content += br + "该主机名已存在!"
			}

			//IP
			countIp, err := repo.CountDeviceByIpAndId(row.Ip, ID)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
				return
			}

			if countIp > 0 {
				var br string
				if row.Content != "" {
					br = "<br />"
				}
				row.Content += br + "该IP已存在!"
			}
		} else {
			//hostname
			countHostname, err := repo.CountDeviceByHostname(row.Hostname)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误:" + err.Error()})
				return
			}
			if countHostname > 0 {
				var br string
				if row.Content != "" {
					br = "<br />"
				}
				row.Content += br + "该主机名已存在!"
			}

			//IP
			countIp, err := repo.CountDeviceByIp(row.Ip)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
				return
			}

			if countIp > 0 {
				var br string
				if row.Content != "" {
					br = "<br />"
				}
				row.Content += br + "该IP已存在!"
			}
		}

		//匹配网络
		isValidate, err := regexp.MatchString("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$", row.Ip)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
			return
		}

		if !isValidate {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "IP格式不正确!"
		}

		modelIp, err := repo.GetIpByIp(row.Ip)
		if err != nil {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "未匹配到网段!"
		} else {
			network, errNetwork := repo.GetNetworkById(modelIp.NetworkID)
			if errNetwork != nil {
				var br string
				if row.Content != "" {
					br = "<br />"
				}
				row.Content += br + "未匹配到网段!"
			}
			row.NetworkName = network.Network
		}

		//OSName
		countOs, err := repo.CountOsConfigByName(row.OsName)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
			return
		}

		if countOs <= 0 {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "未匹配到操作系统!"
		}

		//SystemName
		countSystem, err := repo.CountSystemConfigByName(row.SystemName)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
			return
		}

		if countSystem <= 0 {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "未匹配到系统安装模板!"
		}

		if row.HardwareName != "" {
			//HardwareName
			countHardware, err := repo.CountHardwarrWithSeparator(row.HardwareName)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
				return
			}

			if countHardware <= 0 {
				var br string
				if row.Content != "" {
					br = "<br />"
				}
				row.Content += br + "未匹配到硬件配置模板!"
			}
		}

		/*
			if row.Location != "" {
				locationId, err := repo.GetLocationIdByName(row.Location)
				if err != nil {
					w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
					return
				}
				if locationId <= 0 {
					var br string
					if row.Content != "" {
						br = "<br />"
					}
					row.Content += br + "未匹配到位置!"
				} else {
					row.LocationID = locationId
				}
			}
		*/

		if row.Content != "" {
			failure = append(failure, row)
		} else {
			success = append(success, row)
		}
	}

	var data []Device
	if len(failure) > 0 {
		data = failure
	} else {
		data = success
	}
	var result []Device
	for i := 0; i < len(data); i++ {
		if uint(i) >= info.Offset && uint(i) < (info.Offset+info.Limit) {
			result = append(result, data[i])
		}
	}

	if len(failure) > 0 {
		w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "设备信息不正确", "recordCount": len(data), "Content": result})
	} else {
		w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功", "recordCount": len(data), "Content": result})
	}
}
Exemplo n.º 30
0
func ImportDevice(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
		return
	}
	var info struct {
		Filename string
	}
	if err := r.DecodeJSONPayload(&info); err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}

	file := "./upload/" + info.Filename

	cd, err := iconv.Open("utf-8", "gbk") // convert gbk to utf8
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}
	defer cd.Close()

	input, err := os.Open(file)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}
	bufSize := 1024 * 1024
	read := iconv.NewReader(cd, input, bufSize)

	r2 := csv.NewReader(read)
	ra, err := r2.ReadAll()
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}

	length := len(ra)

	type Device struct {
		ID              uint
		BatchNumber     string
		Sn              string
		Hostname        string
		Ip              string
		NetworkID       uint
		OsID            uint
		HardwareID      uint
		SystemID        uint
		Location        string
		LocationID      uint
		AssetNumber     string
		Status          string
		InstallProgress float64
		InstallLog      string
		NetworkName     string
		OsName          string
		HardwareName    string
		SystemName      string
		Content         string
		IsSupportVm     string
	}

	batchNumber, err := repo.CreateBatchNumber()
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}
	//var result []string
	for i := 1; i < length; i++ {
		//result = append(result, ra[i][0])
		var row Device

		if len(ra[i]) != 8 {
			continue
		}

		row.Sn = strings.TrimSpace(ra[i][0])
		row.Hostname = strings.TrimSpace(ra[i][1])
		row.Ip = strings.TrimSpace(ra[i][2])
		row.OsName = strings.TrimSpace(ra[i][3])
		row.HardwareName = strings.TrimSpace(ra[i][4])
		row.SystemName = strings.TrimSpace(ra[i][5])
		row.Location = strings.TrimSpace(ra[i][6])
		row.AssetNumber = strings.TrimSpace(ra[i][7])

		if len(row.Sn) > 255 {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "SN长度超过255限制!"
		}

		if len(row.Hostname) > 255 {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "主机名长度超过255限制!"
		}

		if len(row.Location) > 255 {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "位置长度超过255限制!"
		}

		if len(row.AssetNumber) > 255 {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "财编长度超过255限制!"
		}

		if row.Sn == "" {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "SN不能为空!"
		}

		if row.Hostname == "" {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "主机名不能为空!"
		}

		if row.Ip == "" {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "IP不能为空!"
		}

		if row.OsName == "" {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "操作系统不能为空!"
		}

		if row.SystemName == "" {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "系统安装模板不能为空!"
		}

		if row.Location == "" {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "位置不能为空!"
		}

		countDevice, err := repo.CountDeviceBySn(row.Sn)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
			return
		}

		if countDevice > 0 {
			ID, err := repo.GetDeviceIdBySn(row.Sn)
			row.ID = ID
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
				return
			}

			//hostname
			countHostname, err := repo.CountDeviceByHostnameAndId(row.Hostname, ID)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误:" + err.Error()})
				return
			}
			if countHostname > 0 {
				var br string
				if row.Content != "" {
					br = "<br />"
				}
				row.Content += br + "SN:" + row.Sn + "该主机名已存在!"
			}

			//IP
			countIp, err := repo.CountDeviceByIpAndId(row.Ip, ID)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
				return
			}

			if countIp > 0 {
				var br string
				if row.Content != "" {
					br = "<br />"
				}
				row.Content += br + "SN:" + row.Sn + "该IP已存在!"
			}
		} else {
			//hostname
			countHostname, err := repo.CountDeviceByHostname(row.Hostname)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误:" + err.Error()})
				return
			}
			if countHostname > 0 {
				var br string
				if row.Content != "" {
					br = "<br />"
				}
				row.Content += br + "SN:" + row.Sn + "该主机名已存在!"
			}

			//IP
			countIp, err := repo.CountDeviceByIp(row.Ip)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
				return
			}

			if countIp > 0 {
				var br string
				if row.Content != "" {
					br = "<br />"
				}
				row.Content += br + "SN:" + row.Sn + "该IP已存在!"
			}
		}

		//匹配网络
		isValidate, err := regexp.MatchString("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$", row.Ip)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
			return
		}

		if !isValidate {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "SN:" + row.Sn + "IP格式不正确!"
		}

		modelIp, err := repo.GetIpByIp(row.Ip)
		if err != nil {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "SN:" + row.Sn + "未匹配到网段!"
		}

		_, errNetwork := repo.GetNetworkById(modelIp.NetworkID)
		if errNetwork != nil {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "SN:" + row.Sn + "未匹配到网段!"
		}

		row.NetworkID = modelIp.NetworkID

		//OSName
		countOs, err := repo.CountOsConfigByName(row.OsName)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
			return
		}

		if countOs <= 0 {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "SN:" + row.Sn + "未匹配到操作系统!"
		}
		mod, err := repo.GetOsConfigByName(row.OsName)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
			return
		}
		row.OsID = mod.ID

		//SystemName
		countSystem, err := repo.CountSystemConfigByName(row.SystemName)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
			return
		}

		if countSystem <= 0 {
			var br string
			if row.Content != "" {
				br = "<br />"
			}
			row.Content += br + "SN:" + row.Sn + "未匹配到系统安装模板!"
		}

		systemId, err := repo.GetSystemConfigIdByName(row.SystemName)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
			return
		}
		row.SystemID = systemId

		if row.HardwareName != "" {
			//HardwareName
			countHardware, err := repo.CountHardwarrWithSeparator(row.HardwareName)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
				return
			}

			if countHardware <= 0 {
				var br string
				if row.Content != "" {
					br = "<br />"
				}
				row.Content += br + "SN:" + row.Sn + "未匹配到硬件配置模板!"
			}

			hardware, err := repo.GetHardwareBySeaprator(row.HardwareName)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
				return
			}
			row.HardwareID = hardware.ID
		}

		if row.Location != "" {
			locationId, err := repo.GetLocationIdByName(row.Location)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
				return
			}
			if locationId <= 0 {
				/*
					var br string
					if row.Content != "" {
						br = "<br />"
					}
					row.Content += br + "SN:" + row.Sn + " 未匹配到位置!"
				*/
				_, err := repo.ImportLocation(row.Location)
				if err != nil {
					w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
					return
				}
				locationId, err := repo.GetLocationIdByName(row.Location)
				if err != nil {
					w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
					return
				}
				if locationId <= 0 {
					var br string
					if row.Content != "" {
						br = "<br />"
					}
					row.Content += br + "SN:" + row.Sn + " 未匹配到位置!"
				}
				row.LocationID = locationId
			} else {
				row.LocationID = locationId
			}
		}
		if row.Content != "" {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": row.Content})
			return
		} else {
			status := "pre_install"
			row.IsSupportVm = "Yes"
			if countDevice > 0 {
				id, err := repo.GetDeviceIdBySn(row.Sn)
				if err != nil {
					w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
					return
				}

				_, errUpdate := repo.UpdateDeviceById(id, batchNumber, row.Sn, row.Hostname, row.Ip, row.NetworkID, row.OsID, row.HardwareID, row.SystemID, "", row.LocationID, row.AssetNumber, status, row.IsSupportVm)
				if errUpdate != nil {
					w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "操作失败:" + errUpdate.Error()})
					return
				}
			} else {
				_, err := repo.AddDevice(batchNumber, row.Sn, row.Hostname, row.Ip, row.NetworkID, row.OsID, row.HardwareID, row.SystemID, "", row.LocationID, row.AssetNumber, status, row.IsSupportVm)
				if err != nil {
					w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "操作失败:" + err.Error()})
					return
				}
			}
		}
	}

	w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功"})
}