|  |  |  | @ -21,14 +21,6 @@ object Backup { | 
			
		
	
		
			
				
					|  |  |  |  |         FileUtils.getDirFile(App.INSTANCE.filesDir, "backup").absolutePath | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     val legadoPath by lazy { | 
			
		
	
		
			
				
					|  |  |  |  |         FileUtils.getSdCardPath() + File.separator + "YueDu3.0" | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     val exportPath by lazy { | 
			
		
	
		
			
				
					|  |  |  |  |         legadoPath + File.separator + "Export" | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     val backupFileNames by lazy { | 
			
		
	
		
			
				
					|  |  |  |  |         arrayOf( | 
			
		
	
		
			
				
					|  |  |  |  |             "bookshelf.json", "bookGroup.json", "bookSource.json", "rssSource.json", | 
			
		
	
	
		
			
				
					|  |  |  | @ -42,16 +34,13 @@ object Backup { | 
			
		
	
		
			
				
					|  |  |  |  |             return | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         Coroutine.async { | 
			
		
	
		
			
				
					|  |  |  |  |             val backupPath = context.getPrefString(PreferKey.backupPath) | 
			
		
	
		
			
				
					|  |  |  |  |             if (backupPath.isNullOrEmpty()) { | 
			
		
	
		
			
				
					|  |  |  |  |                 backup(context) | 
			
		
	
		
			
				
					|  |  |  |  |             } else { | 
			
		
	
		
			
				
					|  |  |  |  |                 backup(context, backupPath) | 
			
		
	
		
			
				
					|  |  |  |  |             context.getPrefString(PreferKey.backupPath)?.let { | 
			
		
	
		
			
				
					|  |  |  |  |                 backup(context, it, true) | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     suspend fun backup(context: Context, path: String = legadoPath) { | 
			
		
	
		
			
				
					|  |  |  |  |     suspend fun backup(context: Context, path: String, isAuto: Boolean = false) { | 
			
		
	
		
			
				
					|  |  |  |  |         context.putPrefLong(PreferKey.lastBackup, System.currentTimeMillis()) | 
			
		
	
		
			
				
					|  |  |  |  |         withContext(IO) { | 
			
		
	
		
			
				
					|  |  |  |  |             synchronized(this@Backup) { | 
			
		
	
	
		
			
				
					|  |  |  | @ -81,9 +70,9 @@ object Backup { | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  |                 WebDavHelp.backUpWebDav(backupPath) | 
			
		
	
		
			
				
					|  |  |  |  |                 if (path.isContentPath()) { | 
			
		
	
		
			
				
					|  |  |  |  |                     copyBackup(context, Uri.parse(path)) | 
			
		
	
		
			
				
					|  |  |  |  |                     copyBackup(context, Uri.parse(path), isAuto) | 
			
		
	
		
			
				
					|  |  |  |  |                 } else { | 
			
		
	
		
			
				
					|  |  |  |  |                     copyBackup(File(path)) | 
			
		
	
		
			
				
					|  |  |  |  |                     copyBackup(File(path), isAuto) | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
	
		
			
				
					|  |  |  | @ -97,28 +86,39 @@ object Backup { | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     @Throws(java.lang.Exception::class) | 
			
		
	
		
			
				
					|  |  |  |  |     private fun copyBackup(context: Context, uri: Uri) { | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     private fun copyBackup(context: Context, uri: Uri, isAuto: Boolean) { | 
			
		
	
		
			
				
					|  |  |  |  |         DocumentFile.fromTreeUri(context, uri)?.let { treeDoc -> | 
			
		
	
		
			
				
					|  |  |  |  |             for (fileName in backupFileNames) { | 
			
		
	
		
			
				
					|  |  |  |  |                 val file = File(backupPath + File.separator + fileName) | 
			
		
	
		
			
				
					|  |  |  |  |                 if (file.exists()) { | 
			
		
	
		
			
				
					|  |  |  |  |                     treeDoc.findFile(fileName)?.delete() | 
			
		
	
		
			
				
					|  |  |  |  |                     treeDoc.createFile("", fileName) | 
			
		
	
		
			
				
					|  |  |  |  |                         ?.writeBytes(context, file.readBytes()) | 
			
		
	
		
			
				
					|  |  |  |  |                     if (isAuto) { | 
			
		
	
		
			
				
					|  |  |  |  |                         treeDoc.findFile("auto")?.findFile(fileName)?.delete() | 
			
		
	
		
			
				
					|  |  |  |  |                         DocumentUtils.createFileIfNotExist( | 
			
		
	
		
			
				
					|  |  |  |  |                             treeDoc, | 
			
		
	
		
			
				
					|  |  |  |  |                             fileName, | 
			
		
	
		
			
				
					|  |  |  |  |                             subDirs = *arrayOf("auto") | 
			
		
	
		
			
				
					|  |  |  |  |                         )?.writeBytes(context, file.readBytes()) | 
			
		
	
		
			
				
					|  |  |  |  |                     } else { | 
			
		
	
		
			
				
					|  |  |  |  |                         treeDoc.findFile(fileName)?.delete() | 
			
		
	
		
			
				
					|  |  |  |  |                         treeDoc.createFile("", fileName) | 
			
		
	
		
			
				
					|  |  |  |  |                             ?.writeBytes(context, file.readBytes()) | 
			
		
	
		
			
				
					|  |  |  |  |                     } | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     @Throws(java.lang.Exception::class) | 
			
		
	
		
			
				
					|  |  |  |  |     private fun copyBackup(rootFile: File) { | 
			
		
	
		
			
				
					|  |  |  |  |     private fun copyBackup(rootFile: File, isAuto: Boolean) { | 
			
		
	
		
			
				
					|  |  |  |  |         for (fileName in backupFileNames) { | 
			
		
	
		
			
				
					|  |  |  |  |             val file = File(backupPath + File.separator + fileName) | 
			
		
	
		
			
				
					|  |  |  |  |             if (file.exists()) { | 
			
		
	
		
			
				
					|  |  |  |  |                 file.copyTo( | 
			
		
	
		
			
				
					|  |  |  |  |                     FileUtils.createFileIfNotExist(rootFile, fileName), | 
			
		
	
		
			
				
					|  |  |  |  |                     true | 
			
		
	
		
			
				
					|  |  |  |  |                     if (isAuto) { | 
			
		
	
		
			
				
					|  |  |  |  |                         FileUtils.createFileIfNotExist(rootFile, fileName, "auto") | 
			
		
	
		
			
				
					|  |  |  |  |                     } else { | 
			
		
	
		
			
				
					|  |  |  |  |                         FileUtils.createFileIfNotExist(rootFile, fileName) | 
			
		
	
		
			
				
					|  |  |  |  |                     }, true | 
			
		
	
		
			
				
					|  |  |  |  |                 ) | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
	
		
			
				
					|  |  |  | 
 |