package qiniu

import (
	"time"

	"github.com/qiniu/api.v7/v7/auth/qbox"
	_ "github.com/qiniu/api.v7/v7/conf"
	"github.com/qiniu/api.v7/v7/storage"

	"applet/app/md"
	"applet/app/utils"
)

// 请求图片上传地址信息
func ReqImgUpload(f *md.FileCallback, callbackUrl string) interface{} {
	if ext := utils.FileExt(f.FileName); ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "gif" || ext == "bmp" || ext == "webp" {
		f.Width = "$(imageInfo.width)"
		f.Height = "$(imageInfo.height)"
	}
	f.Provider = "qiniu"
	f.FileSize = "$(fsize)"
	f.Hash = "$(etag)"
	f.Bucket = "$(bucket)"
	f.Mime = "$(mimeType)"
	f.Time = utils.Int64ToStr(time.Now().Unix())
	f.Sign = Sign(f.Time)
	putPolicy := storage.PutPolicy{
		Scope:        BUCKET + ":" + f.FileName, // 使用覆盖方式时候必须请求里面有key,否则报错
		Expires:      Expires,
		ForceSaveKey: true,
		SaveKey:      f.FileName,
		//MimeLimit:        "image/*", // 只允许上传图片
		CallbackURL:      callbackUrl,
		CallbackBody:     utils.SerializeStr(f),
		CallbackBodyType: "application/json",
	}
	return &struct {
		Method string `json:"method"`
		Key    string `json:"key"`
		Host   string `json:"host"`
		Token  string `json:"token"`
	}{Key: f.FileName, Method: "POST", Host: BUCKET_SCHEME + "://" + BUCKET_REGION, Token: putPolicy.UploadToken(qbox.NewMac(AK, SK))}
}

/*
form表单上传
地址 : http://upload-z2.qiniup.com
header
 - Content-Type : multipart/form-data

body :
 - key : 文件名
 - token : 生成token
 - file : 待上传文件
*/
