File size: 26,086 Bytes
551658a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
import os
import asyncio
import boto3
from botocore.exceptions import ClientError
from pathlib import Path
from typing import List, Dict, Optional, Tuple
from PIL import Image
import io
import hashlib
import threading
from datetime import datetime, timedelta
from dotenv import load_dotenv

# 配置缓存
_config_cache = {}
_cache_lock = threading.Lock()
_CACHE_TTL = timedelta(minutes=30)  # 缓存 30 分钟

# 全局S3客户端缓存,按公司编码管理
# 格式: {company_code: {"client": s3_client, "created_at": datetime}}
GLOBAL_S3_CLIENTS = {}
# 客户端过期时间(秒)
CLIENT_EXPIRY_SECONDS = 24 * 3600  # 24小时
# 客户端缓存锁
_client_lock = threading.Lock()


# ===================== 先定义类,再使用!=====================
class R2Config:
    """R2 配置对象"""
    def __init__(self, config_data: Dict):
        self.account_id = config_data.get('r2_account_id', '')
        self.access_key_id = config_data.get('r2_access_key_id', '')
        self.secret_access_key = config_data.get('r2_secret_access_key', '')
        self.bucket_name = config_data.get('r2_bucket_name', 'ymt-images')
        self.public_url = config_data.get('r2_public_url', '')
        self.enabled = config_data.get('r2_enabled', 1) == 1
        self.cached_at = datetime.now()

    def is_expired(self) -> bool:
        """检查缓存是否过期"""
        return datetime.now() - self.cached_at > _CACHE_TTL


def get_r2_config_from_env() -> Optional[R2Config]:
    """从 .env 文件获取 R2 配置"""
    # 尝试加载 .env 文件
    try:
        load_dotenv()
    except:
        pass

    r2_account_id = os.getenv('R2_ACCOUNT_ID', '')
    r2_access_key_id = os.getenv('R2_ACCESS_KEY_ID', '')
    r2_secret_access_key = os.getenv('R2_SECRET_ACCESS_KEY', '')
    r2_bucket_name = os.getenv('R2_BUCKET_NAME', 'yomaton')
    r2_public_url = os.getenv('R2_PUBLIC_URL', '')
    r2_enabled = os.getenv('R2_ENABLED', 'true').lower() == 'true'

    if r2_enabled and r2_account_id and r2_access_key_id and r2_secret_access_key:
        return R2Config({
            'r2_account_id': r2_account_id,
            'r2_access_key_id': r2_access_key_id,
            'r2_secret_access_key': r2_secret_access_key,
            'r2_bucket_name': r2_bucket_name,
            'r2_public_url': r2_public_url,
            'r2_enabled': 1 if r2_enabled else 0
        })

    return None


class R2Uploader:
    """
    Cloudflare R2 上传工具类
    """

    def __init__(self, config: R2Config, company_code: str = "default"):
        self.config = config
        self.company_code = company_code
        self._cleanup_expired_clients()

    def _cleanup_expired_clients(self):
        """清理过期的客户端"""
        with _client_lock:
            expired_companies = []
            for company_code, client_info in GLOBAL_S3_CLIENTS.items():
                if (datetime.utcnow() - client_info["created_at"]).total_seconds() > CLIENT_EXPIRY_SECONDS:
                    expired_companies.append(company_code)

            for company_code in expired_companies:
                if company_code != '0000' and company_code != 'default':
                    del GLOBAL_S3_CLIENTS[company_code]
                    print(f"清理过期的S3客户端: {company_code}")

    def _get_config_from_db(self, company_code: str) -> Optional[R2Config]:
        """
        从数据库获取 R2 配置

        注意:这里需要在有数据库会话的上下文调用
        实际使用时通过 get_r2_uploader_for_company() 传入配置
        """
        return None

    def get_client(self):
        """
        获取 R2 S3 客户端,按公司编码全局复用
        优先级:当前公司编码 -> '0000' -> 'default'
        """
        if not self.config.enabled or not self.config.account_id or not self.config.access_key_id or not self.config.secret_access_key:
            return None

        # 清理过期客户端
        self._cleanup_expired_clients()

        # 检查顺序:当前公司 -> 0000 -> default
        client_codes = [self.company_code, '0000', 'default']

        with _client_lock:
            for code in client_codes:
                if code in GLOBAL_S3_CLIENTS:
                    client_info = GLOBAL_S3_CLIENTS[code]
                    time_diff = (datetime.utcnow() - client_info["created_at"]).total_seconds()
                    if time_diff < CLIENT_EXPIRY_SECONDS:
                        print(f"复用S3客户端: {code} (优先级: {client_codes.index(code) + 1})")
                        # 如果不是当前公司的客户端,为当前公司创建一个引用
                        if code != self.company_code:
                            GLOBAL_S3_CLIENTS[self.company_code] = {
                                "client": client_info["client"],
                                "created_at": datetime.utcnow()
                            }
                            print(f"为当前公司 {self.company_code} 创建客户端引用")
                        return client_info["client"]
                    else:
                        # 客户端已过期,删除
                        del GLOBAL_S3_CLIENTS[code]
                        print(f"S3客户端已过期,删除: {code}")

        # 创建新的客户端
        try:
            client = boto3.client(
                's3',
                endpoint_url=f'https://{self.config.account_id}.r2.cloudflarestorage.com',
                aws_access_key_id=self.config.access_key_id,
                aws_secret_access_key=self.config.secret_access_key
            )

            # 缓存客户端
            with _client_lock:
                GLOBAL_S3_CLIENTS[self.company_code] = {
                    "client": client,
                    "created_at": datetime.utcnow()
                }
                # 同时缓存为 'default'(如果是从环境变量加载的配置)
                if self.company_code == 'default':
                    pass  # 已经缓存为 default
                elif self.company_code == '0000':
                    # 0000 公司的配置也缓存为 default
                    GLOBAL_S3_CLIENTS['default'] = {
                        "client": client,
                        "created_at": datetime.utcnow()
                    }

            print(f"创建新的S3客户端: {self.company_code}")
            return client
        except Exception as e:
            print(f"R2 客户端初始化失败: {e}")
            return None

    def is_available(self) -> bool:
        """检查 R2 是否可用"""
        if not self.config or not self.config.enabled:
            return False
        return self.get_client() is not None

    def generate_r2_key(self, company_code: str, product_number: str, file_type: str, filename: str, sub_type: Optional[str] = None) -> str:
        """
        生成 R2 对象键(路径)

        格式: {company_code}/{product_number}/{file_type}/{sub_type}/{filename}
        """
        key_parts = [company_code, product_number, file_type]
        if sub_type:
            key_parts.append(sub_type)
        key_parts.append(filename)
        return '/'.join(key_parts)

    def generate_public_url(self, r2_key: str) -> str:
        """
        生成 R2 公共访问 URL
        """
        if not self.config or not self.config.public_url:
            return ''
        return f"{self.config.public_url.rstrip('/')}/{r2_key.lstrip('/')}"

    def generate_thumbnail(self, file_data: bytes, max_width: int = 400, max_height: int = 400) -> Tuple[bytes, str]:
        """
        从字节数据生成缩略图

        返回: (缩略图二进制数据, 文件扩展名)
        """
        try:
            img = Image.open(io.BytesIO(file_data))

            # 计算缩放尺寸
            img.thumbnail((max_width, max_height), Image.Resampling.LANCZOS)

            # 保存为字节流
            img_byte_arr = io.BytesIO()

            # 确定保存格式
            ext = '.jpg'  # 默认使用JPEG
            if img.mode in ['RGBA', 'P']:
                img = img.convert('RGB')
                ext = '.jpg'
            elif img.mode == 'LA':
                img = img.convert('RGBA')
                ext = '.png'

            save_format = 'JPEG' if ext in ['.jpg', '.jpeg'] else ext[1:].upper()
            img.save(img_byte_arr, format=save_format, quality=85)
            img_byte_arr.seek(0)

            return img_byte_arr.read(), ext
        except Exception as e:
            print(f"缩略图生成失败: {e}")
            return file_data, '.jpg'

    def generate_md5_key(self, file_data: bytes, filename: str) -> str:
        """
        使用文件MD5生成R2键

        参数:
            file_data: 文件字节数据
            filename: 原始文件名

        返回:
            R2键(MD5命名)
        """
        # 计算文件MD5
        hash_md5 = hashlib.md5()
        hash_md5.update(file_data)
        file_hash = hash_md5.hexdigest()

        # 获取文件扩展名
        ext = Path(filename).suffix.lower()

        # 生成MD5命名的R2键
        return f"{file_hash}{ext}"

    async def upload_file(
        self,
        file_buffer: bytes,
        file_name: str,
        content_type: Optional[str] = None,
        generate_thumbnail: bool = True,
        max_retries: int = 3
    ) -> str:
        """
        上传单个文件到 R2

        参数:
            file_buffer: 文件字节数据
            file_name: 文件名(完整R2键,包含路径)
            content_type: MIME类型
            generate_thumbnail: 是否生成缩略图
            max_retries: 最大重试次数

        返回:
            R2 URL
        """
        if not self.is_available():
            return ''

        s3_client = self.get_client()

        # 直接使用传入的文件名作为R2键
        r2_key = file_name

        # 上传原图(带重试机制)
        for attempt in range(max_retries):
            try:
                s3_client.put_object(
                    Bucket=self.config.bucket_name,
                    Key=r2_key,
                    Body=file_buffer,
                    ContentType=content_type or 'application/octet-stream'
                )
                print(f"R2 上传成功: {r2_key}")
                break
            except ClientError as e:
                error_code = e.response.get('Error', {}).get('Code')
                # 对于可重试的错误,进行重试
                if error_code in ['RequestTimeout', 'ConnectionError', 'ServiceUnavailable'] and attempt < max_retries - 1:
                    print(f"R2 上传失败 (尝试 {attempt + 1}/{max_retries}): {e}")
                    await asyncio.sleep(1 * (attempt + 1))  # 指数退避
                else:
                    print(f"R2 上传失败: {e}")
                    raise

        # 生成并上传缩略图(如果是图片,但这里不处理缩略图,因为upload_thumbnail已被单独调用
        # 缩略图将在media.py中单独处理

        return self.generate_public_url(r2_key)

    async def batch_upload_files(
        self,
        files: List[Dict[str, any]],
        max_concurrency: int = 10  # 增加并发数
    ) -> Dict[str, str]:
        """
        批量上传多个文件到 R2

        参数:
            files: 文件列表,每个文件包含 {'file_buffer': bytes, 'file_name': str, 'content_type': Optional[str]}
            max_concurrency: 最大并发数

        返回:
            字典,键为文件名,值为R2 URL
        """
        if not self.is_available():
            return {}

        if not files:
            return {}

        # 获取S3客户端,避免每次上传都创建新连接
        s3_client = self.get_client()
        if not s3_client:
            return {}

        # 限制并发数
        semaphore = asyncio.Semaphore(max_concurrency)
        results = {}

        async def upload_file_async(file_info):
            async with semaphore:
                file_buffer = file_info['file_buffer']
                file_name = file_info['file_name']
                content_type = file_info.get('content_type') or 'application/octet-stream'

                try:
                    # 对于大文件使用分块上传
                    file_size = len(file_buffer)
                    if file_size > 5 * 1024 * 1024:  # 5MB以上使用分块上传
                        url = await self._upload_large_file(
                            s3_client, file_buffer, file_name, content_type
                        )
                    else:
                        # 小文件直接上传
                        s3_client.put_object(
                            Bucket=self.config.bucket_name,
                            Key=file_name,
                            Body=file_buffer,
                            ContentType=content_type
                        )
                        url = self.generate_public_url(file_name)

                    results[file_name] = url
                    print(f"✅ 批量上传成功: {file_name} ({file_size/1024/1024:.2f}MB)")
                except ClientError as e:
                    error_code = e.response.get('Error', {}).get('Code')
                    print(f"❌ 批量上传失败: {file_name}, 错误: {error_code} - {e}")
                    results[file_name] = ''
                except Exception as e:
                    print(f"❌ 批量上传失败: {file_name}, 错误: {e}")
                    results[file_name] = ''

        # 并行上传文件
        tasks = [upload_file_async(file) for file in files]
        await asyncio.gather(*tasks)

        return results

    async def _upload_large_file(
        self,
        s3_client,
        file_buffer: bytes,
        file_name: str,
        content_type: str
    ) -> str:
        """
        分块上传大文件

        参数:
            s3_client: S3客户端
            file_buffer: 文件字节数据
            file_name: 文件名
            content_type: MIME类型

        返回:
            R2 URL
        """
        # 初始化分块上传
        response = s3_client.create_multipart_upload(
            Bucket=self.config.bucket_name,
            Key=file_name,
            ContentType=content_type
        )
        upload_id = response['UploadId']

        # 分块大小:5MB
        part_size = 5 * 1024 * 1024
        file_size = len(file_buffer)
        parts = []

        try:
            # 上传分块
            for i in range(0, file_size, part_size):
                part_number = (i // part_size) + 1
                part_data = file_buffer[i:i+part_size]

                response = s3_client.upload_part(
                    Bucket=self.config.bucket_name,
                    Key=file_name,
                    UploadId=upload_id,
                    PartNumber=part_number,
                    Body=part_data
                )

                parts.append({
                    'PartNumber': part_number,
                    'ETag': response['ETag']
                })

            # 完成分块上传
            s3_client.complete_multipart_upload(
                Bucket=self.config.bucket_name,
                Key=file_name,
                UploadId=upload_id,
                MultipartUpload={'Parts': parts}
            )

            return self.generate_public_url(file_name)
        except Exception as e:
            # 取消分块上传
            s3_client.abort_multipart_upload(
                Bucket=self.config.bucket_name,
                Key=file_name,
                UploadId=upload_id
            )
            raise

    async def batch_upload_thumbnails(
        self,
        thumbnails: List[Dict[str, any]],
        max_concurrency: int = 10  # 增加并发数
    ) -> Dict[str, str]:
        """
        批量上传多个缩略图到 R2

        参数:
            thumbnails: 缩略图列表,每个缩略图包含 {'file_buffer': bytes, 'file_name': str, 'width': Optional[int], 'height': Optional[int]}
            max_concurrency: 最大并发数

        返回:
            字典,键为文件名,值为缩略图R2 URL
        """
        if not self.is_available():
            return {}

        if not thumbnails:
            return {}

        # 获取S3客户端,避免每次上传都创建新连接
        s3_client = self.get_client()
        if not s3_client:
            return {}

        # 限制并发数
        semaphore = asyncio.Semaphore(max_concurrency)
        results = {}

        async def upload_thumbnail_async(thumbnail_info):
            async with semaphore:
                file_buffer = thumbnail_info['file_buffer']
                file_name = thumbnail_info['file_name']
                width = thumbnail_info.get('width', 200)
                height = thumbnail_info.get('height', 200)

                try:
                    # 直接使用S3客户端上传,避免重复创建客户端
                    s3_client.put_object(
                        Bucket=self.config.bucket_name,
                        Key=file_name,
                        Body=file_buffer,
                        ContentType='image/jpeg'
                    )
                    url = self.generate_public_url(file_name)
                    results[file_name] = url
                    print(f"✅ 批量缩略图上传成功: {file_name} ({len(file_buffer)/1024:.2f}KB)")
                except ClientError as e:
                    error_code = e.response.get('Error', {}).get('Code')
                    print(f"❌ 批量缩略图上传失败: {file_name}, 错误: {error_code} - {e}")
                    results[file_name] = ''
                except Exception as e:
                    print(f"❌ 批量缩略图上传失败: {file_name}, 错误: {e}")
                    results[file_name] = ''

        # 并行上传缩略图
        tasks = [upload_thumbnail_async(thumbnail) for thumbnail in thumbnails]
        await asyncio.gather(*tasks)

        return results

    async def upload_thumbnail(
        self,
        file_buffer: bytes,
        file_name: str,
        width: int = 200,
        height: int = 200
    ) -> str:
        """
        上传缩略图到 R2

        参数:
            file_buffer: 文件字节数据
            file_name: 文件名(完整R2键,包含路径)
            width: 缩略图宽度
            height: 缩略图高度

        返回:
            缩略图R2 URL
        """
        if not self.is_available():
            return ''

        s3_client = self.get_client()

        # 生成缩略图
        thumbnail_data, ext = self.generate_thumbnail(file_buffer, width, height)

        # 直接使用传入的文件名作为缩略图R2键
        thumbnail_r2_key = file_name

        # 上传缩略图
        try:
            s3_client.put_object(
                Bucket=self.config.bucket_name,
                Key=thumbnail_r2_key,
                Body=thumbnail_data,
                ContentType='image/jpeg'
            )
            print(f"R2 缩略图上传成功: {thumbnail_r2_key}")
        except ClientError as e:
            print(f"R2 缩略图上传失败: {e}")
            raise

        return self.generate_public_url(thumbnail_r2_key)

    async def delete_file(self, r2_key: str) -> bool:
        """
        从 R2 删除文件

        参数:
            r2_key: R2 对象键

        返回:
            是否删除成功
        """
        print(f"  [R2Delete] 准备删除: {r2_key}")

        if not self.is_available():
            print(f"  [R2Delete] ❌ R2不可用")
            return False

        if not r2_key:
            print(f"  [R2Delete] ❌ R2 Key为空")
            return False

        s3_client = self.get_client()
        if not s3_client:
            print(f"  [R2Delete] ❌ 无法获取R2客户端")
            return False

        try:
            print(f"  [R2Delete] 调用delete_object...")
            response = s3_client.delete_object(
                Bucket=self.config.bucket_name,
                Key=r2_key
            )
            print(f"  [R2Delete] ✅ 删除成功: {r2_key}")
            print(f"  [R2Delete] 响应: {response}")
            return True
        except ClientError as e:
            print(f"  [R2Delete] ❌ 删除失败: {e}")
            print(f"  [R2Delete] 错误代码: {e.response.get('Error', {}).get('Code')}")
            print(f"  [R2Delete] 错误消息: {e.response.get('Error', {}).get('Message')}")
            return False
        except Exception as e:
            print(f"  [R2Delete] ❌ 未知错误: {e}")
            import traceback
            traceback.print_exc()
            return False

    def _get_content_type(self, filename: str) -> str:
        """根据文件名获取 Content-Type"""
        ext = Path(filename).suffix.lower()
        content_types = {
            '.jpg': 'image/jpeg',
            '.jpeg': 'image/jpeg',
            '.png': 'image/png',
            '.gif': 'image/gif',
            '.webp': 'image/webp',
            '.mp4': 'video/mp4',
            '.webm': 'video/webm',
            '.pdf': 'application/pdf'
        }
        return content_types.get(ext, 'application/octet-stream')


# 全局实例
_config_cache = {}
_cache_lock = threading.Lock()


# 向后兼容的方法 - 旧API
class CompatibleR2Uploader:
    """向后兼容的R2上传器包装类"""
    def __init__(self):
        self.uploader = None

    def is_available(self, config: R2Config) -> bool:
        """检查是否可用 - 向后兼容"""
        if not config or not config.enabled:
            return False
        return config.account_id and config.access_key_id and config.secret_access_key

    async def upload_file(self, file_path: str, company_code: str,
                        product_number: str, file_type: str,
                        filename: str, config: R2Config,
                        sub_type: Optional[str] = None,
                        generate_thumbnail: bool = True) -> Dict:
        """旧的上传文件方法 - 向后兼容"""
        import hashlib
        from pathlib import Path

        self.uploader = R2Uploader(config)
        if not self.uploader.is_available():
            return {
                'r2_url': '',
                'thumbnail_url': '',
                'r2_key': '',
                'thumbnail_key': ''
            }

        # 读取文件
        with open(file_path, 'rb') as f:
            file_buffer = f.read()

        # 计算MD5
        hash_md5 = hashlib.md5()
        hash_md5.update(file_buffer)
        file_hash = hash_md5.hexdigest()
        file_extension = Path(filename).suffix.lower()

        # 构建R2键
        r2_key = f"{company_code}/{file_hash}{file_extension}"
        thumbnail_key = f"{company_code}/thumb_{file_hash}{file_extension}"

        # 上传原图
        r2_url = await self.uploader.upload_file(
            file_buffer=file_buffer,
            file_name=r2_key,
            content_type=self.uploader._get_content_type(filename),
            generate_thumbnail=False
        )

        thumbnail_url = ''
        if generate_thumbnail:
            thumbnail_url = await self.uploader.upload_thumbnail(
                file_buffer=file_buffer,
                file_name=thumbnail_key,
                width=200,
                height=200
            )

        return {
            'r2_url': r2_url,
            'thumbnail_url': thumbnail_url,
            'r2_key': r2_key,
            'thumbnail_key': thumbnail_key
        }

    async def delete_file(self, r2_key: str, config: R2Config) -> bool:
        """删除文件 - 向后兼容"""
        if not r2_key:
            return False

        self.uploader = R2Uploader(config)
        if not self.uploader.is_available():
            return False

        return await self.uploader.delete_file(r2_key)

    async def batch_upload_files(
        self,
        files: List[Dict[str, any]],
        config: R2Config,
        max_concurrency: int = 5
    ) -> Dict[str, str]:
        """批量上传多个文件 - 向后兼容"""
        self.uploader = R2Uploader(config)
        if not self.uploader.is_available():
            return {}

        return await self.uploader.batch_upload_files(files, max_concurrency)

    async def batch_upload_thumbnails(
        self,
        thumbnails: List[Dict[str, any]],
        config: R2Config,
        max_concurrency: int = 5
    ) -> Dict[str, str]:
        """批量上传多个缩略图 - 向后兼容"""
        self.uploader = R2Uploader(config)
        if not self.uploader.is_available():
            return {}

        return await self.uploader.batch_upload_thumbnails(thumbnails, max_concurrency)


# 全局向后兼容的实例
_compatible_uploader = None

def get_r2_uploader(config: R2Config = None, company_code: str = "default") -> any:
    """获取 R2 上传器实例 - 支持新旧API"""
    if config is not None:
        # 新API - 返回R2Uploader实例,按公司编码管理
        return R2Uploader(config, company_code)
    else:
        # 旧API - 返回向后兼容的实例
        global _compatible_uploader
        if _compatible_uploader is None:
            _compatible_uploader = CompatibleR2Uploader()
        return _compatible_uploader


def get_cached_config(company_code: str) -> Optional[R2Config]:
    """从缓存获取配置"""
    with _cache_lock:
        config = _config_cache.get(company_code)
        if config and not config.is_expired():
            return config
        return None


def set_cached_config(company_code: str, config: R2Config):
    """设置缓存配置"""
    with _cache_lock:
        _config_cache[company_code] = config


def clear_cached_config(company_code: str):
    """清除缓存配置"""
    with _cache_lock:
        if company_code in _config_cache:
            del _config_cache[company_code]
            print(f"已清除公司 {company_code} 的 R2 配置缓存")