@@ -249,26 +249,32 @@ def invoke(self,
249249 mkdir_if_not_exists (zip_dir )
250250
251251 # 原文件夹 -> zip文件
252- dir_zip_dict = {}
252+ dir_zip_dict : Dict [ str , Optional [ str ]] = {}
253253 photo_dict = downloader .all_downloaded [album ]
254254
255255 if level == 'album' :
256256 zip_path = self .get_zip_path (album , None , filename_rule , suffix , zip_dir )
257257 dir_path = self .zip_album (album , photo_dict , zip_path )
258- dir_zip_dict [dir_path ] = zip_path
258+ if dir_path is not None :
259+ # 要删除这个album文件夹
260+ dir_zip_dict [dir_path ] = zip_path
261+ # 也要删除album下的photo文件夹
262+ for d in files_of_dir (dir_path ):
263+ dir_zip_dict [d ] = None
259264
260265 elif level == 'photo' :
261266 for photo , image_list in photo_dict .items ():
262267 zip_path = self .get_zip_path (None , photo , filename_rule , suffix , zip_dir )
263268 dir_path = self .zip_photo (photo , image_list , zip_path )
264- dir_zip_dict [dir_path ] = zip_path
269+ if dir_path is not None :
270+ dir_zip_dict [dir_path ] = zip_path
265271
266272 else :
267273 ExceptionTool .raises (f'Not Implemented Zip Level: { level } ' )
268274
269275 self .after_zip (dir_zip_dict )
270276
271- def zip_photo (self , photo , image_list : list , zip_path : str ):
277+ def zip_photo (self , photo , image_list : list , zip_path : str ) -> Optional [ str ] :
272278 """
273279 压缩photo文件夹
274280 :returns: photo文件夹路径
@@ -277,46 +283,54 @@ def zip_photo(self, photo, image_list: list, zip_path: str):
277283 if len (image_list ) == 0 \
278284 else os .path .dirname (image_list [0 ][0 ])
279285
280- all_filepath = set (map (lambda t : t [0 ], image_list ))
286+ all_filepath = set (map (lambda t : self . unified_path ( t [0 ]) , image_list ))
281287
282- if len (all_filepath ) == 0 :
283- self .debug ('无下载文件,无需压缩' , 'skip' )
284- return
285-
286- from common import backup_dir_to_zip
287- backup_dir_to_zip (photo_dir , zip_path , acceptor = lambda f : f in all_filepath )
288- self .debug (f'压缩章节[{ photo .photo_id } ]成功 → { zip_path } ' , 'finish' )
288+ return self .do_zip (photo_dir ,
289+ zip_path ,
290+ all_filepath ,
291+ f'压缩章节[{ photo .photo_id } ]成功 → { zip_path } ' ,
292+ )
289293
290- return photo_dir
294+ @staticmethod
295+ def unified_path (f ):
296+ return fix_filepath (f , os .path .isdir (f ))
291297
292- def zip_album (self , album , photo_dict : dict , zip_path ):
298+ def zip_album (self , album , photo_dict : dict , zip_path ) -> Optional [ str ] :
293299 """
294300 压缩album文件夹
295301 :returns: album文件夹路径
296302 """
297- album_dir = self .option .decide_album_dir (album )
298303 all_filepath : Set [str ] = set ()
299304
300- for image_list in photo_dict .values ():
301- image_list : List [Tuple [str , JmImageDetail ]]
302- for path , _ in image_list :
303- all_filepath .add (path )
305+ def addpath (f ):
306+ all_filepath .update (set (f ))
307+
308+ album_dir = self .option .decide_album_dir (album )
309+ # addpath(self.option.decide_image_save_dir(photo) for photo in photo_dict.keys())
310+ addpath (path for ls in photo_dict .values () for path , _ in ls )
311+
312+ return self .do_zip (album_dir ,
313+ zip_path ,
314+ all_filepath ,
315+ msg = f'压缩本子[{ album .album_id } ]成功 → { zip_path } ' ,
316+ )
304317
318+ def do_zip (self , source_dir , zip_path , all_filepath , msg ):
305319 if len (all_filepath ) == 0 :
306320 self .debug ('无下载文件,无需压缩' , 'skip' )
307- return
321+ return None
308322
309323 from common import backup_dir_to_zip
310324 backup_dir_to_zip (
311- album_dir ,
325+ source_dir ,
312326 zip_path ,
313- acceptor = lambda f : f in all_filepath
314- )
327+ acceptor = lambda f : os . path . isdir ( f ) or self . unified_path ( f ) in all_filepath
328+ ). close ()
315329
316- self .debug (f'压缩本子[ { album . album_id } ]成功 → { zip_path } ' , 'finish' )
317- return album_dir
330+ self .debug (msg , 'finish' )
331+ return self . unified_path ( source_dir )
318332
319- def after_zip (self , dir_zip_dict : Dict [str , str ]):
333+ def after_zip (self , dir_zip_dict : Dict [str , Optional [ str ] ]):
320334 # 是否要删除所有原文件
321335 if self .delete_original_file is True :
322336 self .delete_all_files_and_empty_dir (
@@ -352,10 +366,10 @@ def delete_all_files_and_empty_dir(self, all_downloaded: dict, dir_list: List[st
352366 os .remove (f )
353367 self .debug (f'删除原文件: { f } ' , 'remove' )
354368
355- for d in dir_list :
369+ for d in sorted ( dir_list , reverse = True ) :
356370 # check exist
357- if file_exists (d ) and len ( os . listdir ( d )) == 0 :
358- os .removedirs (d )
371+ if file_exists (d ):
372+ os .rmdir (d )
359373 self .debug (f'删除文件夹: { d } ' , 'remove' )
360374
361375
0 commit comments