If a recording does not exist, show n/a for its size

This commit is contained in:
0xboobface 2019-12-28 16:56:18 +01:00
parent 738aca8a06
commit a165a83dca
2 changed files with 26 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package ctbrec;
import java.io.File;
import java.io.Serializable;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
@ -12,7 +13,7 @@ import ctbrec.event.EventBusHolder;
import ctbrec.event.RecordingStateChangedEvent;
import ctbrec.recorder.download.Download;
public class Recording {
public class Recording implements Serializable {
private Model model;
private transient Download download;
private Instant startDate;
@ -191,11 +192,15 @@ public class Recording {
private long getSize() {
File rec = new File(Config.getInstance().getSettings().recordingsDir, getPath());
if(rec.isDirectory()) {
if (rec.isDirectory()) {
return getDirectorySize(rec);
} else {
if(!rec.exists()) {
return getDirectorySize(rec.getParentFile());
if (!rec.exists()) {
if (rec.getName().endsWith(".m3u8")) {
return getDirectorySize(rec.getParentFile());
} else {
return -1;
}
} else {
return rec.length();
}
@ -204,12 +209,14 @@ public class Recording {
private long getDirectorySize(File dir) {
long size = 0;
File[] files = dir.listFiles();
if (files == null) {
return 0;
}
for (File file : files) {
size += file.length();
if (dir.exists()) {
File[] files = dir.listFiles();
if (files == null) {
return 0;
}
for (File file : files) {
size += file.length();
}
}
return size;
}

View File

@ -3,6 +3,8 @@ package ctbrec;
import java.text.DecimalFormat;
public class StringUtil {
private StringUtil() {}
public static boolean isBlank(String s) {
return s == null || s.trim().isEmpty();
}
@ -12,16 +14,20 @@ public class StringUtil {
}
public static String formatSize(Number sizeInByte) {
if (sizeInByte.longValue() < 0) {
return "n/a";
}
DecimalFormat df = new DecimalFormat("0.00");
String unit = "Bytes";
double size = sizeInByte.doubleValue();
if(size > 1024.0 * 1024 * 1024) {
if (size > 1024.0 * 1024 * 1024) {
size = size / 1024.0 / 1024 / 1024;
unit = "GiB";
} else if(size > 1024.0 * 1024) {
} else if (size > 1024.0 * 1024) {
size = size / 1024.0 / 1024;
unit = "MiB";
} else if(size > 1024.0) {
} else if (size > 1024.0) {
size = size / 1024.0;
unit = "KiB";
}