forked from j62/ctbrec
Ignore recordings where the actual recording is missing instead of deleting the metadata
This commit is contained in:
parent
e6e8d22631
commit
737d1bbb55
|
@ -1,28 +1,7 @@
|
||||||
package ctbrec.recorder;
|
package ctbrec.recorder;
|
||||||
|
|
||||||
import static ctbrec.Recording.State.*;
|
|
||||||
import static ctbrec.io.IoUtils.*;
|
|
||||||
import static java.nio.charset.StandardCharsets.*;
|
|
||||||
import static java.nio.file.StandardOpenOption.*;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.NoSuchElementException;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import com.squareup.moshi.JsonAdapter;
|
import com.squareup.moshi.JsonAdapter;
|
||||||
import com.squareup.moshi.Moshi;
|
import com.squareup.moshi.Moshi;
|
||||||
|
|
||||||
import ctbrec.Config;
|
import ctbrec.Config;
|
||||||
import ctbrec.Model;
|
import ctbrec.Model;
|
||||||
import ctbrec.Recording;
|
import ctbrec.Recording;
|
||||||
|
@ -31,19 +10,33 @@ import ctbrec.io.FileJsonAdapter;
|
||||||
import ctbrec.io.InstantJsonAdapter;
|
import ctbrec.io.InstantJsonAdapter;
|
||||||
import ctbrec.io.ModelJsonAdapter;
|
import ctbrec.io.ModelJsonAdapter;
|
||||||
import ctbrec.sites.Site;
|
import ctbrec.sites.Site;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
import static ctbrec.Recording.State.*;
|
||||||
|
import static ctbrec.io.IoUtils.deleteDirectory;
|
||||||
|
import static ctbrec.io.IoUtils.deleteEmptyParents;
|
||||||
|
import static java.nio.file.StandardOpenOption.*;
|
||||||
|
|
||||||
public class RecordingManager {
|
public class RecordingManager {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(RecordingManager.class);
|
private static final Logger LOG = LoggerFactory.getLogger(RecordingManager.class);
|
||||||
|
|
||||||
private Config config;
|
private final Config config;
|
||||||
private Moshi moshi;
|
private final JsonAdapter<Recording> adapter;
|
||||||
private JsonAdapter<Recording> adapter;
|
private final List<Recording> recordings = new ArrayList<>();
|
||||||
private List<Recording> recordings = new ArrayList<>();
|
private final ReentrantLock recordingsLock = new ReentrantLock();
|
||||||
private ReentrantLock recordingsLock = new ReentrantLock();
|
|
||||||
|
|
||||||
public RecordingManager(Config config, List<Site> sites) throws IOException {
|
public RecordingManager(Config config, List<Site> sites) throws IOException {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
moshi = new Moshi.Builder()
|
Moshi moshi = new Moshi.Builder()
|
||||||
.add(Model.class, new ModelJsonAdapter(sites))
|
.add(Model.class, new ModelJsonAdapter(sites))
|
||||||
.add(Instant.class, new InstantJsonAdapter())
|
.add(Instant.class, new InstantJsonAdapter())
|
||||||
.add(File.class, new FileJsonAdapter())
|
.add(File.class, new FileJsonAdapter())
|
||||||
|
@ -73,7 +66,7 @@ public class RecordingManager {
|
||||||
String json = adapter.toJson(rec);
|
String json = adapter.toJson(rec);
|
||||||
rec.setMetaDataFile(recordingMetaData.getAbsolutePath());
|
rec.setMetaDataFile(recordingMetaData.getAbsolutePath());
|
||||||
Files.createDirectories(recordingMetaData.getParentFile().toPath());
|
Files.createDirectories(recordingMetaData.getParentFile().toPath());
|
||||||
Files.write(recordingMetaData.toPath(), json.getBytes(UTF_8), CREATE, WRITE, TRUNCATE_EXISTING);
|
Files.writeString(recordingMetaData.toPath(), json, CREATE, WRITE, TRUNCATE_EXISTING);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,29 +75,32 @@ public class RecordingManager {
|
||||||
File[] metaFiles = recordingsMetaDir.listFiles((file, name) -> name.endsWith(".json"));
|
File[] metaFiles = recordingsMetaDir.listFiles((file, name) -> name.endsWith(".json"));
|
||||||
if (metaFiles != null) {
|
if (metaFiles != null) {
|
||||||
for (File file : metaFiles) {
|
for (File file : metaFiles) {
|
||||||
String json = new String(Files.readAllBytes(file.toPath()), UTF_8);
|
String json = Files.readString(file.toPath());
|
||||||
try {
|
try {
|
||||||
Recording recording = adapter.fromJson(json);
|
Recording recording = adapter.fromJson(json);
|
||||||
if (recording.getStatus() == RECORDING || recording.getStatus() == GENERATING_PLAYLIST || recording.getStatus() == POST_PROCESSING) {
|
loadRecording(recording);
|
||||||
recording.setStatus(WAITING);
|
} catch (Exception e) {
|
||||||
}
|
|
||||||
if (recording.getId() == null) {
|
|
||||||
recording.setId(UUID.randomUUID().toString());
|
|
||||||
saveRecording(recording);
|
|
||||||
}
|
|
||||||
if (recordingExists(recording)) {
|
|
||||||
recordings.add(recording);
|
|
||||||
} else {
|
|
||||||
LOG.info("Recording {} does not exist anymore -> deleting meta data", recording);
|
|
||||||
Files.deleteIfExists(file.toPath());
|
|
||||||
}
|
|
||||||
} catch(Exception e) {
|
|
||||||
LOG.error("Couldn't load recording {}", file, e);
|
LOG.error("Couldn't load recording {}", file, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void loadRecording(Recording recording) throws IOException {
|
||||||
|
if (recording.getStatus() == RECORDING || recording.getStatus() == GENERATING_PLAYLIST || recording.getStatus() == POST_PROCESSING) {
|
||||||
|
recording.setStatus(WAITING);
|
||||||
|
}
|
||||||
|
if (recording.getId() == null) {
|
||||||
|
recording.setId(UUID.randomUUID().toString());
|
||||||
|
saveRecording(recording);
|
||||||
|
}
|
||||||
|
if (recordingExists(recording)) {
|
||||||
|
recordings.add(recording);
|
||||||
|
} else {
|
||||||
|
LOG.info("Recording {} does not exist anymore -> ignoring recording", recording);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private boolean recordingExists(Recording recording) {
|
private boolean recordingExists(Recording recording) {
|
||||||
return recording.getAbsoluteFile().exists();
|
return recording.getAbsoluteFile().exists();
|
||||||
}
|
}
|
||||||
|
@ -117,8 +113,9 @@ public class RecordingManager {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes a recording from disk and removes it from ctbrec
|
* Deletes a recording from disk and removes it from ctbrec
|
||||||
* @param recording
|
*
|
||||||
* @throws IOException
|
* @param recording Recording to delete
|
||||||
|
* @throws IOException if the deletion of the recording or of any empty parent directories fails
|
||||||
*/
|
*/
|
||||||
public void delete(Recording recording) throws IOException {
|
public void delete(Recording recording) throws IOException {
|
||||||
if (recording.isPinned()) {
|
if (recording.isPinned()) {
|
||||||
|
@ -174,16 +171,15 @@ public class RecordingManager {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the recording from ctbrec, but does not touch the actual file
|
* Removes the recording from ctbrec, but does not touch the actual file
|
||||||
* @param recording
|
*
|
||||||
* @throws IOException
|
* @param recording Recording to delete
|
||||||
|
* @throws IOException if the deletion of the metadata or of any empty parent directories fails
|
||||||
*/
|
*/
|
||||||
public void remove(Recording recording) throws IOException {
|
public void remove(Recording recording) throws IOException {
|
||||||
recordingsLock.lock();
|
recordingsLock.lock();
|
||||||
try {
|
try {
|
||||||
int idx = recordings.indexOf(recording);
|
int idx = recordings.indexOf(recording);
|
||||||
recording = recordings.get(idx);
|
recording = recordings.get(idx);
|
||||||
File path = recording.getAbsoluteFile();
|
|
||||||
deleteEmptyParents(path.getParentFile());
|
|
||||||
// delete the meta data
|
// delete the meta data
|
||||||
Files.deleteIfExists(new File(recording.getMetaDataFile()).toPath());
|
Files.deleteIfExists(new File(recording.getMetaDataFile()).toPath());
|
||||||
// remove from data structure
|
// remove from data structure
|
||||||
|
@ -244,7 +240,7 @@ public class RecordingManager {
|
||||||
|
|
||||||
private Recording getRecording(Recording rec) {
|
private Recording getRecording(Recording rec) {
|
||||||
for (Recording r : getAll()) {
|
for (Recording r : getAll()) {
|
||||||
if(Objects.equals(r, rec)) {
|
if (Objects.equals(r, rec)) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue