You've already forked VptPassiveAdapter
- 在UploadFileToOSS函数中新增contentType参数 - 设置请求头Content-Length和Content-Type - 为图片上传指定image/jpeg类型 - 增加上传失败时的日志记录 - 引入zap日志库支持结构化日志输出
216 lines
5.4 KiB
Go
216 lines
5.4 KiB
Go
package api
|
|
|
|
import (
|
|
"ZhenTuLocalPassiveAdapter/config"
|
|
"ZhenTuLocalPassiveAdapter/logger"
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// VIID Request/Response Structures
|
|
|
|
type DeviceIdObject struct {
|
|
DeviceID string `json:"DeviceID"`
|
|
}
|
|
|
|
type RegisterRequest struct {
|
|
RegisterObject DeviceIdObject `json:"RegisterObject"`
|
|
}
|
|
|
|
type KeepaliveRequest struct {
|
|
KeepaliveObject DeviceIdObject `json:"KeepaliveObject"`
|
|
}
|
|
|
|
type UnRegisterRequest struct {
|
|
UnRegisterObject DeviceIdObject `json:"UnRegisterObject"`
|
|
}
|
|
|
|
type VIIDBaseResponse struct {
|
|
ResponseStatusObject ResponseStatusObject `json:"ResponseStatusObject"`
|
|
}
|
|
|
|
type ResponseStatusObject struct {
|
|
ID string `json:"Id"`
|
|
RequestURL string `json:"RequestURL"`
|
|
StatusCode string `json:"StatusCode"`
|
|
StatusString string `json:"StatusString"`
|
|
LocalTime string `json:"LocalTime"`
|
|
}
|
|
|
|
type SystemTimeResponse struct {
|
|
ResponseStatusObject struct {
|
|
LocalTime string `json:"LocalTime"` // yyyyMMddHHmmss
|
|
} `json:"ResponseStatusObject"`
|
|
}
|
|
|
|
// VIID Client Methods
|
|
|
|
func ProxyRequest(ctx context.Context, method, path string, body io.Reader, header http.Header) (*http.Response, error) {
|
|
url := fmt.Sprintf("%s%s", config.Config.Viid.ServerUrl, path)
|
|
req, err := http.NewRequestWithContext(ctx, method, url, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Copy headers
|
|
for k, v := range header {
|
|
req.Header[k] = v
|
|
}
|
|
|
|
return GetAPIClient().Do(req)
|
|
}
|
|
|
|
// Upload Proxy Structures
|
|
|
|
type UploadConfig struct {
|
|
TaskID int64 `json:"taskId"`
|
|
FaceUploadURL string `json:"faceUploadUrl"`
|
|
ThumbnailUploadURL string `json:"thumbnailUploadUrl"`
|
|
SourceUploadURL string `json:"sourceUploadUrl"`
|
|
ExpiresAt time.Time `json:"expiresAt"`
|
|
}
|
|
|
|
type ProxyResponse struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Success bool `json:"success"`
|
|
Data *UploadConfig `json:"data"` // Data can be UploadConfig or string depending on endpoint
|
|
}
|
|
|
|
type FacePositionInfo struct {
|
|
LeftTopX int `json:"leftTopX"`
|
|
LeftTopY int `json:"leftTopY"`
|
|
RightBtmX int `json:"rightBtmX"`
|
|
RightBtmY int `json:"rightBtmY"`
|
|
ImgWidth int `json:"imgWidth"`
|
|
ImgHeight int `json:"imgHeight"`
|
|
ShotTime string `json:"shotTime"` // yyyyMMddHHmmss
|
|
}
|
|
|
|
type SubmitResultRequest struct {
|
|
TaskID int64 `json:"taskId"`
|
|
FacePosition FacePositionInfo `json:"facePosition"`
|
|
}
|
|
|
|
type SubmitFailureRequest struct {
|
|
TaskID int64 `json:"taskId"`
|
|
ErrorCode string `json:"errorCode"`
|
|
ErrorMessage string `json:"errorMessage"`
|
|
}
|
|
|
|
// Upload Proxy Methods
|
|
|
|
func GetUploadConfig(ctx context.Context, scenicId int64, deviceNo string) (*UploadConfig, error) {
|
|
url := fmt.Sprintf("%s/proxy/VIID/upload-config?scenicId=%d&deviceNo=%s", config.Config.Viid.ServerUrl, scenicId, deviceNo)
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := GetAPIClient().Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var result ProxyResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !result.Success {
|
|
return nil, fmt.Errorf("get upload config failed: %s", result.Message)
|
|
}
|
|
|
|
return result.Data, nil
|
|
}
|
|
|
|
func SubmitResult(ctx context.Context, taskID int64, facePos FacePositionInfo) error {
|
|
url := fmt.Sprintf("%s/proxy/VIID/submit-result", config.Config.Viid.ServerUrl)
|
|
reqData := SubmitResultRequest{
|
|
TaskID: taskID,
|
|
FacePosition: facePos,
|
|
}
|
|
|
|
jsonData, _ := json.Marshal(reqData)
|
|
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := GetAPIClient().Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var result struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Success bool `json:"success"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !result.Success {
|
|
return fmt.Errorf("submit result failed: %s", result.Message)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func SubmitFailure(ctx context.Context, taskID int64, errorCode, errorMessage string) error {
|
|
url := fmt.Sprintf("%s/proxy/VIID/submit-failure", config.Config.Viid.ServerUrl)
|
|
reqData := SubmitFailureRequest{
|
|
TaskID: taskID,
|
|
ErrorCode: errorCode,
|
|
ErrorMessage: errorMessage,
|
|
}
|
|
|
|
jsonData, _ := json.Marshal(reqData)
|
|
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := GetAPIClient().Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
return nil
|
|
}
|
|
|
|
func UploadFileToOSS(ctx context.Context, uploadUrl string, data []byte, contentType string) error {
|
|
req, err := http.NewRequestWithContext(ctx, "PUT", uploadUrl, bytes.NewReader(data))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Length", fmt.Sprintf("%d", len(data)))
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
resp, err := GetUploadClient().Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
|
body, _ := io.ReadAll(resp.Body)
|
|
logger.Error("oss upload failed", zap.String("body", string(body)))
|
|
return fmt.Errorf("oss upload failed: %d", resp.StatusCode)
|
|
}
|
|
return nil
|
|
}
|