Commit 02f90c5a authored by shajiaiming's avatar shajiaiming

fix

parent 648438c7
package export
import "bwallet/pkg/setting"
const EXT = ".xlsx"
// GetExcelFullUrl get the full access path of the Excel file
func GetExcelFullUrl(name string) string {
return setting.AppSetting.PrefixUrl + "/" + GetExcelPath() + name
}
// GetExcelPath get the relative save path of the Excel file
func GetExcelPath() string {
return setting.AppSetting.ExportSavePath
}
// GetExcelFullPath Get the full save path of the Excel file
func GetExcelFullPath() string {
return setting.AppSetting.RuntimeRootPath + GetExcelPath()
}
......@@ -84,6 +84,7 @@ func Setup() {
mapTo("server", ServerSetting)
mapTo("database", DatabaseSetting)
mapTo("redis", RedisSetting)
mapTo("oss", OssSetting)
AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024
ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
......
......@@ -9,6 +9,7 @@ import (
"bwallet/pkg/e"
"bwallet/pkg/logging"
"bwallet/pkg/upload"
"fmt"
)
// @Summary Import Image
......@@ -36,6 +37,12 @@ func UploadImage(c *gin.Context) {
savePath := upload.GetImagePath()
src := fullPath + imageName
//fmt.Println(imageName)
//fmt.Println(fullPath)
//fmt.Println(savePath)
//fmt.Println(src)
//os.Exit(0)
if !upload.CheckImageExt(imageName) || !upload.CheckImageSize(file) {
appG.Response(http.StatusBadRequest, e.ERROR_UPLOAD_CHECK_IMAGE_FORMAT, nil)
return
......@@ -54,6 +61,8 @@ func UploadImage(c *gin.Context) {
return
}
fmt.Println(upload.GetImageFullUrl(imageName))
fmt.Println(savePath + imageName)
appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
"image_url": upload.GetImageFullUrl(imageName),
"image_save_url": savePath + imageName,
......
......@@ -6,13 +6,19 @@ import (
"bwallet/pkg/app"
"bwallet/pkg/e"
"bwallet/pkg/setting"
"bwallet/pkg/upload"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"fmt"
"os"
)
func UploadImage(c *gin.Context) {
func Upload(c *gin.Context) {
appG := app.Gin{C: c}
accessKeyId := setting.OssSetting.AccessKeyId
accessKeySecret := setting.OssSetting.AccessKeySecret
endpoint := setting.OssSetting.Endpoint
bucketName := setting.OssSetting.Bucket
file, image, err := c.Request.FormFile("image")
if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR, nil)
......@@ -24,19 +30,52 @@ func UploadImage(c *gin.Context) {
return
}
accessKeyId := setting.OssSetting.AccessKeyId
accessKeySecret := setting.OssSetting.AccessKeySecret
endpoint := setting.OssSetting.Endpoint
//bucket := setting.OssSetting.Bucket
imageName := upload.GetImageName(image.Filename)
fullPath := upload.GetImageFullPath()
//savePath := upload.GetImagePath()
src := fullPath + imageName
if !upload.CheckImageExt(imageName) || !upload.CheckImageSize(file) {
appG.Response(http.StatusBadRequest, e.ERROR_UPLOAD_CHECK_IMAGE_FORMAT, nil)
return
}
err = upload.CheckImage(fullPath)
if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_UPLOAD_CHECK_IMAGE_FAIL, nil)
return
}
if err := c.SaveUploadedFile(image, src); err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_UPLOAD_SAVE_IMAGE_FAIL, nil)
return
}
client, err := oss.New(endpoint, accessKeyId, accessKeySecret)
if err != nil {
fmt.Println(err)
os.Exit(0)
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return
}
fmt.Println(client)
os.Exit(0)
bucket, err := client.Bucket(bucketName)
if err != nil {
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return
}
uploaddir := "upload/test"
objectName := fmt.Sprintf("%s/%s", uploaddir, imageName) //完整的oss路径
err = bucket.PutObjectFromFile(objectName, upload.GetImageFullUrl(imageName))
if err != nil {
fmt.Println("aaaaaa")
fmt.Println(err)
return
}
var resultfile = objectName
fmt.Println(imageName)
fmt.Println(resultfile)
appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
"image_url": resultfile,
"image_name": imageName,
})
}
......@@ -6,6 +6,8 @@ import (
"bwallet/routers/api"
"bwallet/routers/api/v1"
"bwallet/middleware/auth"
"net/http"
"bwallet/pkg/upload"
)
func InitRouter() *gin.Engine {
......@@ -15,7 +17,7 @@ func InitRouter() *gin.Engine {
r.Use(gin.Recovery())
//r.StaticFS("/upload/images", http.Dir(upload.GetImageFullPath()))
r.StaticFS("/upload/images", http.Dir(upload.GetImageFullPath()))
//r.StaticFS("/qrcode", http.Dir(qrcode.GetQrCodeFullPath()))
r.POST("/auth", api.GetAuth)
......@@ -25,7 +27,7 @@ func InitRouter() *gin.Engine {
api.Use(auth.AUTH())
{
api.POST("/upload",v1.UploadImage)
//api.POST("/upload",v1.Upload)
api.GET("/coins", v1.GetCoins)
api.POST("/coin", v1.AddCoin)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment