Skip to content

Commit b46f82c

Browse files
author
Julian Kast
committed
Add logic to clone SdlFile, and prevent multiples of the same file being uploaded via fileManager
1 parent 9a2398f commit b46f82c

4 files changed

Lines changed: 57 additions & 37 deletions

File tree

android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlArtwork.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,10 @@ private Image createImageRPC() {
159159
*/
160160
@Override
161161
public SdlArtwork clone() {
162-
try {
163-
SdlArtwork artwork = (SdlArtwork) super.clone();
164-
if (artwork != null) {
165-
artwork.imageRPC = artwork.createImageRPC();
166-
}
162+
SdlArtwork artwork = (SdlArtwork) super.clone();
163+
if (artwork != null) {
164+
artwork.imageRPC = artwork.createImageRPC();
167165
return artwork;
168-
} catch (CloneNotSupportedException e) {
169-
if (DebugTool.isDebugEnabled()) {
170-
throw new RuntimeException("Clone not supported by super class");
171-
}
172166
}
173167
return null;
174168
}

android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlFile.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import com.smartdevicelink.proxy.rpc.enums.FileType;
4040
import com.smartdevicelink.proxy.rpc.enums.StaticIconName;
41+
import com.smartdevicelink.util.DebugTool;
4142

4243
import java.security.MessageDigest;
4344
import java.security.NoSuchAlgorithmException;
@@ -368,4 +369,22 @@ public boolean equals(Object o) {
368369
// return comparison
369370
return hashCode() == o.hashCode();
370371
}
372+
373+
/**
374+
* Creates a deep copy of the object
375+
*
376+
* @return deep copy of the object, null if an exception occurred
377+
*/
378+
@Override
379+
public SdlFile clone() {
380+
try {
381+
SdlFile fileClone = (SdlFile) super.clone();
382+
return fileClone;
383+
} catch (CloneNotSupportedException e) {
384+
if (DebugTool.isDebugEnabled()) {
385+
throw new RuntimeException("Clone not supported by super class");
386+
}
387+
}
388+
return null;
389+
}
371390
}

base/src/main/java/com/smartdevicelink/managers/file/BaseFileManager.java

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ abstract class BaseFileManager extends BaseSubManager {
7070
private HashMap<String, Integer> failedFileUploadsCount;
7171
private final int maxFileUploadAttempts;
7272
private final int maxArtworkUploadAttempts;
73+
String fileManagerCannotOverwriteError = "Cannot overwrite remote file. The remote file system already has a file of this name, and the file manager is set to not automatically overwrite files.";
74+
7375

7476
/**
7577
* Constructor for BaseFileManager
@@ -405,46 +407,29 @@ private void uploadFilePrivate(@NonNull final SdlFile file, final FileManagerCom
405407
return;
406408
}
407409

408-
// HAX: [#827](https://github.com/smartdevicelink/sdl_ios/issues/827) Older versions of Core
409-
// had a bug where list files would cache incorrectly. This led to attempted uploads failing
410-
// due to the system thinking they were already there when they were not. This is only needed
411-
// if connecting to Core v4.3.1 or less which corresponds to RPC v4.3.1 or less
412-
Version rpcVersion = new Version(internalInterface.getSdlMsgVersion());
413-
if (!file.isPersistent() && !hasUploadedFile(file) && new Version(4, 4, 0).isNewerThan(rpcVersion) == 1) {
414-
file.setOverwrite(true);
415-
}
416-
417-
// Check our overwrite settings and error out if it would overwrite
418-
if (!file.getOverwrite() && mutableRemoteFileNames.contains(file.getName())) {
419-
String errorMessage = "Cannot overwrite remote file. The remote file system already has a file of this name, and the file manager is set to not automatically overwrite files.";
420-
DebugTool.logWarning(TAG, errorMessage);
421-
if (listener != null) {
422-
listener.onComplete(true, bytesAvailable, null, errorMessage);
423-
}
424-
return;
425-
}
426-
427410
// If we didn't error out over the overwrite, then continue on
428411
sdl_uploadFilePrivate(file, listener);
429412
}
430413

431414
private void sdl_uploadFilePrivate(@NonNull final SdlFile file, final FileManagerCompletionListener listener) {
432-
final String fileName = file.getName();
415+
final SdlFile fileClone = file.clone();
433416

434-
SdlFileWrapper fileWrapper = new SdlFileWrapper(file, new FileManagerCompletionListener() {
417+
SdlFileWrapper fileWrapper = new SdlFileWrapper(fileClone, new FileManagerCompletionListener() {
435418
@Override
436419
public void onComplete(boolean success, int bytesAvailable, Collection<String> fileNames, String errorMessage) {
437420
if (success) {
438421
BaseFileManager.this.bytesAvailable = bytesAvailable;
439-
BaseFileManager.this.mutableRemoteFileNames.add(fileName);
440-
BaseFileManager.this.uploadedEphemeralFileNames.add(fileName);
441-
} else {
442-
incrementFailedUploadCountForFileName(file.getName(), BaseFileManager.this.failedFileUploadsCount);
422+
BaseFileManager.this.mutableRemoteFileNames.add(fileClone.getName());
423+
if (!file.isPersistent()) {
424+
BaseFileManager.this.uploadedEphemeralFileNames.add(fileClone.getName());
425+
}
426+
} else if (!fileManagerCannotOverwriteError.equals(errorMessage)) {
427+
incrementFailedUploadCountForFileName(fileClone.getName(), BaseFileManager.this.failedFileUploadsCount);
443428

444-
int maxUploadCount = file instanceof SdlArtwork ? maxArtworkUploadAttempts : maxFileUploadAttempts;
445-
if (canFileBeUploadedAgain(file, maxUploadCount, failedFileUploadsCount)) {
429+
int maxUploadCount = fileClone instanceof SdlArtwork ? maxArtworkUploadAttempts : maxFileUploadAttempts;
430+
if (canFileBeUploadedAgain(fileClone, maxUploadCount, failedFileUploadsCount)) {
446431
DebugTool.logInfo(TAG, String.format("Attempting to resend file with name %s after a failed upload attempt", file.getName()));
447-
sdl_uploadFilePrivate(file, listener);
432+
sdl_uploadFilePrivate(fileClone, listener);
448433
return;
449434
}
450435
}

base/src/main/java/com/smartdevicelink/managers/file/UploadFileOperation.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.smartdevicelink.proxy.rpc.PutFileResponse;
4545
import com.smartdevicelink.proxy.rpc.listeners.OnRPCResponseListener;
4646
import com.smartdevicelink.util.DebugTool;
47+
import com.smartdevicelink.util.Version;
4748

4849
import java.io.IOException;
4950
import java.io.InputStream;
@@ -81,6 +82,27 @@ private void start() {
8182
return;
8283
}
8384

85+
SdlFile file = fileWrapper.getFile();
86+
// HAX: [#827](https://github.com/smartdevicelink/sdl_ios/issues/827) Older versions of Core
87+
// had a bug where list files would cache incorrectly. This led to attempted uploads failing
88+
// due to the system thinking they were already there when they were not. This is only needed
89+
// if connecting to Core v4.3.1 or less which corresponds to RPC v4.3.1 or less
90+
if (internalInterface.get() != null && fileManager.get() != null) {
91+
Version rpcVersion = new Version(internalInterface.get().getSdlMsgVersion());
92+
if (!file.isPersistent() && !fileManager.get().hasUploadedFile(file) && new Version(4, 4, 0).isNewerThan(rpcVersion) == 1) {
93+
file.setOverwrite(true);
94+
}
95+
// Check our overwrite settings and error out if it would overwrite
96+
if (!file.getOverwrite() && fileManager.get().mutableRemoteFileNames.contains(file.getName())) {
97+
DebugTool.logWarning(TAG, fileManager.get().fileManagerCannotOverwriteError);
98+
if (this.fileWrapper.getCompletionListener() != null) {
99+
this.fileWrapper.getCompletionListener().onComplete(false, bytesAvailable, null, fileManager.get().fileManagerCannotOverwriteError);
100+
}
101+
onFinished();
102+
return;
103+
}
104+
}
105+
84106
int mtuSize = 0;
85107
if (internalInterface.get() != null) {
86108
mtuSize = (int) internalInterface.get().getMtu(SessionType.RPC);

0 commit comments

Comments
 (0)