Skip to content

Commit 6a565fc

Browse files
authored
Adding file path check to avoid NumberFormatException due to empty file path (firebase#3865)
As the issue firebase#3864 details, this is an edge case I would like to fix and shouldn't occur in regular occurrence. An alternative to this fix is to potentiall change CUSTOM_MODEL_ROOT_PATH = "com.google.firebase.ml.custom.models"; to something different than what FirebaseModelManager was using.
1 parent 921da2e commit 6a565fc

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/internal/ModelFileManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public static ModelFileManager getInstance(@NonNull FirebaseApp app) {
7070
void deleteNonLatestCustomModels() throws FirebaseMlException {
7171
File root = getDirImpl("");
7272

73-
boolean ret = true;
7473
if (root.isDirectory()) {
7574
for (File f : root.listFiles()) {
7675
// for each custom model sub directory - extract customModelName and clean up old models.
@@ -212,7 +211,7 @@ public synchronized File moveModelToDestinationFolder(
212211
public synchronized void deleteOldModels(
213212
@NonNull String modelName, @NonNull String latestModelFilePath) {
214213
File modelFolder = getModelDirUnsafe(modelName);
215-
if (!modelFolder.exists()) {
214+
if (!modelFolder.exists() || latestModelFilePath.isEmpty()) {
216215
return;
217216
}
218217

firebase-ml-modeldownloader/src/test/java/com/google/firebase/ml/modeldownloader/internal/ModelFileManagerTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,29 @@ public void deleteNonLatestCustomModels_fileToDelete()
188188
assertTrue(new File(modelDestinationFolder + "/1").exists());
189189
}
190190

191+
@Test
192+
public void deleteNonLatestCustomModels_whenModelOnDiskButNotInPreferences()
193+
throws FirebaseMlException, IOException {
194+
String modelDestinationFolder2 = setUpTestingFiles(app, MODEL_NAME_2);
195+
196+
// Was just downloaded
197+
MoveFileToDestination(modelDestinationFolder, testModelFile, CUSTOM_MODEL_NO_FILE, 0);
198+
// Was downloaded previously via FirebaseModelManager
199+
MoveFileToDestination(modelDestinationFolder2, testModelFile2, CUSTOM_MODEL_NO_FILE_2, 0);
200+
201+
sharedPreferencesUtil.setLoadedCustomModelDetails(
202+
new CustomModel(MODEL_NAME, MODEL_HASH, 100, 0, modelDestinationFolder + "/0"));
203+
204+
// Download in progress, hence file path is not present
205+
sharedPreferencesUtil.setLoadedCustomModelDetails(
206+
new CustomModel(MODEL_NAME_2, MODEL_HASH_2, 100, 0));
207+
208+
fileManager.deleteNonLatestCustomModels();
209+
210+
assertTrue(new File(modelDestinationFolder + "/0").exists());
211+
assertTrue(new File(modelDestinationFolder2 + "/0").exists());
212+
}
213+
191214
@Test
192215
public void deleteNonLatestCustomModels_noFileToDelete()
193216
throws FirebaseMlException, FileNotFoundException {

0 commit comments

Comments
 (0)