@@ -508,6 +508,30 @@ def _copy_overrides(
508508 store .update_item (dest_block , user_id )
509509
510510
511+ def create_library_v2_zip (library_key : LibraryLocatorV2 , user : User ) -> tuple :
512+ """
513+ Create a zip backup of a v2 library and return ``(temp_dir, zip_file_path)``.
514+
515+ The caller is responsible for cleaning up ``temp_dir`` when done.
516+
517+ Args:
518+ library_key: LibraryLocatorV2 identifying the library to export.
519+ user: User object passed to the backup API.
520+
521+ Returns:
522+ A tuple of ``(temp_dir as Path, zip_file_path as str)``.
523+ """
524+ root_dir = Path (mkdtemp ())
525+ sanitized_lib_key = str (library_key ).replace (":" , "-" )
526+ sanitized_lib_key = slugify (sanitized_lib_key , allow_unicode = True )
527+ timestamp = datetime .now ().strftime ("%Y-%m-%d-%H%M%S" )
528+ filename = f'{ sanitized_lib_key } -{ timestamp } .zip'
529+ file_path = os .path .join (root_dir , filename )
530+ origin_server = getattr (settings , 'CMS_BASE' , None )
531+ create_lib_zip_file (lp_key = str (library_key ), path = file_path , user = user , origin_server = origin_server )
532+ return root_dir , file_path
533+
534+
511535class LibraryBackupTask (UserTask ): # pylint: disable=abstract-method
512536 """
513537 Base class for tasks related with Library backup functionality.
@@ -553,15 +577,8 @@ def backup_library(self, user_id: int, library_key_str: str) -> None:
553577 self .status .set_state ('Exporting' )
554578 set_custom_attribute ("exporting_started" , str (library_key ))
555579
556- root_dir = Path (mkdtemp ())
557- sanitized_lib_key = str (library_key ).replace (":" , "-" )
558- sanitized_lib_key = slugify (sanitized_lib_key , allow_unicode = True )
559- timestamp = datetime .now ().strftime ("%Y-%m-%d-%H%M%S" )
560- filename = f'{ sanitized_lib_key } -{ timestamp } .zip'
561- file_path = os .path .join (root_dir , filename )
562580 user = User .objects .get (id = user_id )
563- origin_server = getattr (settings , 'CMS_BASE' , None )
564- create_lib_zip_file (lp_key = str (library_key ), path = file_path , user = user , origin_server = origin_server )
581+ _root_dir , file_path = create_library_v2_zip (library_key , user )
565582 set_custom_attribute ("exporting_completed" , str (library_key ))
566583
567584 with open (file_path , 'rb' ) as zipfile :
0 commit comments