/
FileResultStorageEngine.java
103 lines (92 loc) · 3.1 KB
/
FileResultStorageEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright 2020, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.async.service.storageengine;
import com.yahoo.elide.async.models.TableExportResult;
import jakarta.inject.Singleton;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;
/**
* Default implementation of ResultStorageEngine that stores results on local filesystem.
* It supports Async Module to store results with Table Export query.
*/
@Singleton
@Slf4j
@Getter
public class FileResultStorageEngine implements ResultStorageEngine {
@Setter private String basePath;
/**
* Constructor.
* @param basePath basePath for storing the files. Can be absolute or relative.
*/
public FileResultStorageEngine(String basePath) {
this.basePath = basePath;
}
@Override
public TableExportResult storeResults(String tableExportID, Consumer<OutputStream> result) {
log.debug("store TableExportResults for Download");
TableExportResult exportResult = new TableExportResult();
try (OutputStream writer = newOutputStream(tableExportID)) {
result.accept(writer);
} catch (IOException e) {
throw new UncheckedIOException(STORE_ERROR, e);
}
return exportResult;
}
@Override
public Consumer<OutputStream> getResultsByID(String tableExportID) {
log.debug("getTableExportResultsByID");
return outputStream -> {
try {
newInputStream(tableExportID).transferTo(outputStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}
/**
* Validates that the path to read/write is as expected to prevent path
* traversal.
*
* @param path the path to read/write
* @throws IOException if the path is not expected
*/
protected void validatePath(Path path) throws IOException {
Path parent = Paths.get(basePath);
if (!path.getParent().equals(parent)) {
throw new FileNotFoundException();
}
}
private InputStream newInputStream(String tableExportID) {
try {
Path path = Paths.get(basePath, tableExportID);
validatePath(path);
return Files.newInputStream(path);
} catch (IOException e) {
log.debug(e.getMessage());
throw new UncheckedIOException(RETRIEVE_ERROR, e);
}
}
private OutputStream newOutputStream(String tableExportID) {
try {
Path path = Paths.get(basePath, tableExportID);
validatePath(path);
return Files.newOutputStream(path);
} catch (IOException e) {
log.debug(e.getMessage());
throw new UncheckedIOException(STORE_ERROR, e);
}
}
}