diff --git a/video-gen-api/app/api/v1/upload_material.py b/video-gen-api/app/api/v1/upload_material.py index 761dce8b..b8e6c6a2 100644 --- a/video-gen-api/app/api/v1/upload_material.py +++ b/video-gen-api/app/api/v1/upload_material.py @@ -46,6 +46,30 @@ class UpdateFileName(BaseModel): class FileNameUpdateRequest(BaseModel): filenames: list[UpdateFileName] = Field(..., description="批量修改文件名列表,格式: [{\"source_id\":\"资源id\",\"file_name\":\"文件名称\"}]") +# @router.post( +# "/batch-upload", +# summary="批量上传素材到平台", +# description="支持批量上传多个授权账户下的资源到素材库,预留下前测功能", +# ) +# async def batch_upload_material( +# current_user: User = Depends(get_current_user), +# db: AsyncSession = Depends(get_db), +# ) -> Any | dict: +# try: +# result = await upload_material_to_platform( +# ["0019ef7700c503991c9"], +# ["1856633793022992"], +# "0019f018b4b3612e184", +# db, +# current_user.id, +# None, +# ) +# return result +# except Exception as e: +# return { +# "code": 0, +# "message": str(e), +# } @router.post( "/async-batch-upload", diff --git a/video-gen-api/app/services/upload_material_service.py b/video-gen-api/app/services/upload_material_service.py index f571e4a9..d2019f86 100644 --- a/video-gen-api/app/services/upload_material_service.py +++ b/video-gen-api/app/services/upload_material_service.py @@ -334,8 +334,14 @@ async def _upload_to_juliang( file_content = f.read() image_signature = hashlib.md5(file_content).hexdigest() + # data = { + # "advertiser_id": advertiser_id, + # "upload_type": "UPLOAD_BY_FILE", + # "image_signature": image_signature, + # "filename": filename, + # } data = { - "advertiser_id": advertiser_id, + "local_account_id": advertiser_id, "upload_type": "UPLOAD_BY_FILE", "image_signature": image_signature, "filename": filename, @@ -345,7 +351,9 @@ async def _upload_to_juliang( "image_file": (filename, file_content, "image/png"), } - response = await douyin_api.upload_image_material(oauth_id, data, files) + # 上传本地推图片 + response = await douyin_api.upload_local_image_material(oauth_id, data, files) + #response = await douyin_api.upload_image_material(oauth_id, data, files) if response["code"] != 0: return { @@ -416,8 +424,14 @@ async def _upload_to_juliang( file_content = f.read() video_signature = hashlib.md5(file_content).hexdigest() + # data = { + # "advertiser_id": advertiser_id, + # "upload_type": "UPLOAD_BY_FILE", + # "video_signature": video_signature, + # "filename": filename, + # } data = { - "advertiser_id": advertiser_id, + "local_account_id": advertiser_id, "upload_type": "UPLOAD_BY_FILE", "video_signature": video_signature, "filename": filename, @@ -428,6 +442,7 @@ async def _upload_to_juliang( } response = await douyin_api.upload_video_material(oauth_id, data, files) + #response = await douyin_api.upload_local_video_material(oauth_id, data, files) if response["code"] != 0: return { diff --git a/video-gen-api/app/services/upload_queue.py b/video-gen-api/app/services/upload_queue.py index ba9d51cf..016bdbf3 100644 --- a/video-gen-api/app/services/upload_queue.py +++ b/video-gen-api/app/services/upload_queue.py @@ -277,6 +277,20 @@ async def _upload_to_juliang( else: filename = os.path.basename(storage_path) + #查询授权记录 + oauth = await db.execute( + select(UserOAuth).where( + UserOAuth.id == oauth_id, + ) + ) + oauth = oauth.scalar_one_or_none() + if not oauth: + return { + "success": False, + "error": "授权记录不存在" + } + account_role = oauth.account_role + if resource_type == "image": if resource.file_size_bytes > 5 * 1024 * 1024: return { @@ -292,17 +306,21 @@ async def _upload_to_juliang( image_signature = hashlib.md5(file_content).hexdigest() data = { - "advertiser_id": advertiser_id, "upload_type": "UPLOAD_BY_FILE", "image_signature": image_signature, "filename": filename, } - files = { "image_file": (filename, file_content, "image/png"), } - response = await douyin_api.upload_image_material(oauth_id, data, files) + #如果account_role授权角色包含:LOCAL,那么就是本地推接口,其他是广告千川接口 + if "LOCAL" in account_role: + data["local_account_id"] = advertiser_id + response = await douyin_api.upload_local_image_material(oauth_id, data, files) + else: + data["advertiser_id"] = advertiser_id + response = await douyin_api.upload_image_material(oauth_id, data, files) if response["code"] != 0: return { @@ -375,17 +393,20 @@ async def _upload_to_juliang( video_signature = hashlib.md5(file_content).hexdigest() data = { - "advertiser_id": advertiser_id, "upload_type": "UPLOAD_BY_FILE", "video_signature": video_signature, "filename": filename, } - files = { "video_file": (filename, file_content, "video/mp4"), } - response = await douyin_api.upload_video_material(oauth_id, data, files) + if "LOCAL" in account_role: + data["local_account_id"] = advertiser_id + response = await douyin_api.upload_local_video_material(oauth_id, data, files) + else: + data["advertiser_id"] = advertiser_id + response = await douyin_api.upload_video_material(oauth_id, data, files) if response["code"] != 0: return { diff --git a/video-gen-api/app/utils/douyinApi.py b/video-gen-api/app/utils/douyinApi.py index b09212b4..ab777ee0 100644 --- a/video-gen-api/app/utils/douyinApi.py +++ b/video-gen-api/app/utils/douyinApi.py @@ -19,6 +19,24 @@ class DouyinApi: 'GET', {'params': params or {}} ) + #上传本地推图片 + async def upload_local_image_material(self, oauth_id: str, data: Optional[Dict[str, Any]] = None, files: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: + if not oauth_id: + raise RuntimeError('OAuth ID is not set.') + + url = "https://api.oceanengine.com/open_api/v3.0/local/image/upload/" + options: Dict[str, Any] = {} + if data: + options['data'] = data + if files: + options['files'] = files + return await self.request.request_with_token_with_context( + oauth_id, + url, + 'POST', + options, + request_count = 3 + ) async def upload_image_material(self, oauth_id: str, data: Optional[Dict[str, Any]] = None, files: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: if not oauth_id: @@ -57,6 +75,26 @@ class DouyinApi: request_count = 3 ) + #上传本地推视频素材 + async def upload_local_video_material(self, oauth_id: str, data: Optional[Dict[str, Any]] = None, files: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: + if not oauth_id: + raise RuntimeError('OAuth ID is not set.') + + url = "https://api.oceanengine.com/open_api/v3.0/local/file/video/upload/" + options: Dict[str, Any] = {} + if data: + options['data'] = data + if files: + options['files'] = files + return await self.request.request_with_token_with_context( + oauth_id, + url, + 'POST', + options, + request_count = 3 + ) + + #获取区域信息 async def get_area(self, oauth_id: str, params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: if not oauth_id: