github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/misc/pic_file/get_contents_from_base64.go (about)

     1  package pic_file
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  //文件内容识别
     9  func GetContentTypeAndContentStr(content string) (content_type string, content_str string, err error) {
    10  	if !strings.HasPrefix(content, DATA_PEFIX) {
    11  		return "", "", fmt.Errorf("content has no data prefix")
    12  	}
    13  
    14  	var encoding_index int
    15  	if encoding_index = strings.Index(content, ENCODING_DESC); encoding_index == -1 {
    16  		return "", "", fmt.Errorf("content has no base64 encode descx")
    17  	}
    18  
    19  	image_type := content[len(DATA_PEFIX):encoding_index]
    20  	content_type = image_type[len("image/"):]
    21  
    22  	content_str = content[encoding_index+len(ENCODING_DESC):]
    23  	return
    24  }
    25  
    26  const (
    27  	DATA_PEFIX    string = "data:"
    28  	ENCODING_DESC string = ";base64,"
    29  )