first commit
This commit is contained in:
@@ -16,18 +16,16 @@ import (
|
||||
|
||||
// UploadToNginx 上传视频到 Nginx 服务器,返回 URL
|
||||
func UploadToNginx(filePath string, eventParams map[string]any, uploadURL string) (string, error) {
|
||||
// === 1. 打开文件 ===
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("open file failed: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// === 2. 创建表单 ===
|
||||
body := new(bytes.Buffer)
|
||||
writer := multipart.NewWriter(body)
|
||||
|
||||
// --- 上传文件 ---
|
||||
part, err := writer.CreateFormFile("file", filepath.Base(filePath))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create form file failed: %w", err)
|
||||
@@ -36,7 +34,6 @@ func UploadToNginx(filePath string, eventParams map[string]any, uploadURL string
|
||||
return "", fmt.Errorf("copy file failed: %w", err)
|
||||
}
|
||||
|
||||
// --- 上传 JSON ---
|
||||
eventJSON, err := json.Marshal(eventParams)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("marshal event failed: %w", err)
|
||||
@@ -52,12 +49,10 @@ func UploadToNginx(filePath string, eventParams map[string]any, uploadURL string
|
||||
return "", fmt.Errorf("close writer failed: %w", err)
|
||||
}
|
||||
|
||||
// === 3. 打印请求头 ===
|
||||
logger.Logger.Printf("[HTTP Request]URL: %s", uploadURL)
|
||||
logger.Logger.Printf("[HTTP Request]Content-Type: %s", writer.FormDataContentType())
|
||||
logger.Logger.Printf("[HTTP Request]Body 长度: %d bytes", body.Len())
|
||||
|
||||
// === 4. 发送请求 ===
|
||||
req, err := http.NewRequest("POST", uploadURL, body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create request failed: %w", err)
|
||||
@@ -71,7 +66,6 @@ func UploadToNginx(filePath string, eventParams map[string]any, uploadURL string
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// === 5. 打印响应 ===
|
||||
respBody, _ := io.ReadAll(resp.Body)
|
||||
logger.Logger.Printf("[nginx response]Status: %d", resp.StatusCode)
|
||||
logger.Logger.Printf("[nginx response]Body: %s", string(respBody))
|
||||
@@ -80,7 +74,6 @@ func UploadToNginx(filePath string, eventParams map[string]any, uploadURL string
|
||||
return "", fmt.Errorf("nginx error %d: %s", resp.StatusCode, string(respBody))
|
||||
}
|
||||
|
||||
// === 6. 解析 video_url ===
|
||||
var result struct {
|
||||
Code int `json:"code"`
|
||||
Result json.RawMessage `json:"result"`
|
||||
|
||||
Reference in New Issue
Block a user