Skip to content

Commit d3776f9

Browse files
committed
Fix
1 parent 5808a09 commit d3776f9

File tree

10 files changed

+416
-391
lines changed

10 files changed

+416
-391
lines changed

library/src/main/java/com/owncloud/android/lib/resources/albums/CopyFileToAlbumRemoteOperation.kt

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,60 +39,62 @@ class CopyFileToAlbumRemoteOperation @JvmOverloads constructor(
3939
* @param client Client object to communicate with the remote ownCloud server.
4040
*/
4141
@Deprecated("Deprecated in Java")
42+
@Suppress("TooGenericExceptionCaught")
4243
override fun run(client: OwnCloudClient): RemoteOperationResult<Any> {
4344
/** check parameters */
4445

46+
var result: RemoteOperationResult<Any>
4547
if (mTargetRemotePath == mSrcRemotePath) {
4648
// nothing to do!
47-
return RemoteOperationResult(ResultCode.OK)
48-
}
49+
result = RemoteOperationResult(ResultCode.OK)
50+
} else if (mTargetRemotePath.startsWith(mSrcRemotePath)) {
51+
result = RemoteOperationResult(ResultCode.INVALID_COPY_INTO_DESCENDANT)
52+
} else {
53+
/** perform remote operation */
54+
var copyMethod: CopyMethod? = null
55+
try {
56+
copyMethod = CopyMethod(
57+
client.getFilesDavUri(this.mSrcRemotePath),
58+
"${client.baseUri}/remote.php/dav/photos/${client.userId}/albums${
59+
WebdavUtils.encodePath(
60+
mTargetRemotePath
61+
)
62+
}",
63+
false
64+
)
65+
val status = client.executeMethod(
66+
copyMethod,
67+
sessionTimeOut.readTimeOut,
68+
sessionTimeOut.connectionTimeOut
69+
)
4970

50-
if (mTargetRemotePath.startsWith(mSrcRemotePath)) {
51-
return RemoteOperationResult(ResultCode.INVALID_COPY_INTO_DESCENDANT)
52-
}
71+
/** process response */
72+
result = when (status) {
73+
HttpStatus.SC_MULTI_STATUS -> processPartialError(copyMethod)
74+
HttpStatus.SC_PRECONDITION_FAILED -> {
75+
client.exhaustResponse(copyMethod.responseBodyAsStream)
76+
RemoteOperationResult<Any>(ResultCode.INVALID_OVERWRITE)
77+
}
5378

54-
/** perform remote operation */
55-
var copyMethod: CopyMethod? = null
56-
var result: RemoteOperationResult<Any>
57-
try {
58-
copyMethod = CopyMethod(
59-
client.getFilesDavUri(this.mSrcRemotePath),
60-
"${client.baseUri}/remote.php/dav/photos/${client.userId}/albums${
61-
WebdavUtils.encodePath(
62-
mTargetRemotePath
63-
)
64-
}",
65-
false
66-
)
67-
val status = client.executeMethod(
68-
copyMethod,
69-
sessionTimeOut.readTimeOut,
70-
sessionTimeOut.connectionTimeOut
71-
)
79+
else -> {
80+
client.exhaustResponse(copyMethod.responseBodyAsStream)
81+
RemoteOperationResult<Any>(isSuccess(status), copyMethod)
82+
}
83+
}
7284

73-
/** process response */
74-
if (status == HttpStatus.SC_MULTI_STATUS) {
75-
result = processPartialError(copyMethod)
76-
} else if (status == HttpStatus.SC_PRECONDITION_FAILED) {
77-
result = RemoteOperationResult<Any>(ResultCode.INVALID_OVERWRITE)
78-
client.exhaustResponse(copyMethod.responseBodyAsStream)
79-
} else {
80-
result = RemoteOperationResult<Any>(isSuccess(status), copyMethod)
81-
client.exhaustResponse(copyMethod.responseBodyAsStream)
85+
Log.i(
86+
TAG,
87+
"Copy $mSrcRemotePath to $mTargetRemotePath : ${result.logMessage}"
88+
)
89+
} catch (e: Exception) {
90+
result = RemoteOperationResult<Any>(e)
91+
Log.e(
92+
TAG,
93+
"Copy $mSrcRemotePath to $mTargetRemotePath : ${result.logMessage}", e
94+
)
95+
} finally {
96+
copyMethod?.releaseConnection()
8297
}
83-
84-
Log.i(
85-
TAG,
86-
"Copy $mSrcRemotePath to $mTargetRemotePath : ${result.logMessage}"
87-
)
88-
} catch (e: Exception) {
89-
result = RemoteOperationResult<Any>(e)
90-
Log.e(
91-
TAG,
92-
"Copy $mSrcRemotePath to $mTargetRemotePath : ${result.logMessage}", e
93-
)
94-
} finally {
95-
copyMethod?.releaseConnection()
9698
}
9799

98100
return result
@@ -128,7 +130,7 @@ class CopyFileToAlbumRemoteOperation @JvmOverloads constructor(
128130
var i = 0
129131
while (i < responses.size && !failFound) {
130132
status = responses[i].status
131-
failFound = (!status.isNullOrEmpty() && status[0].statusCode > 299
133+
failFound = (!status.isNullOrEmpty() && status[0].statusCode > FAILED_STATUS_CODE
132134
)
133135
i++
134136
}
@@ -147,5 +149,6 @@ class CopyFileToAlbumRemoteOperation @JvmOverloads constructor(
147149

148150
companion object {
149151
private val TAG: String = CopyFileToAlbumRemoteOperation::class.java.simpleName
152+
private const val FAILED_STATUS_CODE = 299
150153
}
151154
}

library/src/main/java/com/owncloud/android/lib/resources/albums/CreateNewAlbumRemoteOperation.kt

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,58 +13,60 @@ import com.owncloud.android.lib.common.network.WebdavUtils
1313
import com.owncloud.android.lib.common.operations.RemoteOperation
1414
import com.owncloud.android.lib.common.operations.RemoteOperationResult
1515
import com.owncloud.android.lib.common.utils.Log_OC
16-
import com.owncloud.android.lib.resources.albums.CreateNewAlbumRemoteOperation
1716
import org.apache.commons.httpclient.HttpStatus
1817
import org.apache.jackrabbit.webdav.client.methods.MkColMethod
1918

20-
class CreateNewAlbumRemoteOperation @JvmOverloads constructor(
21-
val newAlbumName: String,
22-
private val sessionTimeOut: SessionTimeOut = defaultSessionTimeOut
23-
) :
24-
RemoteOperation<Void>() {
25-
/**
26-
* Performs the operation.
27-
*
28-
* @param client Client object to communicate with the remote ownCloud server.
29-
*/
30-
@Deprecated("Deprecated in Java")
31-
override fun run(client: OwnCloudClient): RemoteOperationResult<Void> {
32-
var mkCol: MkColMethod? = null
33-
var result: RemoteOperationResult<Void>
34-
try {
35-
mkCol = MkColMethod(
36-
"${client.baseUri}/remote.php/dav/photos/${client.userId}/albums${
37-
WebdavUtils.encodePath(
38-
newAlbumName
19+
class CreateNewAlbumRemoteOperation
20+
@JvmOverloads
21+
constructor(
22+
val newAlbumName: String,
23+
private val sessionTimeOut: SessionTimeOut = defaultSessionTimeOut
24+
) : RemoteOperation<Void>() {
25+
/**
26+
* Performs the operation.
27+
*
28+
* @param client Client object to communicate with the remote ownCloud server.
29+
*/
30+
@Deprecated("Deprecated in Java")
31+
@Suppress("TooGenericExceptionCaught")
32+
override fun run(client: OwnCloudClient): RemoteOperationResult<Void> {
33+
var mkCol: MkColMethod? = null
34+
var result: RemoteOperationResult<Void>
35+
try {
36+
mkCol =
37+
MkColMethod(
38+
"${client.baseUri}/remote.php/dav/photos/${client.userId}/albums${
39+
WebdavUtils.encodePath(
40+
newAlbumName
41+
)
42+
}"
3943
)
40-
}"
41-
)
42-
client.executeMethod(
43-
mkCol,
44-
sessionTimeOut.readTimeOut,
45-
sessionTimeOut.connectionTimeOut
46-
)
47-
if (HttpStatus.SC_METHOD_NOT_ALLOWED == mkCol.statusCode) {
48-
result =
49-
RemoteOperationResult(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS)
50-
} else {
51-
result = RemoteOperationResult(mkCol.succeeded(), mkCol)
52-
result.resultData = null
44+
client.executeMethod(
45+
mkCol,
46+
sessionTimeOut.readTimeOut,
47+
sessionTimeOut.connectionTimeOut
48+
)
49+
if (HttpStatus.SC_METHOD_NOT_ALLOWED == mkCol.statusCode) {
50+
result =
51+
RemoteOperationResult(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS)
52+
} else {
53+
result = RemoteOperationResult(mkCol.succeeded(), mkCol)
54+
result.resultData = null
55+
}
56+
57+
Log_OC.d(TAG, "Create album $newAlbumName : ${result.logMessage}")
58+
client.exhaustResponse(mkCol.responseBodyAsStream)
59+
} catch (e: Exception) {
60+
result = RemoteOperationResult(e)
61+
Log_OC.e(TAG, "Create album $newAlbumName : ${result.logMessage}", e)
62+
} finally {
63+
mkCol?.releaseConnection()
5364
}
5465

55-
Log_OC.d(TAG, "Create album $newAlbumName : ${result.logMessage}")
56-
client.exhaustResponse(mkCol.responseBodyAsStream)
57-
} catch (e: Exception) {
58-
result = RemoteOperationResult(e)
59-
Log_OC.e(TAG, "Create album $newAlbumName : ${result.logMessage}", e)
60-
} finally {
61-
mkCol?.releaseConnection()
66+
return result
6267
}
6368

64-
return result
65-
}
66-
67-
companion object {
68-
private val TAG: String = CreateNewAlbumRemoteOperation::class.java.simpleName
69+
companion object {
70+
private val TAG: String = CreateNewAlbumRemoteOperation::class.java.simpleName
71+
}
6972
}
70-
}

library/src/main/java/com/owncloud/android/lib/resources/albums/PhotoAlbumEntry.kt

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import java.text.SimpleDateFormat
1919
import java.util.Date
2020
import java.util.Locale
2121

22-
class PhotoAlbumEntry(response: MultiStatusResponse) {
22+
class PhotoAlbumEntry(
23+
response: MultiStatusResponse
24+
) {
2325
val href: String
2426
val lastPhoto: Long
2527
val nbItems: Int
@@ -28,6 +30,7 @@ class PhotoAlbumEntry(response: MultiStatusResponse) {
2830

2931
companion object {
3032
private val dateFormat = SimpleDateFormat("MMM yyyy", Locale.US)
33+
private const val MILLIS = 1000L
3134
}
3235

3336
init {
@@ -42,27 +45,28 @@ class PhotoAlbumEntry(response: MultiStatusResponse) {
4245
this.dateRange = parseString(properties, WebdavEntry.PROPERTY_DATE_RANGE)
4346
}
4447

45-
private fun parseString(props: DavPropertySet, name: String): String? {
48+
private fun parseString(
49+
props: DavPropertySet,
50+
name: String
51+
): String? {
4652
val propName = DavPropertyName.create(name, Namespace.getNamespace("nc", WebdavEntry.NAMESPACE_NC))
4753
val prop = props[propName]
4854
return if (prop != null && prop.value != null) prop.value.toString() else null
4955
}
5056

51-
private fun parseInt(value: String?): Int {
52-
return try {
57+
private fun parseInt(value: String?): Int =
58+
try {
5359
value?.toInt() ?: 0
5460
} catch (_: NumberFormatException) {
5561
0
5662
}
57-
}
5863

59-
private fun parseLong(value: String?): Long {
60-
return try {
64+
private fun parseLong(value: String?): Long =
65+
try {
6166
value?.toLong() ?: 0L
6267
} catch (_: NumberFormatException) {
6368
0L
6469
}
65-
}
6670

6771
val albumName: String
6872
get() {
@@ -79,13 +83,14 @@ class PhotoAlbumEntry(response: MultiStatusResponse) {
7983
return try {
8084
val obj = JSONObject(dateRange ?: return dateFormat.format(currentDate))
8185
val startTimestamp = obj.optLong("start", 0)
82-
if (startTimestamp > 0)
83-
dateFormat.format(Date(startTimestamp * 1000L))
84-
else
86+
if (startTimestamp > 0) {
87+
dateFormat.format(Date(startTimestamp * MILLIS))
88+
} else {
8589
dateFormat.format(currentDate)
90+
}
8691
} catch (e: JSONException) {
8792
e.printStackTrace()
8893
dateFormat.format(currentDate)
8994
}
9095
}
91-
}
96+
}

0 commit comments

Comments
 (0)