forked from electerm/electerm-sync-server-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileStore.java
51 lines (41 loc) · 1.52 KB
/
FileStore.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
package ElectermSync;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileStore {
public static void main(String[] args) {
// Test code
}
public static Path getFilePath (String userId, Config dotenv) {
String storePath = dotenv.getValue("FILE_STORE_PATH");
Path folder = storePath != null ? Path.of(storePath) : Path.of(System.getProperty("user.dir"));
return folder.resolve(userId + ".json");
}
public static WriteResult write(String jsonBody, String userId, Config dotenv) {
Path filePath = getFilePath(userId, dotenv);
try {
Files.writeString(filePath, jsonBody);
} catch (IOException e) {
return new WriteResult("Error writing file", 500);
}
return new WriteResult("ok", 200);
}
public static ReadResult read(String userId, Config dotenv) {
ObjectMapper objectMapper = new ObjectMapper();
Path filePath = getFilePath(userId, dotenv);
File file = filePath.toFile();
if (file.isFile()) {
String fileContent;
try {
fileContent = Files.readString(filePath).toString();
return new ReadResult(fileContent, 200);
} catch (IOException e) {
return new ReadResult("File read error", 500);
}
} else {
return new ReadResult("File not found", 404);
}
}
}