FilesUploadHelper.kt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. *
  3. * Nextcloud Android client application
  4. *
  5. * @author Tobias Kaminsky
  6. * Copyright (C) 2022 Tobias Kaminsky
  7. * Copyright (C) 2022 Nextcloud GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. package com.owncloud.android.utils
  23. import com.nextcloud.client.account.User
  24. import com.nextcloud.client.jobs.BackgroundJobManager
  25. import com.owncloud.android.MainApp
  26. import com.owncloud.android.datamodel.OCFile
  27. import com.owncloud.android.datamodel.UploadsStorageManager
  28. import com.owncloud.android.db.OCUpload
  29. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder
  30. import com.owncloud.android.files.services.NameCollisionPolicy
  31. import com.owncloud.android.lib.common.network.OnDatatransferProgressListener
  32. import com.owncloud.android.lib.common.utils.Log_OC
  33. import javax.inject.Inject
  34. class FilesUploadHelper {
  35. @Inject
  36. lateinit var backgroundJobManager: BackgroundJobManager
  37. @Inject
  38. lateinit var uploadsStorageManager: UploadsStorageManager
  39. init {
  40. MainApp.getAppComponent().inject(this)
  41. }
  42. @Suppress("LongParameterList")
  43. fun uploadNewFiles(
  44. user: User,
  45. localPaths: Array<String>,
  46. remotePaths: Array<String>,
  47. createRemoteFolder: Boolean,
  48. createdBy: Int,
  49. requiresWifi: Boolean,
  50. requiresCharging: Boolean,
  51. nameCollisionPolicy: NameCollisionPolicy,
  52. localBehavior: Int
  53. ) {
  54. val uploads = localPaths.mapIndexed { index, localPath ->
  55. OCUpload(localPath, remotePaths[index], user.accountName).apply {
  56. this.nameCollisionPolicy = nameCollisionPolicy
  57. isUseWifiOnly = requiresWifi
  58. isWhileChargingOnly = requiresCharging
  59. uploadStatus = UploadsStorageManager.UploadStatus.UPLOAD_IN_PROGRESS
  60. this.createdBy = createdBy
  61. isCreateRemoteFolder = createRemoteFolder
  62. localAction = localBehavior
  63. }
  64. }
  65. uploadsStorageManager.storeUploads(uploads)
  66. backgroundJobManager.startFilesUploadJob(user)
  67. }
  68. fun cancelFileUpload(remotePath: String, user: User) {
  69. // need to update now table in mUploadsStorageManager,
  70. // since the operation will not get to be run by FileUploader#uploadFile
  71. uploadsStorageManager.removeUpload(user.accountName, remotePath)
  72. restartUploadJob(user)
  73. }
  74. fun restartUploadJob(user: User) {
  75. backgroundJobManager.cancelFilesUploadJob(user)
  76. backgroundJobManager.startFilesUploadJob(user)
  77. }
  78. fun uploadUpdatedFile(
  79. user: User,
  80. existingFiles: Array<OCFile>,
  81. behaviour: Int,
  82. nameCollisionPolicy: NameCollisionPolicy
  83. ) {
  84. Log_OC.d(this, "upload updated file")
  85. val uploads = existingFiles.map { file ->
  86. OCUpload(file, user).apply {
  87. fileSize = file.fileLength
  88. this.nameCollisionPolicy = nameCollisionPolicy
  89. isCreateRemoteFolder = true
  90. this.localAction = behaviour
  91. isUseWifiOnly = false
  92. isWhileChargingOnly = false
  93. uploadStatus = UploadsStorageManager.UploadStatus.UPLOAD_IN_PROGRESS
  94. }
  95. }
  96. uploadsStorageManager.storeUploads(uploads)
  97. backgroundJobManager.startFilesUploadJob(user)
  98. }
  99. fun retryUpload(upload: OCUpload, user: User) {
  100. Log_OC.d(this, "retry upload")
  101. upload.uploadStatus = UploadsStorageManager.UploadStatus.UPLOAD_IN_PROGRESS
  102. uploadsStorageManager.updateUpload(upload)
  103. backgroundJobManager.startFilesUploadJob(user)
  104. }
  105. fun addDatatransferProgressListener(
  106. listener: OnDatatransferProgressListener,
  107. targetKey: String
  108. ) {
  109. mBoundListeners[targetKey] = listener
  110. }
  111. fun removeDatatransferProgressListener(
  112. listener: OnDatatransferProgressListener,
  113. targetKey: String
  114. ) {
  115. if (mBoundListeners[targetKey] === listener) {
  116. mBoundListeners.remove(targetKey)
  117. }
  118. }
  119. companion object Progress {
  120. val mBoundListeners = HashMap<String, OnDatatransferProgressListener>()
  121. fun onTransferProgress(
  122. accountName: String?,
  123. remotePath: String?,
  124. progressRate: Long,
  125. totalTransferredSoFar: Long,
  126. totalToTransfer: Long,
  127. fileName: String?
  128. ) {
  129. if (accountName == null || remotePath == null) return
  130. val key: String =
  131. FileUploaderBinder.buildRemoteName(accountName, remotePath)
  132. val boundListener = mBoundListeners[key]
  133. boundListener?.onTransferProgress(progressRate, totalTransferredSoFar, totalToTransfer, fileName)
  134. Log_OC.d("TAG", "Hello")
  135. }
  136. }
  137. }