瀏覽代碼

FilesExport*: fix detekt issues

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
Álvaro Brey 2 年之前
父節點
當前提交
3c7e429465

+ 7 - 7
app/src/main/java/com/nextcloud/client/jobs/FilesExportWork.kt

@@ -77,19 +77,20 @@ class FilesExportWork(
 
     private fun exportFiles(fileIDs: LongArray): Int {
         var successfulExports = 0
-        for (fileID in fileIDs) {
-            val ocFile = storageManager.getFileById(fileID)
-            if (ocFile != null) {
-                // check if storage is left
+        fileIDs
+            .asSequence()
+            .map { storageManager.getFileById(it) }
+            .filterNotNull()
+            .forEach { ocFile ->
                 if (!FileStorageUtils.checkIfEnoughSpace(ocFile)) {
                     showErrorNotification(successfulExports)
-                    break
+                    return@forEach
                 }
 
                 if (ocFile.isDown) {
                     try {
                         exportFile(ocFile)
-                    } catch (e: RuntimeException) {
+                    } catch (e: IllegalStateException) {
                         Log_OC.e(TAG, "Error exporting file", e)
                         showErrorNotification(successfulExports)
                     }
@@ -99,7 +100,6 @@ class FilesExportWork(
 
                 successfulExports++
             }
-        }
         return successfulExports
     }
 

+ 20 - 14
app/src/main/java/com/owncloud/android/utils/FileExportUtils.kt

@@ -35,12 +35,9 @@ import java.io.FileInputStream
 import java.io.FileNotFoundException
 import java.io.FileOutputStream
 import java.io.IOException
+import java.io.InputStream
 
 class FileExportUtils {
-    companion object {
-        const val INITIAL_RENAME_COUNT = 2
-    }
-
     @Throws(IllegalStateException::class)
     fun exportFile(
         fileName: String,
@@ -148,23 +145,27 @@ class FileExportUtils {
             } else if (file != null) {
                 FileInputStream(file)
             } else {
-                throw IllegalStateException("ocFile and file both may not be null")
+                error("ocFile and file both may not be null")
             }
 
-            inputStream.use { fis ->
-                outputStream.use { os ->
-                    val buffer = ByteArray(1024)
-                    var len: Int
-                    while (fis!!.read(buffer).also { len = it } != -1) {
-                        os.write(buffer, 0, len)
-                    }
-                }
-            }
+            copyStream(inputStream!!, outputStream)
         } catch (e: IOException) {
             Log_OC.e(this, "Cannot write file", e)
         }
     }
 
+    private fun copyStream(inputStream: InputStream, outputStream: FileOutputStream) {
+        inputStream.use { input ->
+            outputStream.use { output ->
+                val buffer = ByteArray(COPY_BUFFER_SIZE)
+                var len: Int
+                while (input.read(buffer).also { len = it } != -1) {
+                    output.write(buffer, 0, len)
+                }
+            }
+        }
+    }
+
     private fun generateNewName(name: String, count: Int): String {
         val extPos = name.lastIndexOf('.')
         val suffix = " ($count)"
@@ -178,4 +179,9 @@ class FileExportUtils {
             name + suffix
         }
     }
+
+    companion object {
+        private const val INITIAL_RENAME_COUNT = 2
+        private const val COPY_BUFFER_SIZE = 1024
+    }
 }