| File was renamed from aigc-director/aigc-claw/backend/tool/image_processor.py |
| | |
| | | from PIL import Image |
| | | import logging |
| | | |
| | | logger = logging.getLogger(__name__) |
| | | |
| | | |
| | | class ImageProcessor: |
| | | """ |
| | |
| | | for chunk in response.iter_content(chunk_size=8192): |
| | | if chunk: |
| | | file.write(chunk) |
| | | print(f"✓ 图片下载成功: {save_path}") |
| | | logger.info("Image downloaded: %s", save_path) |
| | | return True |
| | | else: |
| | | print(f"下载失败,状态码: {response.status_code}") |
| | | logger.warning("Image download failed: status=%s url=%s", response.status_code, image_url) |
| | | |
| | | except requests.exceptions.SSLError as e: |
| | | print(f"SSL错误 (尝试 {attempt + 1}/{max_retries}): {str(e)[:100]}") |
| | | logger.warning("Image download SSL error: attempt=%d/%d error=%s", attempt + 1, max_retries, str(e)[:100]) |
| | | if attempt < max_retries - 1: |
| | | wait_time = (attempt + 1) * 2 |
| | | print(f"等待 {wait_time} 秒后重试...") |
| | | logger.info("Retrying image download in %s seconds", wait_time) |
| | | time.sleep(wait_time) |
| | | else: |
| | | print("尝试禁用SSL验证重新下载...") |
| | | logger.warning("Retrying image download with SSL verification disabled") |
| | | try: |
| | | response = requests.get( |
| | | image_url, |
| | |
| | | for chunk in response.iter_content(chunk_size=8192): |
| | | if chunk: |
| | | file.write(chunk) |
| | | print(f"✓ 图片下载成功(已禁用SSL验证): {save_path}") |
| | | logger.info("Image downloaded with SSL verification disabled: %s", save_path) |
| | | return True |
| | | except Exception as fallback_error: |
| | | print(f"禁用SSL验证后仍然失败: {fallback_error}") |
| | | logger.exception("Image download failed after disabling SSL verification: %s", fallback_error) |
| | | raise |
| | | |
| | | except requests.exceptions.Timeout as e: |
| | | print(f"超时错误 (尝试 {attempt + 1}/{max_retries}): {e}") |
| | | logger.warning("Image download timeout: attempt=%d/%d error=%s", attempt + 1, max_retries, e) |
| | | if attempt < max_retries - 1: |
| | | time.sleep((attempt + 1) * 2) |
| | | else: |
| | | raise |
| | | |
| | | except Exception as e: |
| | | print(f"下载错误 (尝试 {attempt + 1}/{max_retries}): {e}") |
| | | logger.warning("Image download error: attempt=%d/%d error=%s", attempt + 1, max_retries, e) |
| | | if attempt < max_retries - 1: |
| | | time.sleep((attempt + 1) * 2) |
| | | else: |
| | |
| | | # 3. 计算过期时间 |
| | | expire_time = datetime.now() + timedelta(hours=48) |
| | | |
| | | logging.info(f"文件上传成功: {file_path}") |
| | | logging.info(f" OSS URL: {oss_url}") |
| | | logging.info(f" 过期时间: {expire_time.strftime('%Y-%m-%d %H:%M:%S')} (48小时)") |
| | | logger.info( |
| | | "File uploaded to OSS: file=%s oss_url=%s expires_at=%s", |
| | | file_path, |
| | | oss_url, |
| | | expire_time.strftime('%Y-%m-%d %H:%M:%S'), |
| | | ) |
| | | |
| | | return oss_url |
| | | |
| | |
| | | img = Image.open(p) |
| | | images.append(img) |
| | | except Exception as e: |
| | | logging.error(f"Cannot open image {p}: {e}") |
| | | logger.error("Cannot open image %s: %s", p, e) |
| | | |
| | | if not images: |
| | | return None |