[go: nahoru, domu]

Skip to content

Commit

Permalink
Adding file path check to avoid NumberFormatException due to empty fi…
Browse files Browse the repository at this point in the history
…le path

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.
  • Loading branch information
kabeer-uber committed Jul 1, 2022
1 parent 7d64cde commit fab9a2a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,13 @@ public static ModelFileManager getInstance(@NonNull FirebaseApp app) {
void deleteNonLatestCustomModels() throws FirebaseMlException {
File root = getDirImpl("");

boolean ret = true;
if (root.isDirectory()) {
for (File f : root.listFiles()) {
// for each custom model sub directory - extract customModelName and clean up old models.
String modelName = f.getName();

CustomModel model = sharedPreferencesUtil.getCustomModelDetails(modelName);
if (model != null) {
if (model != null && model.getLocalFilePath() != null && !model.getLocalFilePath().isEmpty()) {
deleteOldModels(modelName, model.getLocalFilePath());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,29 @@ public void deleteNonLatestCustomModels_fileToDelete()
assertTrue(new File(modelDestinationFolder + "/1").exists());
}

@Test
public void deleteNonLatestCustomModels_whenModelOnDiskButNotInPreferences()
throws FirebaseMlException, IOException {
String modelDestinationFolder2 = setUpTestingFiles(app, MODEL_NAME_2);

//Was just downloaded
MoveFileToDestination(modelDestinationFolder, testModelFile, CUSTOM_MODEL_NO_FILE, 0);
//Was downloaded previously via FirebaseModelManager
MoveFileToDestination(modelDestinationFolder2, testModelFile2, CUSTOM_MODEL_NO_FILE_2, 0);

sharedPreferencesUtil.setLoadedCustomModelDetails(
new CustomModel(MODEL_NAME, MODEL_HASH, 100, 0, modelDestinationFolder + "/0"));

//Download in progress, hence file path is not present
sharedPreferencesUtil.setLoadedCustomModelDetails(
new CustomModel(MODEL_NAME_2, MODEL_HASH_2, 100, 0));

fileManager.deleteNonLatestCustomModels();

assertTrue(new File(modelDestinationFolder + "/0").exists());
assertTrue(new File(modelDestinationFolder2 + "/0").exists());
}

@Test
public void deleteNonLatestCustomModels_noFileToDelete()
throws FirebaseMlException, FileNotFoundException {
Expand Down

0 comments on commit fab9a2a

Please sign in to comment.