Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
90066bf05f |
No files matched your search
@@ -420,7 +420,8 @@ public interface NcApi {
|
||||
@Field("shareWith") String roomToken,
|
||||
@Field("shareType") String shareType,
|
||||
@Field("talkMetaData") String talkMetaData,
|
||||
@Field("referenceId") String referenceId);
|
||||
@Field("referenceId") String referenceId,
|
||||
@Nullable @Field("permissions") Integer permissions);
|
||||
|
||||
@FormUrlEncoded
|
||||
@PUT
|
||||
|
||||
@@ -69,7 +69,8 @@ class ShareOperationWorker(context: Context, workerParams: WorkerParameters) : W
|
||||
roomToken,
|
||||
"10",
|
||||
metaData,
|
||||
"" // no reference id
|
||||
"", // no reference id
|
||||
null // server default permissions
|
||||
)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.blockingSubscribe(
|
||||
|
||||
@@ -63,8 +63,10 @@ import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.HttpException
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.net.HttpURLConnection
|
||||
import java.util.Collections
|
||||
import java.util.UUID
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
@@ -108,6 +110,9 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
||||
lateinit var conversationName: String
|
||||
lateinit var currentUser: User
|
||||
private var isChunkedUploading = false
|
||||
|
||||
// true when the upload itself shared the file, i.e. via postConversationAttachment
|
||||
private var isSharedByUpload = false
|
||||
private var file: File? = null
|
||||
private var chunkedFileUploader: ChunkedFileUploader? = null
|
||||
private var referenceId: String? = null
|
||||
@@ -197,9 +202,10 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
||||
}
|
||||
|
||||
if (uploadSuccess) {
|
||||
// useConversationSubfolders already shares as part of uploadFile() via
|
||||
// postConversationAttachment, so only share explicitly for the plain upload path.
|
||||
val shareSuccess = useConversationSubfolders || shareFile(remotePath, metaData)
|
||||
// The conversation subfolder upload already shares via postConversationAttachment,
|
||||
// so only share explicitly for an upload into the attachment folder.
|
||||
val shareSuccess = isSharedByUpload ||
|
||||
shareFile(remotePath, metaData, sharePermissions(useConversationSubfolders, allowUpdate))
|
||||
if (shareSuccess) {
|
||||
updatePlaceholderStatus(SendStatus.SENT_PENDING_ACK)
|
||||
_uploadCompletedFlow.tryEmit(roomToken)
|
||||
@@ -276,11 +282,24 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
||||
remotePath: String,
|
||||
useConversationSubfolders: Boolean,
|
||||
allowUpdate: Boolean
|
||||
): Boolean =
|
||||
if (file == null) {
|
||||
false
|
||||
} else if (useConversationSubfolders) {
|
||||
): Boolean {
|
||||
val trySubfolders = file != null && useConversationSubfolders
|
||||
val subfolderUploadSuccess = if (trySubfolders) {
|
||||
uploadUsingConversationSubfolders(sourceFileUri, metaData, allowUpdate)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
isSharedByUpload = subfolderUploadSuccess != null
|
||||
if (trySubfolders && subfolderUploadSuccess == null) {
|
||||
// The server answered 501 despite the capability, e.g. because the conversation has message
|
||||
// expiration enabled. Upload to the attachment folder and share the file from there instead.
|
||||
Log.i(TAG, "Conversation subfolders not available, using the attachment folder")
|
||||
}
|
||||
|
||||
return if (file == null) {
|
||||
false
|
||||
} else if (subfolderUploadSuccess != null) {
|
||||
subfolderUploadSuccess
|
||||
} else if (isChunkedUploading) {
|
||||
Log.d(TAG, "starting chunked upload because size is " + file!!.length())
|
||||
val mimeType = context.contentResolver.getType(sourceFileUri)?.toMediaTypeOrNull()
|
||||
@@ -305,6 +324,7 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
||||
.upload(sourceFileUri, fileName, remotePath, null)
|
||||
blockingUpload(observable)
|
||||
}
|
||||
}
|
||||
|
||||
// unlike .blockingFirst(), keeps a Disposable so onStopped() can cancel the underlying OkHttp call
|
||||
private fun blockingUpload(observable: Observable<Boolean>): Boolean {
|
||||
@@ -334,11 +354,17 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads into the conversation's Draft folder and shares the file via postConversationAttachment.
|
||||
*
|
||||
* @return null when the server answers the probe with 501 despite the capability, the
|
||||
* attachment folder has to be used then.
|
||||
*/
|
||||
private fun uploadUsingConversationSubfolders(
|
||||
sourceFileUri: Uri,
|
||||
metaData: String?,
|
||||
allowUpdate: Boolean
|
||||
): Boolean =
|
||||
): Boolean? =
|
||||
runBlocking {
|
||||
val credentials = ApiUtils.getCredentials(
|
||||
currentUser.username,
|
||||
@@ -350,11 +376,13 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
||||
this.allowUpdate = allowUpdate
|
||||
}
|
||||
|
||||
val probeResponse = ncApiCoroutines.probeConversationAttachmentFolder(
|
||||
credentials,
|
||||
ApiUtils.getUrlForChatAttachmentFolder(ApiUtils.API_V1, currentUser.baseUrl, roomToken),
|
||||
fileNames
|
||||
)
|
||||
val probeResponse = nullIfNotImplemented {
|
||||
ncApiCoroutines.probeConversationAttachmentFolder(
|
||||
credentials,
|
||||
ApiUtils.getUrlForChatAttachmentFolder(ApiUtils.API_V1, currentUser.baseUrl, roomToken),
|
||||
fileNames
|
||||
)
|
||||
} ?: return@runBlocking null
|
||||
|
||||
val draftFolderPath = probeResponse.ocs?.data?.folder
|
||||
if (draftFolderPath.isNullOrEmpty()) {
|
||||
@@ -402,7 +430,7 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
||||
}
|
||||
|
||||
@SuppressLint("CheckResult")
|
||||
private fun shareFile(remotePath: String, metaData: String?): Boolean =
|
||||
private fun shareFile(remotePath: String, metaData: String?, permissions: Int?): Boolean =
|
||||
try {
|
||||
ncApi.createRemoteShare(
|
||||
ApiUtils.getCredentials(currentUser.username, currentUser.token),
|
||||
@@ -411,7 +439,8 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
||||
roomToken,
|
||||
"10",
|
||||
metaData,
|
||||
referenceId.orEmpty()
|
||||
referenceId.orEmpty(),
|
||||
permissions
|
||||
).blockingFirst()
|
||||
true
|
||||
} catch (e: NoSuchElementException) {
|
||||
@@ -491,6 +520,9 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
||||
private const val COMPRESS_IMAGES = "COMPRESS_IMAGES"
|
||||
private const val ALLOW_UPDATE = "ALLOW_UPDATE"
|
||||
private const val CHUNK_UPLOAD_THRESHOLD_SIZE: Long = 1024 * 1024
|
||||
private const val SHARE_PERMISSION_READ = 1
|
||||
private const val SHARE_PERMISSION_UPDATE = 2
|
||||
private const val SHARE_PERMISSION_SHARE = 16
|
||||
|
||||
// Total attempts allowed for a single upload (1 initial run + retries) before giving up on a
|
||||
// transient network failure and marking the placeholder FAILED.
|
||||
@@ -508,6 +540,32 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
||||
)
|
||||
val uploadCompletedFlow: SharedFlow<String> = _uploadCompletedFlow
|
||||
|
||||
/**
|
||||
* The user only picks view only or editable when the conversation subfolder capability is present,
|
||||
* so only an attachment folder fallback after a 501 sends permissions. Otherwise the server applies
|
||||
* its default share permissions.
|
||||
*/
|
||||
internal fun sharePermissions(hasConversationSubfolders: Boolean, allowUpdate: Boolean): Int? =
|
||||
when {
|
||||
!hasConversationSubfolders -> null
|
||||
allowUpdate -> SHARE_PERMISSION_READ or SHARE_PERMISSION_UPDATE or SHARE_PERMISSION_SHARE
|
||||
else -> SHARE_PERMISSION_READ or SHARE_PERMISSION_SHARE
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns null when the server answers 501, which the conversation subfolder endpoints do despite
|
||||
* the capability when the conversation can not use them, e.g. because of message expiration.
|
||||
*/
|
||||
internal suspend fun <T> nullIfNotImplemented(request: suspend () -> T): T? =
|
||||
try {
|
||||
request()
|
||||
} catch (e: HttpException) {
|
||||
if (e.code() != HttpURLConnection.HTTP_NOT_IMPLEMENTED) {
|
||||
throw e
|
||||
}
|
||||
null
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
fun clearUploadCompletedReplay() {
|
||||
_uploadCompletedFlow.resetReplayCache()
|
||||
|
||||
@@ -21,15 +21,22 @@ import com.nextcloud.talk.data.database.dao.ChatMessagesDao
|
||||
import com.nextcloud.talk.data.database.model.SendStatus
|
||||
import com.nextcloud.talk.users.UserManager
|
||||
import com.nextcloud.talk.utils.UploadSourceGrants
|
||||
import java.net.HttpURLConnection.HTTP_INTERNAL_ERROR
|
||||
import java.net.HttpURLConnection.HTTP_NOT_IMPLEMENTED
|
||||
import java.util.concurrent.Executor
|
||||
import java.util.concurrent.Executors
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import okhttp3.ResponseBody.Companion.toResponseBody
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.mock
|
||||
import org.mockito.kotlin.verify
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import retrofit2.HttpException
|
||||
import retrofit2.Response
|
||||
import java.util.UUID
|
||||
|
||||
/**
|
||||
@@ -70,6 +77,35 @@ class UploadAndShareFilesWorkerTest {
|
||||
assertEquals(emptyList<Any>(), context().contentResolver.persistedUriPermissions)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a 501 of a conversation subfolder endpoint falls back instead of failing`() {
|
||||
val result = runBlocking {
|
||||
UploadAndShareFilesWorker.nullIfNotImplemented<String> { throw httpException(HTTP_NOT_IMPLEMENTED) }
|
||||
}
|
||||
|
||||
assertNull(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `other errors of a conversation subfolder endpoint still fail the upload`() {
|
||||
val error = runCatching {
|
||||
runBlocking {
|
||||
UploadAndShareFilesWorker.nullIfNotImplemented<String> { throw httpException(HTTP_INTERNAL_ERROR) }
|
||||
}
|
||||
}.exceptionOrNull()
|
||||
|
||||
assertEquals(HTTP_INTERNAL_ERROR, (error as HttpException).code())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the fallback share keeps the permissions the user picked`() {
|
||||
assertEquals(READ or SHARE, UploadAndShareFilesWorker.sharePermissions(true, allowUpdate = false))
|
||||
assertEquals(READ or UPDATE or SHARE, UploadAndShareFilesWorker.sharePermissions(true, allowUpdate = true))
|
||||
assertNull(UploadAndShareFilesWorker.sharePermissions(false, allowUpdate = false))
|
||||
}
|
||||
|
||||
private fun httpException(code: Int): HttpException = HttpException(Response.error<Any>(code, "".toResponseBody()))
|
||||
|
||||
private fun upload(referenceId: String): UUID =
|
||||
UploadAndShareFilesWorker.upload(
|
||||
userId = ACCOUNT_ID,
|
||||
@@ -122,5 +158,8 @@ class UploadAndShareFilesWorkerTest {
|
||||
private const val CONVERSATION_ID = "1@room"
|
||||
private const val WAIT_MILLIS = 30_000L
|
||||
private const val POLL_MILLIS = 20L
|
||||
private const val READ = 1
|
||||
private const val UPDATE = 2
|
||||
private const val SHARE = 16
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user