Skip to content

Commit

Permalink
Handle content type errors explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
huangsam committed Dec 8, 2024
1 parent ed66769 commit dd2eab0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void migratePhotos(@NotNull Collection<Photo> photos) {
try {
String folderId = createDriveFolder(targetRoot, targetPath);
createDrivePhoto(folderId, photo);
} catch (IOException | NullPointerException e) {
} catch (IOException e) {
LOG.error("Cannot move {}: {}", photo.name(), e.getMessage());
failureCount++;
}
Expand Down Expand Up @@ -105,6 +105,11 @@ private void createDrivePhoto(String folderId, @NotNull Photo photo) throws IOEx
photoMetadata.setParents(List.of(folderId));

String contentType = Files.probeContentType(photo.path());
if (contentType == null) {
failureCount++;
return;
}

java.io.File photoFile = new java.io.File(photo.path().toString());
FileContent photoContent = new FileContent(contentType, photoFile);

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/huangsam/photohaul/traversal/PathRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public static Predicate<Path> validExtensions() {
public static Predicate<Path> isImageContent() {
return path -> {
try {
return Files.probeContentType(path).startsWith("image/");
} catch (IOException | NullPointerException e) {
String contentType = Files.probeContentType(path);
return contentType != null && contentType.startsWith("image/");
} catch (IOException e) {
return false;
}
};
Expand Down

0 comments on commit dd2eab0

Please sign in to comment.