All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.ciechanowiec.sling.rocket.asset.FileMetadata Maven / Gradle / Ivy

There is a newer version: 13.32.0
Show newest version
package eu.ciechanowiec.sling.rocket.asset;

import eu.ciechanowiec.sling.rocket.jcr.NodeProperties;
import eu.ciechanowiec.sneakyfun.SneakySupplier;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.tika.Tika;

import java.io.File;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

/**
 * In-memory {@link AssetMetadata} generated for a {@link File}. The mime type is detected automatically.
 */
@Slf4j
@ToString
public class FileMetadata implements AssetMetadata {

    @ToString.Exclude
    private final Supplier mimeTypeSupplier;
    @ToString.Exclude
    private final Supplier> allSupplier;
    @ToString.Exclude
    private final Supplier> propertiesSupplier;

    /**
     * Constructs an instance of this class.
     * @param file {@link File} for which this {@link FileMetadata} will be generated
     */
    @SuppressWarnings("WeakerAccess")
    public FileMetadata(File file) {
        this(() -> file);
    }

    /**
     * Constructs an instance of this class.
     * @param fileSupplier {@link Supplier} that will provide a {@link File} for which
     *                     this {@link FileMetadata} will be generated
     */
    @SuppressWarnings("WeakerAccess")
    public FileMetadata(Supplier fileSupplier) {
        mimeTypeSupplier = SneakySupplier.sneaky(() -> {
            File file = fileSupplier.get();
            Tika tika = new Tika();
            log.trace("Detecting the mime type of {}", file);
            String detectedMimeType = tika.detect(file);
            log.trace("Mime type for {} detected: {}", file, detectedMimeType);
            return detectedMimeType;
        });
        allSupplier = () -> Map.of(PN_MIME_TYPE, mimeType());
        propertiesSupplier = Optional::empty;
        log.trace("Initialized {}", this);
    }

    @Override
    public String mimeType() {
        return mimeTypeSupplier.get();
    }

    @Override
    public Map all() {
        return allSupplier.get();
    }

    @Override
    public Optional properties() {
        return propertiesSupplier.get();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy