Creating your own temporary storage
public interface TemporaryFileStorageService {
public String getPresignedURL(String filename, Duration accessTime);
public void saveFile(String fileName, byte[] byteArray);
}@Service
@ConditionalOnMissingBean(TemporaryFileStorageService.class)
@Slf4j
public class LocalFileStorageService implements TemporaryFileStorageService {
@Value("${file.storage.path:/tmp}")
String path = "/tmp";
@Override
public String getPresignedURL(String filename, Duration accessTime) {
Timer timer = new Timer("Delete saved file: " + filename);//create a new Timer
timer.schedule(new TimerTask() {
@Override
public void run() {
try {
Files.delete(Paths.get(path + File.separator + filename));
} catch (IOException e) {
log.error("Unable to delete temp file: " + e.getMessage());
}
}
}, accessTime.getSeconds());
return "file://" + path + "/" + filename;
}
@Override
public void saveFile(String fileName, byte[] byteArray) {
Path filePath = Paths.get(path + File.separator + fileName); // Path to the output file
try {
Files.write(filePath, byteArray);
} catch (IOException e) {
log.error("Error writing byte array to file: " + e.getMessage());
}
}
}Last updated
Was this helpful?
