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

com.uid2.shared.cloud.EmbeddedResourceStorage Maven / Gradle / Ivy

package com.uid2.shared.cloud;

import com.uid2.shared.Utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.stream.Collectors;

public class EmbeddedResourceStorage implements ICloudStorage {
    private final Path tmpDir = Paths.get("/tmp/uid2");
    private final Class cls;
    private final List resourceList;
    private String urlPrefix;

    public EmbeddedResourceStorage(Class cls) {
        Utils.ensureDirectoryExists(tmpDir);
        this.cls = cls;
        this.resourceList = null;
        this.urlPrefix = null;
    }

    public EmbeddedResourceStorage(Class cls, List resourceList) {
        Utils.ensureDirectoryExists(tmpDir);
        this.cls = cls;
        this.resourceList = Collections.unmodifiableList(resourceList);
        this.urlPrefix = null;
    }

    public EmbeddedResourceStorage withUrlPrefix(String urlPrefix) {
        this.urlPrefix = urlPrefix;
        return this;
    }

    @Override
    public void upload(String localPath, String cloudPath) throws CloudStorageException {
        // this is a read-only cloud storage
        throw new UnsupportedOperationException("EmbeddedResourceStorage::upload method is not supported");
    }

    @Override
    public void upload(InputStream input, String cloudPath) throws CloudStorageException {
        // this is a read-only cloud storage
        throw new UnsupportedOperationException("EmbeddedResourceStorage::upload method is not supported");
    }

    @Override
    public InputStream download(String cloudPath) throws CloudStorageException {
        return cls.getResourceAsStream(cloudPath);
    }

    @Override
    public void delete(String cloudPath) throws CloudStorageException {
        // this is a read-only cloud storage
        throw new UnsupportedOperationException("EmbeddedResourceStorage::delete method is not supported");
    }

    @Override
    public void delete(Collection cloudPath) throws CloudStorageException {
        // this is a read-only cloud storage
        throw new UnsupportedOperationException("EmbeddedResourceStorage::delete method is not supported");
    }

    @Override
    public List list(String prefix) throws CloudStorageException {
        if (this.resourceList != null) {
            return this.resourceList.stream()
                    .filter(p -> p.startsWith(prefix))
                    .collect(Collectors.toList());
        }

        File resource = new File(cls.getResource(prefix).getPath());
        if (resource.isDirectory()) {
            return Arrays.stream(resource.listFiles())
                    .map(File::getAbsolutePath)
                    .collect(Collectors.toList());
        }

        if (resource.exists()) {
            List result = new ArrayList<>();
            result.add(resource.getAbsolutePath());
            return result;
        }

        return Collections.emptyList();
    }

    @Override
    public URL preSignUrl(String cloudPath) throws CloudStorageException {
        try {
            // for embedded resource, pre-signing URL is to download the cloud path to a tmp file and return URL for it
            Path tmpFile = Files.createTempFile(this.tmpDir, "pre-signed", ".dat");
            try (InputStream inputStream = download(cloudPath)) {
                Files.copy(inputStream, tmpFile, StandardCopyOption.REPLACE_EXISTING);
            }
            if(this.urlPrefix == null || this.urlPrefix.isEmpty()) {
                return tmpFile.toUri().toURL();
            } else {
                return new URL(this.urlPrefix + tmpFile.toString());
            }
        } catch (IOException e) {
            throw new CloudStorageException("EmbeddedResourceStorage::preSignUrl error: " + e.getMessage(), e);
        }
    }

    @Override
    public void setPreSignedUrlExpiry(long expiry) {
        // no-op
    }

    @Override
    public String mask(String cloudPath) {
        return cloudPath;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy