first commit

This commit is contained in:
gqc
2025-12-10 14:34:57 +08:00
parent 9b02bd9779
commit c554fd3d3d
13 changed files with 182 additions and 277 deletions
+5 -8
View File
@@ -212,14 +212,13 @@ func alignTo10Seconds(timestamp int64) int64 {
return alignedTime.Unix()
}
// parseSimpleTimestamp 解析简单的时间戳文件名 (如: 20251029181740.mp4)
// parseSimpleTimestamp 解析简单的时间戳文件名
func parseSimpleTimestamp(filename string) (int64, error) {
// 移除文件扩展名
nameWithoutExt := strings.TrimSuffix(filename, filepath.Ext(filename))
// 文件名应该就是14位时间戳
if len(nameWithoutExt) != 14 {
//return 0, fmt.Errorf("无效的时间戳格式: %s (期望14位数字)", nameWithoutExt)
}
// 解析时间戳
@@ -297,7 +296,7 @@ func CleanupOldVideos(deviceUUID string) {
func CleanupOldRawVideos(deviceUUID string) {
rawDir := filepath.Join("/usr/data/camera", deviceUUID)
if _, err := os.Stat(rawDir); os.IsNotExist(err) {
return // 目录不存在,直接返回
return
}
files, err := os.ReadDir(rawDir)
@@ -308,7 +307,7 @@ func CleanupOldRawVideos(deviceUUID string) {
var videoFiles []string
for _, file := range files {
if !file.IsDir() && filepath.Ext(file.Name()) == ".mp4" { // 假设是mp4,也可以加更多判断
if !file.IsDir() && filepath.Ext(file.Name()) == ".mp4" {
videoFiles = append(videoFiles, filepath.Join(rawDir, file.Name()))
}
}
@@ -317,14 +316,12 @@ func CleanupOldRawVideos(deviceUUID string) {
return // 不超过60条,不清理
}
// 按修改时间排序(旧 → 新)
sort.Slice(videoFiles, func(i, j int) bool {
fi, _ := os.Stat(videoFiles[i])
fj, _ := os.Stat(videoFiles[j])
return fi.ModTime().Before(fj.ModTime())
})
// 删除最早的(超过60条的部分)
for i := 0; i < len(videoFiles)-60; i++ {
if err := os.Remove(videoFiles[i]); err != nil {
logger.Logger.Printf("Failed to delete the old original video %s: %v", videoFiles[i], err)
+1 -8
View File
@@ -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"`