2024-04-26 15:26:11 +02:00
|
|
|
package org.opencdmp.filetransformer.docx.service.storage;
|
2024-01-31 16:49:49 +01:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.nio.file.StandardOpenOption;
|
2024-03-06 15:34:34 +01:00
|
|
|
import java.util.Locale;
|
2024-01-31 16:49:49 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
@Service
|
2024-03-06 15:34:34 +01:00
|
|
|
public class FileStorageServiceImpl implements FileStorageService {
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(FileStorageServiceImpl.class);
|
2024-01-31 16:49:49 +01:00
|
|
|
|
2024-03-06 15:34:34 +01:00
|
|
|
private final FileStorageServiceProperties properties;
|
2024-01-31 16:49:49 +01:00
|
|
|
|
|
|
|
@Autowired
|
2024-03-06 15:34:34 +01:00
|
|
|
public FileStorageServiceImpl(FileStorageServiceProperties properties) {
|
2024-01-31 16:49:49 +01:00
|
|
|
this.properties = properties;
|
|
|
|
}
|
|
|
|
|
2024-03-06 15:34:34 +01:00
|
|
|
@Override
|
2024-01-31 16:49:49 +01:00
|
|
|
public String storeFile(byte[] data) {
|
|
|
|
try {
|
2024-03-06 15:34:34 +01:00
|
|
|
String fileName = UUID.randomUUID().toString().replace("-", "").toLowerCase(Locale.ROOT);
|
2024-01-31 16:49:49 +01:00
|
|
|
Path storagePath = Paths.get(properties.getTransientPath() + "/" + fileName);
|
|
|
|
Files.write(storagePath, data, StandardOpenOption.CREATE_NEW);
|
|
|
|
return fileName;
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-03-06 15:34:34 +01:00
|
|
|
@Override
|
2024-01-31 16:49:49 +01:00
|
|
|
public byte[] readFile(String fileRef) {
|
|
|
|
try (FileInputStream inputStream = new FileInputStream(properties.getTransientPath() + "/" + fileRef)) {
|
|
|
|
return inputStream.readAllBytes();
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
}
|
2024-03-06 15:34:34 +01:00
|
|
|
return new byte[0];
|
2024-01-31 16:49:49 +01:00
|
|
|
}
|
|
|
|
}
|