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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import os
import sys
import time
import requests
import json
import hashlib
import logging
import threading
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from urllib.parse import unquote

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    # handlers=[
    #     logging.FileHandler('r2_sync.log'),
    #     logging.StreamHandler()
    # ]
)
logger = logging.getLogger(__name__)

class Config:
    """配置管理类"""
    def __init__(self):
        self.data_dir = Path(os.getenv("DATA_DIR", "/app"))
        self.api_key = os.getenv("API_AUTH_KEY")
        self.api_endpoint = os.getenv("API_ENDPOINT")
        self.sync_interval = int(os.getenv("SYNC_INTERVAL", "300"))  # 默认5分钟
        self.use_hash_check = os.getenv("USE_HASH_CHECK", "true").lower() == "true"
        self.max_upload_workers = int(os.getenv("MAX_UPLOAD_WORKERS", "500"))
        self.upload_retry_delay = int(os.getenv("UPLOAD_RETRY_DELAY", "60"))  # 秒
        
        # 忽略的文件后缀配置(逗号分隔,如 ".tmp,.log")
        ignore_suffixes = os.getenv("IGNORE_SUFFIXES", ".db-journal,.tmp,.log,sqlite3-journal")
        self.ignore_suffixes = [s.strip().lower() for s in ignore_suffixes.split(",") if s.strip()]
        
        # 只同步的文件后缀配置(逗号分隔,如 ".jpg,.png",优先级高于忽略后缀)
        only_suffixes = os.getenv("ONLY_SUFFIXES", ".db")
        self.only_suffixes = [s.strip().lower() for s in only_suffixes.split(",") if s.strip()]
        
        # 验证配置
        if not all([self.api_key, self.api_endpoint]):
            raise ValueError("必须设置API_AUTH_KEY和API_ENDPOINT环境变量")
        
        # 检查冲突配置
        if self.ignore_suffixes and self.only_suffixes:
            logger.warning("同时设置了IGNORE_SUFFIXES和ONLY_SUFFIXES,将优先使用ONLY_SUFFIXES")

class SyncHandler(FileSystemEventHandler):
    """文件系统事件处理器"""
    def __init__(self, callback, config):
        self.callback = callback
        self.config = config
    
    def should_sync(self, file_path):
        """检查文件是否应该被同步"""
        file_path = str(file_path).lower()
        
        # 优先检查白名单模式
        if self.config.only_suffixes:
            return any(file_path.endswith(suffix) for suffix in self.config.only_suffixes)
        
        # 然后检查黑名单模式
        if self.config.ignore_suffixes:
            return not any(file_path.endswith(suffix) for suffix in self.config.ignore_suffixes)
        
        # 默认同步所有文件
        return True
    
    def on_modified(self, event):
        if not event.is_directory and self.should_sync(event.src_path):
            self.callback(event.src_path)
    
    def on_created(self, event):
        if not event.is_directory and self.should_sync(event.src_path):
            self.callback(event.src_path)

class R2Sync:
    """R2存储同步器"""
    def __init__(self, config):
        self.config = config
        self.file_states = {}  # 跟踪文件最后状态 (size, mtime, hash?)
        self.upload_queue = {}  # 上传队列 {file_path: (key, mtime, timer)}
        self.remote_files_cache = None  # 缓存远程文件列表
        self.cache_valid = False  # 缓存是否有效
        self.executor = ThreadPoolExecutor(max_workers=self.config.max_upload_workers)
        self.cache_lock = threading.Lock()
        self.upload_queue_lock = threading.Lock()
        
        # 确保数据目录存在
        self.config.data_dir.mkdir(parents=True, exist_ok=True)
    
    def should_sync_file(self, file_path):
        """检查文件是否应该被同步"""
        file_path = str(file_path).lower()
        
        # 优先检查白名单模式
        if self.config.only_suffixes:
            return any(file_path.endswith(suffix) for suffix in self.config.only_suffixes)
        
        # 然后检查黑名单模式
        if self.config.ignore_suffixes:
            return not any(file_path.endswith(suffix) for suffix in self.config.ignore_suffixes)
        
        # 默认同步所有文件
        return True
    
    def r2_api_request(self, method, path, data=None, max_retries=3):
        """发送API请求"""
        url = f"{self.config.api_endpoint}/{path}"
        headers = {
            'X-API-Key': self.config.api_key,
            'Content-Type': 'application/octet-stream'
        }
        
        for attempt in range(max_retries):
            try:
                if method == "GET":
                    resp = requests.get(url, headers=headers)
                elif method == "POST":
                    resp = requests.post(url, data=data, headers=headers)
                elif method == "PUT":
                    resp = requests.put(url, data=data, headers=headers)
                elif method == "DELETE":
                    resp = requests.delete(url, headers=headers)
                    
                resp.raise_for_status()
                
                if method == "GET":
                    return resp.content
                elif method == "DELETE":
                    return True
                return resp.json()
                
            except requests.exceptions.RequestException as e:
                if attempt == max_retries - 1:
                    logger.error(f"API请求最终失败: {e}")
                    return None
                wait_time = (attempt + 1) * 2
                logger.warning(f"API请求失败(尝试 {attempt + 1}/{max_retries}), {wait_time}秒后重试...")
                time.sleep(wait_time)
    
    def get_file_hash(self, file_path):
        """计算文件哈希值"""
        hash_obj = hashlib.blake2b()
        with open(file_path, 'rb') as f:
            while chunk := f.read(8192):
                hash_obj.update(chunk)
        return hash_obj.hexdigest()
    
    
    def get_remote_files(self, force_refresh=False):
        """获取远程文件列表,使用缓存"""
        with self.cache_lock:
            if force_refresh or not self.cache_valid or self.remote_files_cache is None:
                self.remote_files_cache = self._fetch_remote_files()
                self.cache_valid = True
            return self.remote_files_cache.copy()
    
    def _fetch_remote_files(self):
        """实际获取远程文件列表"""
        resp = self.r2_api_request("GET", "list")
        if not resp:
            return None
            
        try:
            raw_resp = resp.decode('utf-8').strip()
            if raw_resp == "[]":
                return []
                
            data = json.loads(raw_resp)
            
            # 统一处理为列表格式
            if isinstance(data, dict):
                if 'objects' in data:
                    data = data['objects']
                else:
                    data = [data]
            elif not isinstance(data, list):
                data = [data]
                
            # 处理文件名编码
            for item in data:
                if isinstance(item, dict) and 'key' in item:
                    try:
                        item['key'] = unquote(item['key'])
                    except:
                        pass
                        
            return data
        except Exception as e:
            logger.error(f"解析远程文件列表出错: {e}")
            return []
    
    def invalidate_cache(self):
        """使缓存失效"""
        with self.cache_lock:
            self.cache_valid = False
    
    def download_file(self, key, dest_path):
        """下载文件"""
        if not self.should_sync_file(dest_path):
            logger.debug(f"忽略下载文件(不匹配同步规则): {key}")
            return False
            
        path = f"download/{key}"
        logger.debug(f"下载路径: {dest_path} (原始key: {key})")
        
        resp = self.r2_api_request("GET", path)
        if not resp:
            return False
            
        # 确保目标目录存在
        dest_path.parent.mkdir(parents=True, exist_ok=True)
        
        # 处理不同响应类型
        if isinstance(resp, bytes):
            content = resp
        else:
            try:
                content = resp.decode('utf-8').encode('utf-8')
            except UnicodeDecodeError:
                content = resp
        
        with open(dest_path, 'wb') as f:
            f.write(content)
        
        logger.info(f"文件已保存到: {dest_path} (大小: {len(content)}字节)")
        return True
    
    def _upload_file_task(self, file_path, key):
        """线程池中执行的上传任务"""
        if not self.should_sync_file(file_path):
            logger.debug(f"忽略上传文件(不匹配同步规则): {key}")
            return False
            
        path = f"upload/{key}"
        try:
            with open(file_path, 'rb') as f:
                result = self.r2_api_request("POST", path, f.read()) is not None
                if result:
                    self.invalidate_cache()  # 上传成功,使缓存失效
                return result
        except Exception as e:
            logger.error(f"上传文件出错: {e}")
            return False
    
    def upload_file(self, file_path, key):
        """上传文件"""
        # 提交到线程池执行
        future = self.executor.submit(self._upload_file_task, file_path, key)
        return future.result()  # 阻塞等待结果
    
    def delete_file(self, key):
        """删除文件并更新缓存"""
        if not self.should_sync_file(key):
            logger.debug(f"忽略删除文件(不匹配同步规则): {key}")
            return False
            
        logger.info(f"删除远程文件: {key}")
        path = f"delete/{key}"
        if not self.r2_api_request("DELETE", path):
            return False
            
        # 使缓存失效
        self.invalidate_cache()
        
        # 验证文件是否真的被删除
        remote_files = self.get_remote_files(force_refresh=True)
        if remote_files is None:
            return False
            
        return not any(file_info['key'] == key for file_info in remote_files)
    
    def check_api_health(self):
        """检查API健康状态"""
        logger.info("检查API健康状态...")
        
        # 测试列表API
        files = self.get_remote_files()
        if files is None:
            logger.error("列表API测试失败")
            return False
            
        logger.info("API健康状态检查通过")
        return True
    
    def _download_and_update_state(self, file_info):
        """线程任务:下载文件并更新状态"""
        key = file_info['key']
        dest_path = self.config.data_dir / key
        
        if not self.should_sync_file(dest_path):
            logger.debug(f"忽略下载文件(不匹配同步规则): {key}")
            return
            
        logger.info(f"下载: {key} -> {dest_path}")
        if self.download_file(key, dest_path):
            # 记录文件初始状态
            stat = dest_path.stat()
            file_state = (stat.st_size, stat.st_mtime)
            if self.config.use_hash_check:
                file_state += (self.get_file_hash(dest_path),)
            with self.cache_lock:  # 使用锁保护file_states更新
                self.file_states[key] = file_state

    def init_sync(self):
        """初始化同步"""
        logger.info("初始化数据目录...")
        
        if not self.check_api_health():
            logger.error("API检查失败,请检查配置和网络连接")
            sys.exit(1)
        
        # 获取远程文件列表
        remote_files = self.get_remote_files()
        if remote_files is None:
            logger.error("获取远程文件列表失败")
            sys.exit(1)
            
        logger.info(f"找到 {len(remote_files)} 个远程文件")
        if not remote_files:
            logger.info("远程存储桶为空,无需同步")
            return
        
        logger.info("开始并行同步远程文件...")
        # 使用线程池并行下载
        futures = []
        for file_info in remote_files:
            futures.append(self.executor.submit(self._download_and_update_state, file_info))
            
        # 等待所有任务完成
        for future in futures:
            try:
                future.result()  # 捕获任何异常
            except Exception as e:
                logger.error(f"文件下载任务出错: {e}")
    
    def handle_file_change(self, file_path):
        """处理文件变化事件,加入上传队列并设置定时检查"""
        try:
            if not self.should_sync_file(file_path):
                logger.debug(f"忽略文件变化(不匹配同步规则): {file_path}")
                return
                
            rel_path = Path(file_path).relative_to(self.config.data_dir)
            key = str(rel_path).replace('\\', '/')
            
            # 获取文件当前状态
            stat = os.stat(file_path)
            current_mtime = stat.st_mtime
            
            with self.upload_queue_lock:
                # 如果文件已在队列中,更新修改时间并重置计时器
                if file_path in self.upload_queue:
                    _, _, timer = self.upload_queue[file_path]
                    timer.cancel()  # 取消之前的计时器
                
                # 创建新计时器,15秒后检查
                timer = threading.Timer(
                    self.config.upload_retry_delay,
                    self._process_upload_queue,
                    args=(file_path,)
                )
                timer.start()
                
                # 加入/更新队列
                self.upload_queue[file_path] = (key, current_mtime, timer)
                logger.info(f"检测到文件变化,加入上传队列: {key} (将在{self.config.upload_retry_delay}秒后检查)")
                
        except Exception as e:
            logger.error(f"处理文件变化出错: {e}")
    def is_file_modified(self, file_path, last_known_state):
            """检查文件是否修改"""
            try:
                stat = os.stat(file_path)
                current_size = stat.st_size
                
                # 只比较文件大小
                if last_known_state[0] == current_size:  # 比较size
                    logger.info(f"文件大小没有变化: {file_path} (大小: {current_size})")
                    # 新增:当hash不存在时,计算并更新hash
                    if not self.config.use_hash_check:
                        logger.info(f"不使用hash检查: {file_path} 视为未修改")
                        return False  # 不使用hash检查,直接返回False
                    logger.info(f"文件大小相同,开始检查hash: {file_path}")
                    current_hash = self.get_file_hash(file_path)
                    if len(last_known_state) <= 2:  # hash不存在
                        # 获取文件key用于更新状态
                        rel_path = str(Path(file_path).relative_to(self.config.data_dir)).replace('\\', '/')
                        # 更新状态 (size, mtime, hash)
                        self.file_states[rel_path] = (current_size, stat.st_mtime, current_hash)
                        return True  # 返回True触发更新
                    return current_hash != last_known_state[2]  # 比较hash
                return True  # 大小不同,认为已修改
            except Exception as e:
                logger.error(f"检查文件修改状态出错: {e}")
                return False
    def _process_upload_queue(self, file_path):
        """处理上传队列中的文件"""
        with self.upload_queue_lock:
            if file_path not in self.upload_queue:
                return
                
            key, original_mtime, _ = self.upload_queue[file_path]
            del self.upload_queue[file_path]  # 从队列移除
            
        try:
            # 检查文件是否还存在
            if not os.path.exists(file_path):
                logger.info(f"文件已被删除,取消上传: {key}")
                return
                
            # 获取当前文件状态
            current_stat = os.stat(file_path)
            current_mtime = current_stat.st_mtime
            
            # 比较修改时间,确认文件是否又有新变化
            if current_mtime != original_mtime:
                logger.info(f"文件 {key} 在等待期间又有新变化,重新加入队列")
                self.handle_file_change(file_path)  # 重新加入队列
                return
            
            if not self.is_file_modified(file_path, self.file_states.get(key, (0, 0))):
                logger.info(f"文件 {key} 没有新变化,跳过上传")
                return
            # 确认文件无新变化,开始上传
            logger.info(f"开始上传文件: {key}")
            if self.upload_file(file_path, key):
                # 更新文件状态
                file_state = (current_stat.st_size, current_mtime)
                if self.config.use_hash_check:
                    file_state += (self.get_file_hash(file_path),)
                self.file_states[key] = file_state
            else:
                logger.error(f"上传失败: {key}")
                
        except Exception as e:
            logger.error(f"处理上传队列出错: {e}")

    def sync_deleted_files(self):
        """同步删除操作"""
        try:
            remote_files = self.get_remote_files()
            if remote_files is None:
                return
                
            local_files = {
                str(f.relative_to(self.config.data_dir)).replace('\\', '/')
                for f in self.config.data_dir.rglob('*') 
                if f.is_file() and self.should_sync_file(f)
            }
            
            for file_info in remote_files:
                key = file_info['key']
                if key not in local_files and key not in self.upload_queue:
                    logger.info(f"删除远程文件: {key}")
                    if self.delete_file(key):
                        if key in self.file_states:
                            del self.file_states[key]
                    else:
                        logger.error(f"删除失败: {key}")
        except Exception as e:
            logger.error(f"同步删除操作出错: {e}")

    def watch_and_sync(self):
        """监控并同步文件"""
        logger.info("启动持续同步服务...")
        
        # 初始化文件状态(根据同步规则)
        for file in self.config.data_dir.rglob('*'):
            if file.is_file() and self.should_sync_file(file):
                rel_path = str(file.relative_to(self.config.data_dir)).replace('\\', '/')
                stat = file.stat()
                file_state = (stat.st_size, stat.st_mtime)
                if self.config.use_hash_check:
                    file_state += (self.get_file_hash(file),)
                self.file_states[rel_path] = file_state
        
        # 设置文件监控(根据同步规则)
        event_handler = SyncHandler(self.handle_file_change, self.config)
        observer = Observer()
        observer.schedule(event_handler, path=str(self.config.data_dir), recursive=True)
        observer.start()
        
        try:
            while True:
                # 定期检查远程文件删除
                self.sync_deleted_files()
                time.sleep(self.config.sync_interval)
        except KeyboardInterrupt:
            logger.info("收到停止信号,关闭监控...")
            # 取消所有待处理的上传计时器
            with self.upload_queue_lock:
                for file_path, (_, _, timer) in self.upload_queue.items():
                    timer.cancel()
                self.upload_queue.clear()
            observer.stop()
        observer.join()
        self.executor.shutdown()

def main():
    if len(sys.argv) < 2:
        logger.info("Usage: python r2_sync.py [init|sync]")
        sys.exit(1)
    
    try:
        config = Config()
    except ValueError as e:
        logger.error(str(e))
        sys.exit(1)
    
    r2_sync = R2Sync(config)
    command = sys.argv[1]
    
    if command == "init":
        r2_sync.init_sync()
    elif command == "sync":
        logger.info(f"启动同步服务,间隔: {config.sync_interval}秒")
        r2_sync.watch_and_sync()
    else:
        logger.error(f"未知命令: {command}")
        sys.exit(1)

if __name__ == "__main__":
    main()