//写入文件,保存
func WriteFile(path string, base64_image_content string) bool {
b, _ := regexp.MatchString(`^data:\s*image\/(\w+);base64,`, base64_image_content)
if !b {
return false
}
re, _ := regexp.Compile(`^data:\s*image\/(\w+);base64,`)
allData := re.FindAllSubmatch([]byte(base64_image_content), 2)
fileType := string(allData[0][1]) //png ,jpeg 后缀获取
base64Str := re.ReplaceAllString(base64_image_content, "")
date := time.Now().Format("20060102")
if ok := IsFileExist(path + "/" + date); !ok {
os.Mkdir(path+"/"+date, 0666)
}
var file string = path + "/" + date + "/" + strconv.FormatInt(time.Now().Unix(),10) + strconv.Itoa(rand.Intn(999999-100000)+100000) + "." + fileType
byte, _ := base64.StdEncoding.DecodeString(base64Str)
err := ioutil.WriteFile(file, byte, 0666)
if err != nil {
return false
}
return true
}
//判断文件是否存在
func IsFileExist(filename string) bool {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return true
}