Golang implements asynchronous upload of files and supports progress bar query

Keywords: encoding

Article directory

Business background

Business requirements require the development of an asynchronous upload file interface, and support the query of upload progress.

requirement analysis

ZIP compression package contains a CSV file and a picture folder. Requirements: parse the CSV data into mongo, and correspond the picture information in the picture folder to the personnel information in csv.

ZIP Compression Packet Decompression

Decompression using golang's own "archive/zip" package.

func decompressZip(filePath, dest string) (string, string, error) {

	var csvName string
	imageFolder := path.Base(filePath)
	ext := path.Ext(filePath)
	folderName := strings.TrimSuffix(imageFolder, ext)
	src, err := os.Open(filePath)
	if err != nil {
		return "", "", err
	}
	defer src.Close()

	zipFile, err := zip.OpenReader(src.Name())
	if err != nil {
		return "", "", err
	}
	defer zipFile.Close()

	err = os.MkdirAll(path.Join(dest, folderName), os.ModePerm)
	for _, innerFile := range zipFile.File {
		info := innerFile.FileInfo()
		if info.IsDir() {
			continue
		}
		dst, err := os.Create(path.Join(dest, folderName, info.Name()))
		if err != nil {
			logManager.Error(err.Error())
			continue
		}
		src, err := innerFile.Open()
		if err != nil {
			logManager.Error(err.Error())
			continue
		}
		io.Copy(dst, src)
	}
	destPath, err := ioutil.ReadDir(path.Join(dest, folderName))
	if err != nil {
		return "", "", err
	}
	for _, v := range destPath {
		if path.Ext(v.Name()) == ".csv" {
			csvName = path.Join(dest, folderName, v.Name())
		}
	}
	return folderName, csvName, nil
}

Verify that the encoding format of csv file is UTF-8

func ValidUTF8(buf []byte) bool {
	nBytes := 0
	for i := 0; i < len(buf); i++ {
		if nBytes == 0 {
			if (buf[i] & 0x80) != 0 { //It is not 0 after operation, indicating that the first place is 1.
				for (buf[i] & 0x80) != 0 {
					buf[i] <<= 1 //Move left one.
					nBytes++     //Record characters take up several bytes altogether
				}

				if nBytes < 2 || nBytes > 6 { //Because UTF8 encoding a single character does not exceed 6 bytes at most
					return false
				}

				nBytes-- //Minus a count of the first byte
			}
		} else { //Processing multi-byte characters
			if buf[i]&0xc0 != 0x80 { //Determine whether the byte after the multi-byte is the beginning of 10
				return false
			}
			nBytes--
		}
	}
	return nBytes == 0
}

Posted by SharkBait on Sun, 06 Oct 2019 18:13:25 -0700