UrlsWorker/src/main/java/eu/openaire/urls_worker/services/FileStorageService.java

100 lines
3.7 KiB
Java

package eu.openaire.urls_worker.services;
import eu.openaire.urls_worker.exceptions.FileStorageException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import java.io.*;
import java.nio.file.*;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
@Service
public class FileStorageService {
private static final Logger logger = LoggerFactory.getLogger(FileStorageService.class);
public static Path assignmentsLocation = null;
static {
String springPropertiesFile = System.getProperty("user.dir") + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "application.properties";
FileReader fReader = null;
try {
fReader = new FileReader(springPropertiesFile);
Properties props = new Properties();
props.load(fReader); // Load jdbc related properties.
String assignmentsDir = props.getProperty("file.assignments-dir");
assignmentsLocation = Paths.get(assignmentsDir).toAbsolutePath().normalize();
} catch (java.io.FileNotFoundException fnfe) {
logger.error("The properties file was not found!", fnfe);
System.exit(-10);
} catch (IOException ioe) {
logger.error("I/O error when reading the properties file!", ioe);
System.exit(-10);
}
}
@Autowired
public FileStorageService() throws FileStorageException {
try {
Files.createDirectories(assignmentsLocation);
} catch (Exception ex) {
throw new FileStorageException("Could not create the directory where the uploaded files will be stored.", ex);
}
}
private static final int bufferSize = 10485760; // 10 MB
public StreamingResponseBody loadFileAsAStream(String fullFileName)
{
StreamingResponseBody streamingResponseBody = null;
AtomicReference<FileInputStream> fileInputStream = new AtomicReference<>();
try {
streamingResponseBody = response -> { // This does not need to be explicitly closed.
fileInputStream.set(new FileInputStream(fullFileName));
int bytesRead;
byte[] byteBuffer = new byte[bufferSize];
FileInputStream fInS = fileInputStream.get();
while ( (bytesRead = fInS.read(byteBuffer, 0, bufferSize)) > 0 )
response.write(byteBuffer, 0, bytesRead);
};
return streamingResponseBody;
} catch (Exception e) {
if ( e instanceof FileNotFoundException )
logger.error("File \"" + fullFileName + "\" is not a file!");
else
logger.error(e.getMessage(), e);
return null;
} finally {
FileInputStream fInS = fileInputStream.get();
if ( fInS != null ) {
try {
fInS.close();
} catch (IOException ex) {
logger.error("", ex);
}
}
}
}
public Resource loadFileAsResource(String fullFileName) {
try {
Path filePath = assignmentsLocation.resolve(fullFileName).normalize();
Resource resource = new UrlResource(filePath.toUri());
return resource.exists() ? resource : null;
} catch (Exception e) {
logger.error("Error when loading file: " + fullFileName, e);
return null;
}
}
}