package de.roko.genalogy.downloader;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.*;

public class ImageDownloader {

    public static void main(String[] args) {
        String imageUrl = "https://www.archion.de/typo3conf/ext/archion_sitepackage/Resources/Public/Images/logo_quer_weiss.svg";
        String targetDirectory = System.getProperty("user.home") + "/Pictures";

        try {
            downloadImage(imageUrl, targetDirectory);
        } catch (IOException e) {
            System.err.println("❌ Fehler beim Herunterladen: " + e.getMessage());
        }
    }

    public static void downloadImage(String imageUrl, String targetDirPath) throws IOException {
        URL url = new URL(imageUrl);
        String fileName = Paths.get(url.getPath()).getFileName().toString(); // Datei extrahieren
        Path targetPath = Paths.get(targetDirPath, fileName);

        try (InputStream in = url.openStream()) {
            Files.createDirectories(Paths.get(targetDirPath)); // Sicherstellen, dass Verzeichnis existiert
            Files.copy(in, targetPath, StandardCopyOption.REPLACE_EXISTING);
            System.out.println("✅ Bild gespeichert unter: " + targetPath);
        }
    }
}
