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()
}
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
type App struct { type App struct {
JwtSecret string JwtSecret string
PageSize int PageSize int
Timeout int Timeout int
PrefixUrl string PrefixUrl string
RuntimeRootPath string RuntimeRootPath string
...@@ -62,10 +62,10 @@ type Redis struct { ...@@ -62,10 +62,10 @@ type Redis struct {
var RedisSetting = &Redis{} var RedisSetting = &Redis{}
type Oss struct { type Oss struct {
AccessKeyId string AccessKeyId string
AccessKeySecret string AccessKeySecret string
Endpoint string Endpoint string
Bucket string Bucket string
} }
var OssSetting = &Oss{} var OssSetting = &Oss{}
...@@ -84,6 +84,7 @@ func Setup() { ...@@ -84,6 +84,7 @@ func Setup() {
mapTo("server", ServerSetting) mapTo("server", ServerSetting)
mapTo("database", DatabaseSetting) mapTo("database", DatabaseSetting)
mapTo("redis", RedisSetting) mapTo("redis", RedisSetting)
mapTo("oss", OssSetting)
AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024 AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024
ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"bwallet/pkg/e" "bwallet/pkg/e"
"bwallet/pkg/logging" "bwallet/pkg/logging"
"bwallet/pkg/upload" "bwallet/pkg/upload"
"fmt"
) )
// @Summary Import Image // @Summary Import Image
...@@ -36,6 +37,12 @@ func UploadImage(c *gin.Context) { ...@@ -36,6 +37,12 @@ func UploadImage(c *gin.Context) {
savePath := upload.GetImagePath() savePath := upload.GetImagePath()
src := fullPath + imageName 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) { if !upload.CheckImageExt(imageName) || !upload.CheckImageSize(file) {
appG.Response(http.StatusBadRequest, e.ERROR_UPLOAD_CHECK_IMAGE_FORMAT, nil) appG.Response(http.StatusBadRequest, e.ERROR_UPLOAD_CHECK_IMAGE_FORMAT, nil)
return return
...@@ -54,6 +61,8 @@ func UploadImage(c *gin.Context) { ...@@ -54,6 +61,8 @@ func UploadImage(c *gin.Context) {
return return
} }
fmt.Println(upload.GetImageFullUrl(imageName))
fmt.Println(savePath + imageName)
appG.Response(http.StatusOK, e.SUCCESS, map[string]string{ appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
"image_url": upload.GetImageFullUrl(imageName), "image_url": upload.GetImageFullUrl(imageName),
"image_save_url": savePath + imageName, "image_save_url": savePath + imageName,
......
...@@ -6,13 +6,19 @@ import ( ...@@ -6,13 +6,19 @@ import (
"bwallet/pkg/app" "bwallet/pkg/app"
"bwallet/pkg/e" "bwallet/pkg/e"
"bwallet/pkg/setting" "bwallet/pkg/setting"
"bwallet/pkg/upload"
"github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/aliyun/aliyun-oss-go-sdk/oss"
"fmt" "fmt"
"os"
) )
func UploadImage(c *gin.Context) { func Upload(c *gin.Context) {
appG := app.Gin{C: c} 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") file, image, err := c.Request.FormFile("image")
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR, nil) appG.Response(http.StatusInternalServerError, e.ERROR, nil)
...@@ -24,19 +30,52 @@ func UploadImage(c *gin.Context) { ...@@ -24,19 +30,52 @@ func UploadImage(c *gin.Context) {
return return
} }
accessKeyId := setting.OssSetting.AccessKeyId imageName := upload.GetImageName(image.Filename)
accessKeySecret := setting.OssSetting.AccessKeySecret fullPath := upload.GetImageFullPath()
endpoint := setting.OssSetting.Endpoint //savePath := upload.GetImagePath()
//bucket := setting.OssSetting.Bucket 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) client, err := oss.New(endpoint, accessKeyId, accessKeySecret)
if err != nil { if err != nil {
fmt.Println(err)
os.Exit(0)
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil) appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return return
} }
fmt.Println(client)
os.Exit(0) bucket, err := client.Bucket(bucketName)
return 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 ( ...@@ -6,6 +6,8 @@ import (
"bwallet/routers/api" "bwallet/routers/api"
"bwallet/routers/api/v1" "bwallet/routers/api/v1"
"bwallet/middleware/auth" "bwallet/middleware/auth"
"net/http"
"bwallet/pkg/upload"
) )
func InitRouter() *gin.Engine { func InitRouter() *gin.Engine {
...@@ -15,7 +17,7 @@ func InitRouter() *gin.Engine { ...@@ -15,7 +17,7 @@ func InitRouter() *gin.Engine {
r.Use(gin.Recovery()) 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.StaticFS("/qrcode", http.Dir(qrcode.GetQrCodeFullPath()))
r.POST("/auth", api.GetAuth) r.POST("/auth", api.GetAuth)
...@@ -25,7 +27,7 @@ func InitRouter() *gin.Engine { ...@@ -25,7 +27,7 @@ func InitRouter() *gin.Engine {
api.Use(auth.AUTH()) api.Use(auth.AUTH())
{ {
api.POST("/upload",v1.UploadImage) //api.POST("/upload",v1.Upload)
api.GET("/coins", v1.GetCoins) api.GET("/coins", v1.GetCoins)
api.POST("/coin", v1.AddCoin) 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