Skip to content

Commit e34cfef

Browse files
committed
Small enhancement for FileServiceImpl
1 parent 88875a8 commit e34cfef

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

services/src/main/java/io/scalecube/services/files/FileServiceImpl.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,31 @@ public class FileServiceImpl implements FileService, FileStreamer {
2222

2323
private static final Logger LOGGER = LoggerFactory.getLogger(FileServiceImpl.class);
2424

25-
private static final int DEFAULT_MAX_CHUNK_SIZE = 64 * 1024;
26-
private static final String TEMP_DIR = System.getProperty("java.io.tmpdir");
25+
private static final int DEFAULT_CHUNK_SIZE = 64 * 1024;
26+
private static final String DEFAULT_DIR = System.getProperty("java.io.tmpdir");
27+
private static final Duration DEFAULT_TTL = Duration.ofSeconds(600);
2728

2829
private final Path baseDir;
29-
private final int maxChunkSize;
30+
private final int chunkSize;
31+
private final Duration fileTtl;
3032

3133
private String serviceEndpointId;
3234

33-
/**
34-
* Default constructor. {@code baseDir} will be {@code java.io.tmpdir}, {@code maxChunkSize} will
35-
* be {@code 64k}.
36-
*/
3735
public FileServiceImpl() {
38-
this(new File(TEMP_DIR), DEFAULT_MAX_CHUNK_SIZE);
36+
this(new File(DEFAULT_DIR), DEFAULT_CHUNK_SIZE, DEFAULT_TTL);
3937
}
4038

4139
/**
4240
* Constructor.
4341
*
4442
* @param baseDir baseDir for storing files
45-
* @param maxChunkSize maximum buffer size for reading file and publishing by flux
43+
* @param chunkSize maximum buffer size for reading file and publishing by flux
44+
* @param fileTtl file lifetime, {@code null} means forever
4645
*/
47-
public FileServiceImpl(File baseDir, int maxChunkSize) {
46+
public FileServiceImpl(File baseDir, int chunkSize, Duration fileTtl) {
4847
this.baseDir = baseDir.toPath();
49-
this.maxChunkSize = maxChunkSize;
48+
this.chunkSize = chunkSize;
49+
this.fileTtl = fileTtl;
5050
}
5151

5252
@AfterConstruct
@@ -73,15 +73,17 @@ public Mono<String> addFile(AddFileRequest request) {
7373
throw new IllegalArgumentException("Wrong file: " + file);
7474
}
7575

76-
if (ttl != null && ttl != Duration.ZERO) {
76+
final var actualTtl = ttl != null ? ttl : fileTtl;
77+
78+
if (actualTtl != null) {
7779
final var scheduler = Schedulers.single();
7880
scheduler.schedule(
7981
() -> {
8082
if (!file.delete()) {
8183
LOGGER.warn("Cannot delete file: {}", file);
8284
}
8385
},
84-
ttl.toMillis(),
86+
actualTtl.toMillis(),
8587
TimeUnit.MILLISECONDS);
8688
}
8789

@@ -100,7 +102,7 @@ public Flux<byte[]> streamFile() {
100102
if (!isPathValid(filePath)) {
101103
return Flux.error(new FileNotFoundException("File not found: " + name));
102104
} else {
103-
return fluxFrom(filePath, ByteBuffer.allocate(maxChunkSize));
105+
return fluxFrom(filePath, ByteBuffer.allocate(chunkSize));
104106
}
105107
});
106108
}
@@ -134,8 +136,8 @@ private static Flux<byte[]> fluxFrom(Path filePath, ByteBuffer chunkBuffer) {
134136
channel -> {
135137
try {
136138
channel.close();
137-
} catch (Throwable e) {
138-
LOGGER.warn("Cannot close file: {}", filePath);
139+
} catch (Exception e) {
140+
LOGGER.warn("Cannot close file: {}", filePath, e);
139141
}
140142
});
141143
}

0 commit comments

Comments
 (0)