Files
VptPassiveAdapter/api/viid_client.go
Jerry Yan f10b68e487 refactor(api): 移除上传文件时的Content-Type参数
- 删除UploadFileToOSS函数中的contentType参数
- 更新所有调用UploadFileToOSS的地方,移除传递的Content-Type值
- 简化上传逻辑,不再手动设置HTTP请求头中的Content-Type
- 依赖服务器端自动检测文件类型进行处理
2025-11-24 17:56:05 +08:00

209 lines
5.1 KiB
Go

package api
import (
"ZhenTuLocalPassiveAdapter/config"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// 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) error {
req, err := http.NewRequestWithContext(ctx, "PUT", uploadUrl, bytes.NewReader(data))
if err != nil {
return err
}
resp, err := GetUploadClient().Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return fmt.Errorf("oss upload failed: %d", resp.StatusCode)
}
return nil
}