Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(upload-post): retain upload tracking IDs after lost responses

Rudy Celeklib7a55e5f5ce9
diff --git a/app/services/upload_post.py b/app/services/upload_post.py
index 33895944..85e0d9d6 100644
--- a/app/services/upload_post.py
+++ b/app/services/upload_post.py
@@ -6,6 +6,7 @@ Docs: https://docs.upload-post.com
import os
import time
from typing import Callable, Optional
+from uuid import uuid4
import requests
from loguru import logger
@@ -185,12 +186,16 @@ class UploadPostService:
logger.info(f"Cross-posting video to {', '.join(platforms)} via Upload-Post...")
+ # Generate the remote handle before POST: a lost response does not
+ # prove that Upload-Post stopped publishing the received video.
+ client_request_id = str(uuid4())
try:
with open(video_path, 'rb') as video_file:
files = {'video': video_file}
data = [
('user', self.username),
+ ('request_id', client_request_id),
('title', title[:2200]),
('privacy_level', privacy_level),
]
@@ -297,7 +302,22 @@ class UploadPostService:
except requests.exceptions.RequestException as e:
logger.error(f"Failed to cross-post video: {str(e)}")
- return {"success": False, "error": str(e)}
+ uncertain_outcome = isinstance(
+ e, (requests.exceptions.Timeout, requests.exceptions.ConnectionError)
+ ) or (
+ e.response is not None and e.response.status_code >= 500
+ )
+ error = str(e)
+ if uncertain_outcome:
+ error += (
+ "; upload outcome is unconfirmed; "
+ f"check request_id {client_request_id} before submitting again"
+ )
+ return {
+ "success": False,
+ "request_id": client_request_id,
+ "error": error,
+ }
def check_status(self, request_id: str) -> dict:
"""
diff --git a/test/services/test_upload_post_timeout_id.py b/test/services/test_upload_post_timeout_id.py
new file mode 100644
index 00000000..cf7fd60d
--- /dev/null
+++ b/test/services/test_upload_post_timeout_id.py
@@ -0,0 +1,62 @@
+import tempfile
+import unittest
+from pathlib import Path
+from unittest.mock import Mock, patch
+from uuid import UUID
+
+import requests
+
+from app.services.upload_post import UploadPostService
+
+
+class TestUploadPostTimeoutId(unittest.TestCase):
+ def test_lost_upload_response_retains_a_client_supplied_tracking_id(self):
+ config = {"upload_post_enabled": True, "upload_post_api_key": "test-key", "upload_post_username": "creator"}
+ for error in (requests.exceptions.ReadTimeout("response lost"), requests.exceptions.ConnectionError("connection dropped")):
+ with self.subTest(error=type(error).__name__), tempfile.TemporaryDirectory() as directory:
+ video = Path(directory) / "video.mp4"
+ video.write_bytes(b"local fixture")
+ with patch("app.services.upload_post.config.app", config), patch(
+ "app.services.upload_post.requests.post", side_effect=error
+ ) as post:
+ result = UploadPostService().upload_video(str(video), "title", platforms=["tiktok"])
+ self.assertEqual(post.call_count, 1)
+ request_id = dict(post.call_args.kwargs["data"]).get("request_id")
+ self.assertTrue(request_id, "Send a client ID before the response can be lost")
+ UUID(request_id)
+ self.assertEqual(result["request_id"], request_id)
+ self.assertFalse(result["success"])
+ self.assertIn("unconfirmed", result["error"])
+ self.assertIn(request_id, result["error"])
+ self.assertTrue(post.call_args.kwargs["files"]["video"].closed)
+
+ def test_definitive_client_rejection_is_not_labelled_unconfirmed(self):
+ config = {"upload_post_enabled": True, "upload_post_api_key": "test-key", "upload_post_username": "creator"}
+ with tempfile.TemporaryDirectory() as directory:
+ video = Path(directory) / "video.mp4"
+ video.write_bytes(b"local fixture")
+ response = Mock(status_code=400)
+ response.raise_for_status.side_effect = requests.exceptions.HTTPError("bad platform", response=response)
+ with patch("app.services.upload_post.config.app", config), patch(
+ "app.services.upload_post.requests.post", return_value=response
+ ) as post:
+ result = UploadPostService().upload_video(str(video), "title", platforms=["tiktok"])
+ self.assertFalse(result["success"])
+ self.assertNotIn("unconfirmed", result["error"])
+ self.assertEqual(post.call_count, 1)
+
+ def test_gateway_timeout_retains_id_without_republishing(self):
+ config = {"upload_post_enabled": True, "upload_post_api_key": "test-key", "upload_post_username": "creator"}
+ with tempfile.TemporaryDirectory() as directory:
+ video = Path(directory) / "video.mp4"
+ video.write_bytes(b"local fixture")
+ response = Mock(status_code=504)
+ response.raise_for_status.side_effect = requests.exceptions.HTTPError("gateway timeout", response=response)
+ with patch("app.services.upload_post.config.app", config), patch(
+ "app.services.upload_post.requests.post", return_value=response
+ ) as post:
+ result = UploadPostService().upload_video(str(video), "title", platforms=["tiktok"])
+ self.assertFalse(result["success"])
+ self.assertIn("unconfirmed", result["error"])
+ self.assertEqual(result["request_id"], dict(post.call_args.kwargs["data"])["request_id"])
+ self.assertEqual(post.call_count, 1)