Skip to content

Commit

Permalink
Create an 'images' folder and save processed images inside it
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus6n committed Nov 11, 2024
1 parent 3d9e8bf commit 32dfbdc
Showing 1 changed file with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
public class ThumbcacheParser extends AbstractParser {

private static final long serialVersionUID = 1L;
private static final String OUTPUT_DIR = "output";

@Override
public Set<MediaType> getSupportedTypes(ParseContext context) {
Expand All @@ -35,8 +34,16 @@ public void parse(InputStream stream, ContentHandler handler, Metadata metadata,
TikaInputStream tis = TikaInputStream.get(stream, tmp)) {

File file = tis.getFile();
String outputDir = file.getParent();
String imagesDir = outputDir + File.separator + "images";

File imagesDirectory = new File(imagesDir);
if (!imagesDirectory.exists()) {
imagesDirectory.mkdirs();
}

try (FileInputStream fis = new FileInputStream(file)) {
parseThumbcacheFile(fis, xhtml);
parseThumbcacheFile(fis, xhtml, imagesDir);
}

} catch (IOException e) {
Expand All @@ -46,7 +53,7 @@ public void parse(InputStream stream, ContentHandler handler, Metadata metadata,
}
}

private void parseThumbcacheFile(InputStream stream, XHTMLContentHandler xhtml) throws IOException, SAXException {
private void parseThumbcacheFile(InputStream stream, XHTMLContentHandler xhtml, String imagesDir) throws IOException, SAXException {
byte[] buffer = new byte[56];

ByteBuffer fileHeader = ByteBuffer.allocate(24).order(ByteOrder.LITTLE_ENDIAN);
Expand Down Expand Up @@ -94,12 +101,6 @@ private void parseThumbcacheFile(InputStream stream, XHTMLContentHandler xhtml)
xhtml.characters("Seen on Windows version : " + windowsVersion + "\n");
xhtml.endElement("pre");

// Criação do diretório de saída para imagens
File dir = new File(OUTPUT_DIR);
if (!dir.exists()) {
dir.mkdirs();
}

while (stream.read(buffer) == buffer.length) {
ByteBuffer bb = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN);

Expand Down Expand Up @@ -143,12 +144,11 @@ private void parseThumbcacheFile(InputStream stream, XHTMLContentHandler xhtml)
String ext = detectImageExtension(imageData);
String fileName = "thumb_" + Long.toHexString(entryHash) + "." + ext;

saveImage(fileName, imageData);
saveImage(imagesDir, fileName, imageData);
}
}
}

// Método para detectar a extensão da imagem com base nos primeiros bytes
private String detectImageExtension(byte[] data) {
if (data.length >= 4) {
if (data[0] == (byte) 0x42 && data[1] == (byte) 0x4D) {
Expand All @@ -162,9 +162,8 @@ private String detectImageExtension(byte[] data) {
return "img";
}

// Método para salvar a imagem em um arquivo
private void saveImage(String fileName, byte[] imageData) {
try (FileOutputStream fos = new FileOutputStream(OUTPUT_DIR + File.separator + fileName)) {
private void saveImage(String imagesDir, String fileName, byte[] imageData) {
try (FileOutputStream fos = new FileOutputStream(imagesDir + File.separator + fileName)) {
fos.write(imageData);
System.out.println("Saved image: " + fileName);
} catch (IOException e) {
Expand Down

0 comments on commit 32dfbdc

Please sign in to comment.