コード例 #1
0
ファイル: fdfs.go プロジェクト: RaymondChou/OTTStaticServer
//上传文件到fdfs
//conf:配置文件的文件,example:/etc/fdfs/client.conf
//imagePath:需要上传文件的完整路径,example:"/root/Desktop/logo.jpg"
func FdfsUploadFile(conf string, imagePath string) (result map[string]string, err error) {

	result = make(map[string]string)
	var resData C.responseData = C.upload_file(C.CString(conf), C.CString(imagePath))

	// fmt.Println("upload file msg:", C.GoString(resData.msg)) ////当成功的时候,是返回图片的id,example:group1/M00/00/00/wKgBP1NxvSqH9qNuAAAED6CzHYE179.jpg ,当失败的时候是返回错误消息
	// fmt.Println("upload file result:", resData.result)       //1表示成功,0表示失败

	if resData.result == 0 { //上传失败, 返回例子:{"r":{"r":false}}

		err = errors.New(C.GoString(resData.msg))
		return
	} else { //上传成功, 返回例子:"filename":"scree.jpg","group":"group1","url":"M00\/00\/00\/wKgBP1NxvIPf81_1AABJuZk6wJM879.jpg"

		filename := filepath.Base(imagePath)

		strPath := C.GoString(resData.msg)

		result["filename"] = filename
		result["path"] = strPath

		return result, nil
	}

}
コード例 #2
0
//上传文件到fdfs
//conf:配置文件的文件,example:/etc/fdfs/client.conf
//imagePath:需要上传文件的完整路径,example:"/root/Desktop/logo.jpg"
func FdfsUploadFile(conf string, imagePath string) (result map[string]interface{}, err error) {

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

	c_conf := C.CString(conf)
	c_imagePath := C.CString(imagePath)
	var resData C.responseData = C.upload_file(c_conf, c_imagePath)
	defer C.free(unsafe.Pointer(c_conf))
	defer C.free(unsafe.Pointer(c_imagePath))

	fmt.Println("upload file msg:", C.GoString(resData.msg)) ////当成功的时候,是返回图片的id,example:group1/M00/00/00/wKgBP1NxvSqH9qNuAAAED6CzHYE179.jpg ,当失败的时候是返回错误消息
	fmt.Println("upload file result:", resData.result)       //1表示成功,0表示失败

	if resData.result == 0 { //上传失败, 返回例子:{"r":{"r":false}}

		err = errors.New(C.GoString(resData.msg))
		return
	} else { //上传成功, 返回例子:"filename":"scree.jpg","group":"group1","url":"M00\/00\/00\/wKgBP1NxvIPf81_1AABJuZk6wJM879.jpg"

		filename := filepath.Base(imagePath)

		strPath := C.GoString(resData.msg)
		splitStr := strings.Split(strPath, "/")
		groupName := splitStr[0]
		path := ""
		joinStr := ""
		for i := 1; i < len(splitStr); i++ {
			path = path + joinStr + splitStr[i]
			joinStr = "/"
		}
		fmt.Println("group :" + groupName + "")
		fmt.Println("path: " + path)

		result["filename"] = filename
		result["group"] = groupName
		result["url"] = path

		return result, nil
	}

}