Skip to content

Commit 9b5b2c4

Browse files
committed
Can now process metadata from URL links in addition to file paths.
1 parent e2f976a commit 9b5b2c4

1 file changed

Lines changed: 46 additions & 16 deletions

File tree

src/HiddenSecrets.java

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
import com.drew.metadata.Directory;
66
import com.drew.metadata.Tag;
77
import com.drew.imaging.ImageMetadataReader;
8-
import java.io.File;
9-
import java.io.FileInputStream;
10-
import java.io.FileNotFoundException;
11-
import java.io.IOException;
128

13-
import java.nio.file.Path;
9+
import java.io.*;
10+
11+
import java.net.URL;
1412
import java.nio.file.Paths;
1513
import java.util.Scanner;
1614

@@ -40,16 +38,48 @@ public static void getHiddenSecrets(File file) {
4038
}
4139
}
4240

43-
public static void main(String[] args) {
44-
System.out.print("File path to JPEG: ");
45-
Scanner scanner = new Scanner(System.in);
46-
Path filePath = Paths.get(scanner.nextLine());
47-
48-
getHiddenSecrets(filePath.toFile());
49-
// Put your code to request a file path,
50-
// read in a string from System.in,
51-
// convert that string into A Path type using Paths class,
52-
// and call the getHiddenSecrets method to get the file's meta-data
53-
// HERE
41+
public static void getHiddenSecretsFromURL(URL url) {
42+
try {
43+
Metadata metadata = ImageMetadataReader.readMetadata(
44+
url.openStream()
45+
);
46+
for (Directory directory : metadata.getDirectories()) {
47+
for (Tag tag : directory.getTags()) {
48+
System.out.format("[%s] - %s = %s%n",
49+
directory.getName(), tag.getTagName(), tag.getDescription());
50+
}
51+
if (directory.hasErrors()) {
52+
for (String error : directory.getErrors()) {
53+
System.err.format("ERROR: %s%n", error);
54+
}
55+
}
56+
}
57+
} catch (ImageProcessingException e) {
58+
System.out.println("Failed to process the image meta-data.");
59+
throw new RuntimeException(e);
60+
} catch (IOException e) {
61+
System.out.println("Problem reading from URL.");
62+
throw new RuntimeException(e);
63+
}
64+
}
65+
66+
public static void main(String[] args) throws IOException {
67+
while (true) {
68+
System.out.print("File path or URL to image: ");
69+
Scanner scanner = new Scanner(System.in);
70+
String location = scanner.nextLine().trim();
71+
72+
// add a lazy exit clause
73+
if (location.equalsIgnoreCase("end") || location.equalsIgnoreCase("exit")) {
74+
return;
75+
}
76+
77+
if (location.startsWith("https://") || location.startsWith("http://")) {
78+
getHiddenSecretsFromURL(new URL(location));
79+
} else {
80+
getHiddenSecrets(Paths.get(location).toFile());
81+
}
82+
System.out.println();
83+
}
5484
}
5585
}

0 commit comments

Comments
 (0)