@@ -183,9 +183,27 @@ def _handle_move_source(self, src_path, book_hash, book_info):
183183 def _handle_move_destination (self , dest_path ):
184184 """处理移动操作目标文件的后台任务"""
185185 try :
186- print (f"[{ str (datetime .now ())} ][Move] Wait for 3 seconds to allow the file to stabilize before adding it: { dest_path } " )
187- time .sleep (3 ) # 等待文件稳定
188- print (f"[{ str (datetime .now ())} ][Move] Processing destination addition: { dest_path } " )
186+ # Wait for the file to stabilize (rclone/WebDAV uploads may need extra time)
187+ print (f"[{ str (datetime .now ())} ][Move] Waiting for file to stabilize: { dest_path } " )
188+ stable = False
189+ last_size = - 1
190+ for attempt in range (10 ): # up to ~5 seconds of stabilization
191+ if not os .path .exists (dest_path ):
192+ print (f"[{ str (datetime .now ())} ][Move] File not found, waiting: { dest_path } " )
193+ time .sleep (0.5 )
194+ continue
195+ current_size = os .path .getsize (dest_path )
196+ if current_size == last_size and current_size > 0 :
197+ stable = True
198+ break
199+ last_size = current_size
200+ time .sleep (0.5 )
201+
202+ if not stable :
203+ print (f"[{ str (datetime .now ())} ][Move] File did not stabilize, skipping: { dest_path } " )
204+ return
205+
206+ print (f"[{ str (datetime .now ())} ][Move] File stabilized ({ last_size } bytes), processing: { dest_path } " )
189207 with self .library_lock :
190208 ok , book_info = self .library .add_book (dest_path )
191209 if ok :
@@ -200,27 +218,38 @@ def _handle_move_destination(self, dest_path):
200218 sys .stderr .flush ()
201219
202220 def on_moved (self , event ):
203- if not event .is_directory and event .src_path .endswith ('.epub' ):
204- print (f"[{ str (datetime .now ())} ][Move] EPUB file detected: from { event .src_path } to { event .dest_path } " )
205- # 处理源文件删除(如果存在)
206- if event .src_path in self .library .file2hash :
207- book_hash = self .library .file2hash [event .src_path ]
208- if book_hash in self .library .books :
209- book_info = self .library .books [book_hash ]
210- # 提交源文件处理到线程池
211- task_id = f"move_src_{ event .src_path } "
212- self ._submit_task (task_id , self ._handle_move_source , event .src_path , book_hash , book_info )
213-
214- # 处理目标文件添加
215- if event .dest_path .endswith ('.epub' ):
216- if (not os .path .basename (event .dest_path ).startswith ("." )) and (not self .has_hidden_component (event .dest_path )):
217- dest_path = event .dest_path
218- if (os .path .basename (dest_path ).startswith ("." )) or (self .has_hidden_component (dest_path )):
219- print (f"[{ str (datetime .now ())} ][Move] Hidden file will not be processed: { dest_path } " )
220- return
221- # 提交目标文件处理到线程池
222- task_id = f"move_dest_{ dest_path } "
223- self ._submit_task (task_id , self ._handle_move_destination , dest_path )
221+ if event .is_directory :
222+ return
223+
224+ dest_is_epub = event .dest_path .endswith ('.epub' )
225+ src_is_epub = event .src_path .endswith ('.epub' )
226+
227+ # Handle .partial → .epub rename (common with rclone/WebDAV uploads)
228+ if not src_is_epub and dest_is_epub :
229+ # e.g. file.epub..partial → file.epub
230+ if not os .path .basename (event .dest_path ).startswith ("." ) and not self .has_hidden_component (event .dest_path ):
231+ print (f"[{ str (datetime .now ())} ][Move] EPUB file detected (partial rename): from { event .src_path } to { event .dest_path } " )
232+ task_id = f"move_dest_{ event .dest_path } "
233+ self ._submit_task (task_id , self ._handle_move_destination , event .dest_path )
234+ return
235+
236+ if not src_is_epub :
237+ return
238+
239+ print (f"[{ str (datetime .now ())} ][Move] EPUB file detected: from { event .src_path } to { event .dest_path } " )
240+ # Handle source deletion (if existed)
241+ if event .src_path in self .library .file2hash :
242+ book_hash = self .library .file2hash [event .src_path ]
243+ if book_hash in self .library .books :
244+ book_info = self .library .books [book_hash ]
245+ task_id = f"move_src_{ event .src_path } "
246+ self ._submit_task (task_id , self ._handle_move_source , event .src_path , book_hash , book_info )
247+
248+ # Handle destination addition
249+ if dest_is_epub :
250+ if not os .path .basename (event .dest_path ).startswith ("." ) and not self .has_hidden_component (event .dest_path ):
251+ task_id = f"move_dest_{ event .dest_path } "
252+ self ._submit_task (task_id , self ._handle_move_destination , event .dest_path )
224253
225254 def shutdown (self ):
226255 """关闭线程池"""
0 commit comments