forked from j62/ctbrec
1
0
Fork 0

Add methods to get the free and total space

This commit is contained in:
0xboobface 2018-11-26 13:49:42 +01:00
parent 91ea7d65a3
commit 8fdb24bad1
4 changed files with 74 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import static ctbrec.Recording.STATUS.*;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@ -745,4 +746,20 @@ public class LocalRecorder implements Recorder {
public HttpClient getHttpClient() {
return client;
}
@Override
public long getTotalSpaceBytes() throws IOException {
return getRecordingsFileStore().getTotalSpace();
}
@Override
public long getFreeSpaceBytes() throws IOException {
return getRecordingsFileStore().getUsableSpace();
}
private FileStore getRecordingsFileStore() throws IOException {
File recordingsDir = new File(Config.getInstance().getSettings().recordingsDir);
FileStore store = Files.getFileStore(recordingsDir.toPath());
return store;
}
}

View File

@ -42,4 +42,18 @@ public interface Recorder {
public List<Model> getOnlineModels();
public HttpClient getHttpClient();
/**
* Get the total size of the filesystem we are recording to
* @return the total size in bytes
* @throws IOException
*/
public long getTotalSpaceBytes() throws IOException;
/**
* Get the free space left on the filesystem we are recording to
* @return the free space in bytes
* @throws IOException
*/
public long getFreeSpaceBytes() throws IOException;
}

View File

@ -8,6 +8,7 @@ import java.time.Instant;
import java.util.Collections;
import java.util.List;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -45,6 +46,8 @@ public class RemoteRecorder implements Recorder {
private List<Model> models = Collections.emptyList();
private List<Model> onlineModels = Collections.emptyList();
private List<Site> sites;
private long spaceTotal = -1;
private long spaceFree = -1;
private Config config;
private HttpClient client;
@ -150,10 +153,35 @@ public class RemoteRecorder implements Recorder {
while(running) {
syncModels();
syncOnlineModels();
syncSpace();
sleep();
}
}
private void syncSpace() {
try {
String msg = "{\"action\": \"space\"}";
RequestBody body = RequestBody.create(JSON, msg);
Request.Builder builder = new Request.Builder()
.url("http://" + config.getSettings().httpServer + ":" + config.getSettings().httpPort + "/rec")
.post(body);
addHmacIfNeeded(msg, builder);
Request request = builder.build();
try(Response response = client.execute(request)) {
String json = response.body().string();
if(response.isSuccessful()) {
JSONObject resp = new JSONObject(json);
spaceTotal = resp.getLong("spaceTotal");
spaceFree = resp.getLong("spaceFree");
} else {
LOG.error("Couldn't synchronize with server. HTTP status: {} - {}", response.code(), json);
}
}
} catch (IOException | InvalidKeyException | NoSuchAlgorithmException | IllegalStateException e) {
LOG.error("Couldn't synchronize with server", e);
}
}
private void syncModels() {
try {
String msg = "{\"action\": \"list\"}";
@ -362,4 +390,14 @@ public class RemoteRecorder implements Recorder {
public HttpClient getHttpClient() {
return client;
}
@Override
public long getTotalSpaceBytes() throws IOException {
return spaceTotal;
}
@Override
public long getFreeSpaceBytes() {
return spaceFree;
}
}

View File

@ -137,9 +137,13 @@ public class RecorderServlet extends AbstractCtbrecServlet {
response = "{\"status\": \"success\", \"msg\": \"Recording resumed\"}";
resp.getWriter().write(response);
break;
case "space":
response = "{\"status\": \"success\", \"spaceTotal\": "+recorder.getTotalSpaceBytes()+", \"spaceFree\": "+recorder.getFreeSpaceBytes()+"}";
resp.getWriter().write(response);
break;
default:
resp.setStatus(SC_BAD_REQUEST);
response = "{\"status\": \"error\", \"msg\": \"Unknown action\"}";
response = "{\"status\": \"error\", \"msg\": \"Unknown action ["+request.action+"]\"}";
resp.getWriter().write(response);
break;
}