Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit ac2f837

Browse files
authored
Merge pull request #414 from owncloud/new_arch/camera_uploads
[New arch] Camera Uploads
2 parents 022c486 + 22719c8 commit ac2f837

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2021 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*
23+
*/
24+
25+
package com.owncloud.android.lib.resources.files
26+
27+
import android.content.ContentResolver
28+
import android.net.Uri
29+
import android.provider.OpenableColumns
30+
import com.owncloud.android.lib.common.OwnCloudClient
31+
import com.owncloud.android.lib.common.http.HttpConstants
32+
import com.owncloud.android.lib.common.http.methods.webdav.PutMethod
33+
import com.owncloud.android.lib.common.network.WebdavUtils
34+
import com.owncloud.android.lib.common.operations.RemoteOperation
35+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
36+
import okhttp3.MediaType
37+
import okhttp3.MediaType.Companion.toMediaTypeOrNull
38+
import okhttp3.RequestBody
39+
import okio.BufferedSink
40+
import okio.source
41+
import timber.log.Timber
42+
import java.io.IOException
43+
import java.net.URL
44+
45+
class UploadFileFromContentUriOperation(
46+
private val uploadPath: String,
47+
private val lastModified: String,
48+
private val requestBody: ContentUriRequestBody
49+
) : RemoteOperation<Unit>() {
50+
51+
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
52+
val putMethod = PutMethod(URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(uploadPath)), requestBody).apply {
53+
setRetryOnConnectionFailure(false)
54+
addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, requestBody.contentLength().toString())
55+
addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, lastModified)
56+
}
57+
58+
return try {
59+
val status = client.executeHttpMethod(putMethod)
60+
if (isSuccess(status)) {
61+
RemoteOperationResult<Unit>(RemoteOperationResult.ResultCode.OK).apply { data = Unit }
62+
} else {
63+
RemoteOperationResult<Unit>(putMethod)
64+
}
65+
} catch (e: Exception) {
66+
val result = RemoteOperationResult<Unit>(e)
67+
Timber.e(e, "Upload from content uri failed : ${result.logMessage}")
68+
result
69+
}
70+
}
71+
72+
fun isSuccess(status: Int): Boolean {
73+
return status == HttpConstants.HTTP_OK || status == HttpConstants.HTTP_CREATED || status == HttpConstants.HTTP_NO_CONTENT
74+
}
75+
}
76+
77+
class ContentUriRequestBody(
78+
private val contentResolver: ContentResolver,
79+
private val contentUri: Uri
80+
) : RequestBody() {
81+
82+
override fun contentType(): MediaType? {
83+
val contentType = contentResolver.getType(contentUri) ?: return null
84+
return contentType.toMediaTypeOrNull()
85+
}
86+
87+
override fun contentLength(): Long {
88+
contentResolver.query(contentUri, null, null, null, null)?.use { cursor ->
89+
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
90+
cursor.moveToFirst()
91+
return cursor.getLong(sizeIndex)
92+
} ?: return -1
93+
}
94+
95+
override fun writeTo(sink: BufferedSink) {
96+
val inputStream = contentResolver.openInputStream(contentUri)
97+
?: throw IOException("Couldn't open content URI for reading: $contentUri")
98+
99+
inputStream.source().use { source ->
100+
sink.writeAll(source)
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)