Skip to content

Commit 732fbfd

Browse files
author
raghavgarg
committed
refactor: improve code quality based on Codacy analysis
Signed-off-by: raghavgarg <[email protected]>
1 parent 8557988 commit 732fbfd

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadWorker.kt

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,28 @@ class FileUploadWorker(
400400
val hash = baseString.hashCode()
401401
val notificationId = Math.abs(hash)
402402

403-
Log_OC.d(TAG, "generateDeterministicNotificationId: Generated notification ID: $notificationId for file: $canonicalPath (size: $fileSize)")
403+
Log_OC.d(
404+
TAG,
405+
"generateDeterministicNotificationId: Generated notification ID: $notificationId " +
406+
"for file: $canonicalPath (size: $fileSize)"
407+
)
404408
notificationId
405-
} catch (e: Exception) {
406-
Log_OC.e(TAG, "generateDeterministicNotificationId: Error generating deterministic notification ID, falling back to random", e)
407-
Random.nextInt()
409+
} catch (e: java.io.IOException) {
410+
Log_OC.e(
411+
TAG,
412+
"generateDeterministicNotificationId: IO error getting canonical path, falling back to localPath hash",
413+
e
414+
)
415+
// Fallback to deterministic hash based on localPath and fileSize
416+
Math.abs("${localPath}_$fileSize".hashCode())
417+
} catch (e: SecurityException) {
418+
Log_OC.e(
419+
TAG,
420+
"generateDeterministicNotificationId: Security error accessing file, falling back to localPath hash",
421+
e
422+
)
423+
// Fallback to deterministic hash based on localPath and fileSize
424+
Math.abs("${localPath}_$fileSize".hashCode())
408425
}
409426
}
410427

@@ -416,7 +433,10 @@ class FileUploadWorker(
416433
val notification = notificationManager.notificationBuilder.build()
417434
val notificationId = Random.nextInt() // Will be replaced by deterministic ID when upload starts
418435

419-
Log_OC.d(TAG, "createForegroundInfo: Creating foreground service for upload with notification ID: $notificationId")
436+
Log_OC.d(
437+
TAG,
438+
"createForegroundInfo: Creating foreground service for upload with notification ID: $notificationId"
439+
)
420440

421441
ForegroundServiceHelper.createWorkerForegroundInfo(
422442
notificationId,

app/src/main/java/com/owncloud/android/operations/FixedChunkUploadRemoteOperation.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public class FixedChunkUploadRemoteOperation extends RemoteOperation implements
7474
// Fixed 1 MB chunk size (1024 * 1024 bytes)
7575
public static final long FIXED_CHUNK_SIZE = 1024 * 1024;
7676

77+
// Constants for repeated string literals
78+
private static final String BYTES_SUFFIX = " bytes";
79+
private static final String SIZE_SEPARATOR = ", size: ";
80+
private static final String DAV_UPLOADS_PATH = "/remote.php/dav/uploads/";
81+
7782
private final String mLocalPath;
7883
private final String mRemotePath;
7984
private final String mMimeType;
@@ -165,8 +170,8 @@ protected RemoteOperationResult run(OwnCloudClient client) {
165170
mFileSize = localFile.length();
166171

167172
Log_OC.d(TAG, "FixedChunkUploadRemoteOperation: Processing file " + localFile.getName() +
168-
" with size " + mFileSize + " bytes");
169-
Log_OC.d(TAG, "FixedChunkUploadRemoteOperation: FIXED_CHUNK_SIZE = " + FIXED_CHUNK_SIZE + " bytes");
173+
" with size " + mFileSize + BYTES_SUFFIX);
174+
Log_OC.d(TAG, "FixedChunkUploadRemoteOperation: FIXED_CHUNK_SIZE = " + FIXED_CHUNK_SIZE + BYTES_SUFFIX);
170175

171176
// This operation is only used for large files (>2MB) to provide multipart upload
172177
Log_OC.d(TAG, "FixedChunkUploadRemoteOperation: Using chunked upload for large file");
@@ -190,7 +195,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
190195
*/
191196
private RemoteOperationResult uploadFileDirectly(OwnCloudClient client, File localFile) {
192197
Log_OC.d(TAG, "FixedChunkUploadRemoteOperation.uploadFileDirectly() - ENTRY");
193-
Log_OC.d(TAG, "uploadFileDirectly: File size = " + localFile.length() + " bytes");
198+
Log_OC.d(TAG, "uploadFileDirectly: File size = " + localFile.length() + BYTES_SUFFIX);
194199

195200
try {
196201
// Use the existing UploadFileRemoteOperation for small files
@@ -236,7 +241,7 @@ private RemoteOperationResult uploadFileInChunks(OwnCloudClient client, File loc
236241

237242
try {
238243
Log_OC.d(TAG, "uploadFileInChunks: Starting Nextcloud v2 chunked upload for file: " + localFile.getName() +
239-
", size: " + mFileSize + " bytes, using " + FIXED_CHUNK_SIZE + " byte chunks");
244+
SIZE_SEPARATOR + mFileSize + BYTES_SUFFIX + ", using " + FIXED_CHUNK_SIZE + " byte chunks");
240245

241246
mTotalChunks = (mFileSize + FIXED_CHUNK_SIZE - 1) / FIXED_CHUNK_SIZE; // Ceiling division
242247
mCurrentChunk = 0;
@@ -284,7 +289,7 @@ private RemoteOperationResult uploadFileInChunks(OwnCloudClient client, File loc
284289
long chunkSize = chunkEnd - chunkStart;
285290

286291
Log_OC.d(TAG, "uploadFileInChunks: Uploading chunk " + mCurrentChunk + "/" + mTotalChunks +
287-
" (bytes " + chunkStart + "-" + (chunkEnd - 1) + ", size: " + chunkSize + ")");
292+
" (bytes " + chunkStart + "-" + (chunkEnd - 1) + SIZE_SEPARATOR + chunkSize + ")");
288293

289294

290295
// Read chunk data
@@ -358,7 +363,7 @@ private RemoteOperationResult createUploadSession(OwnCloudClient client) {
358363
try {
359364
String encodedUsername = URLEncoder.encode(client.getCredentials().getUsername(), StandardCharsets.UTF_8.toString());
360365
String encodedSessionId = URLEncoder.encode(mUploadSessionId, StandardCharsets.UTF_8.toString());
361-
sessionUrl = client.getBaseUri() + "/remote.php/dav/uploads/" + encodedUsername + "/" + encodedSessionId;
366+
sessionUrl = client.getBaseUri() + DAV_UPLOADS_PATH + encodedUsername + "/" + encodedSessionId;
362367
} catch (Exception e) {
363368
Log_OC.e(TAG, "createUploadSession: Error encoding URL components", e);
364369
return new RemoteOperationResult(e);
@@ -417,15 +422,15 @@ private RemoteOperationResult uploadChunkToSession(OwnCloudClient client, byte[]
417422
}
418423

419424
try {
420-
Log_OC.d(TAG, "uploadChunkToSession: Uploading chunk " + chunkNumber + " with " + chunkData.length + " bytes");
425+
Log_OC.d(TAG, "uploadChunkToSession: Uploading chunk " + chunkNumber + " with " + chunkData.length + BYTES_SUFFIX);
421426

422427
// Construct chunk URL - chunks are named as numbers (1, 2, 3...)
423428
String chunkUrl;
424429
try {
425430
String encodedUsername = URLEncoder.encode(client.getCredentials().getUsername(), StandardCharsets.UTF_8.toString());
426431
String encodedSessionId = URLEncoder.encode(mUploadSessionId, StandardCharsets.UTF_8.toString());
427432
String chunkFileName = String.format("%05d", chunkNumber); // 5-digit padded number
428-
chunkUrl = client.getBaseUri() + "/remote.php/dav/uploads/" + encodedUsername + "/" + encodedSessionId + "/" + chunkFileName;
433+
chunkUrl = client.getBaseUri() + DAV_UPLOADS_PATH + encodedUsername + "/" + encodedSessionId + "/" + chunkFileName;
429434
} catch (Exception e) {
430435
Log_OC.e(TAG, "uploadChunkToSession: Error encoding URL components", e);
431436
return new RemoteOperationResult(e);
@@ -512,7 +517,7 @@ private RemoteOperationResult assembleChunks(OwnCloudClient client) {
512517
try {
513518
String encodedUsername = URLEncoder.encode(client.getCredentials().getUsername(), StandardCharsets.UTF_8.toString());
514519
String encodedSessionId = URLEncoder.encode(mUploadSessionId, StandardCharsets.UTF_8.toString());
515-
assemblySourceUrl = client.getBaseUri() + "/remote.php/dav/uploads/" + encodedUsername + "/" + encodedSessionId + "/.file";
520+
assemblySourceUrl = client.getBaseUri() + DAV_UPLOADS_PATH + encodedUsername + "/" + encodedSessionId + "/.file";
516521
} catch (Exception e) {
517522
Log_OC.e(TAG, "assembleChunks: Error encoding source URL", e);
518523
return new RemoteOperationResult(e);
@@ -658,7 +663,7 @@ private int checkExistingSession(OwnCloudClient client) {
658663
try {
659664
String encodedUsername = URLEncoder.encode(client.getCredentials().getUsername(), StandardCharsets.UTF_8.toString());
660665
String encodedSessionId = URLEncoder.encode(mUploadSessionId, StandardCharsets.UTF_8.toString());
661-
String sessionUrl = client.getBaseUri() + "/remote.php/dav/uploads/" + encodedUsername + "/" + encodedSessionId;
666+
String sessionUrl = client.getBaseUri() + DAV_UPLOADS_PATH + encodedUsername + "/" + encodedSessionId;
662667

663668
Log_OC.d(TAG, "checkExistingSession: Checking for existing session: " + sessionUrl);
664669

@@ -747,7 +752,7 @@ private void updateProgress(long bytesUploaded, String fileName) {
747752
// Calculate percentage - same logic as FileUploadWorker's getPercent()
748753
int currentPercent = (int) ((100.0 * bytesUploaded) / mFileSize);
749754

750-
Log_OC.d(TAG, "updateProgress: " + bytesUploaded + "/" + mFileSize + " bytes (" + currentPercent + "%)");
755+
Log_OC.d(TAG, "updateProgress: " + bytesUploaded + "/" + mFileSize + BYTES_SUFFIX + " (" + currentPercent + "%)");
751756

752757
// Report progress to all listeners (including FileUploadWorker)
753758
for (OnDatatransferProgressListener listener : mDataTransferListeners) {

0 commit comments

Comments
 (0)