From 72df6ddf82560cacd868fca22278df2ca7af741d Mon Sep 17 00:00:00 2001
From: agarcia <agarcia@solidgeargroup.com>
Date: Thu, 13 Aug 2020 12:57:32 +0200
Subject: [PATCH] Add download file to file service

---
 .../files/DownloadRemoteFileOperation.kt          | 15 +++++----------
 .../lib/resources/files/services/FileService.kt   |  5 +++++
 .../services/implementation/OCFileService.kt      | 11 +++++++++++
 3 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.kt
index 7bf535b5..c00ea76f 100644
--- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.kt
+++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.kt
@@ -61,10 +61,10 @@ class DownloadRemoteFileOperation(
     override fun run(client: OwnCloudClient): RemoteOperationResult<Any> {
         var result: RemoteOperationResult<Any>
 
-        /// download will be performed to a temporal file, then moved to the final location
+        // download will be performed to a temporal file, then moved to the final location
         val tmpFile = File(tmpPath)
 
-        /// perform the download
+        // perform the download
         try {
             tmpFile.mkdirs()
             result = downloadFile(client, tmpFile)
@@ -145,15 +145,15 @@ class DownloadRemoteFileOperation(
                     // TODO some kind of error control!
                 }
 
-            } else if (status != FORBIDDEN_ERROR && status != SERVICE_UNAVAILABLE_ERROR) {
+            } else if (status != HttpConstants.HTTP_FORBIDDEN && status != HttpConstants.HTTP_SERVICE_UNAVAILABLE) {
                 client.exhaustResponse(getMethod.getResponseBodyAsStream())
             } // else, body read by RemoteOperationResult constructor
 
             result =
                 if (isSuccess(status)) {
-                    RemoteOperationResult<Any>(RemoteOperationResult.ResultCode.OK)
+                    RemoteOperationResult(RemoteOperationResult.ResultCode.OK)
                 } else {
-                    RemoteOperationResult<Any>(getMethod)
+                    RemoteOperationResult(getMethod)
                 }
         } finally {
             fos?.close()
@@ -181,9 +181,4 @@ class DownloadRemoteFileOperation(
         mCancellationRequested.set(true) // atomic set; there is no need of synchronizing it
     }
 
-    companion object {
-        private const val FORBIDDEN_ERROR = 403
-        private const val SERVICE_UNAVAILABLE_ERROR = 503
-    }
-
 }
diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/FileService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/FileService.kt
index 904496c0..ec0b7421 100644
--- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/FileService.kt
+++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/FileService.kt
@@ -41,6 +41,11 @@ interface FileService : Service {
         isChunkFolder: Boolean = false
     ): RemoteOperationResult<Unit>
 
+    fun downloadFile(
+        remotePath: String,
+        localTempPath: String
+    ): RemoteOperationResult<Any>
+
     fun refreshFolder(
         remotePath: String
     ): RemoteOperationResult<ArrayList<RemoteFile>>
diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/implementation/OCFileService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/implementation/OCFileService.kt
index a21c6fe0..d49533bb 100644
--- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/implementation/OCFileService.kt
+++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/implementation/OCFileService.kt
@@ -27,12 +27,14 @@ import com.owncloud.android.lib.common.OwnCloudClient
 import com.owncloud.android.lib.common.operations.RemoteOperationResult
 import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation
 import com.owncloud.android.lib.resources.files.CreateRemoteFolderOperation
+import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation
 import com.owncloud.android.lib.resources.files.GetUrlToOpenInWebRemoteOperation
 import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation
 import com.owncloud.android.lib.resources.files.RemoteFile
 import com.owncloud.android.lib.resources.files.services.FileService
 
 class OCFileService(override val client: OwnCloudClient) : FileService {
+
     override fun checkPathExistence(
         path: String,
         isUserLogged: Boolean
@@ -56,6 +58,15 @@ class OCFileService(override val client: OwnCloudClient) : FileService {
             isChunksFolder = isChunkFolder
         ).execute(client)
 
+    override fun downloadFile(
+        remotePath: String,
+        localTempPath: String
+    ): RemoteOperationResult<Any> =
+        DownloadRemoteFileOperation(
+            remotePath = remotePath,
+            localFolderPath = localTempPath
+        ).execute(client)
+
     override fun refreshFolder(remotePath: String): RemoteOperationResult<ArrayList<RemoteFile>> =
         ReadRemoteFolderOperation(
             remotePath = remotePath