4 Commits

Author SHA1 Message Date
b049581632 Updated version info to v0.2.0
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2022-05-04 00:43:46 -04:00
5bde5bfee0 Made the transcoder run as a periodic task, with the interval defined in the config TOML file
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2022-05-04 00:40:25 -04:00
347211d566 Cleaned up the IOUtilsTest class a bit
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-05-03 00:17:50 -04:00
204a5e15f1 Added some tests for the IOUtil class
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-05-03 00:13:34 -04:00
8 changed files with 122 additions and 11 deletions

View File

@ -1,4 +1,9 @@
pipeline:
test:
image: maven:3-jdk-11
commands:
- mvn test
build:
image: maven:3-jdk-11
commands:

View File

@ -6,7 +6,7 @@
<groupId>tech.bitgoblin</groupId>
<artifactId>Dragoon</artifactId>
<version>0.1.1</version>
<version>0.2.0</version>
<name>Dragoon</name>
<url>https://www.bitgoblin.tech</url>

View File

@ -1,7 +1,10 @@
package tech.bitgoblin;
import tech.bitgoblin.config.Config;
import tech.bitgoblin.video.Transcoder;
import tech.bitgoblin.transcoder.RunTranscoderTask;
import tech.bitgoblin.transcoder.Transcoder;
import java.util.Timer;
/**
* The Bit Goblin video transcoder service.
@ -16,7 +19,8 @@ public class App {
Config c = new Config(configFile);
// create new Transcoder object and start the service
Transcoder t = new Transcoder(c);
t.transcode();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new RunTranscoderTask(t), 2500, 120 * 1000);
}
}

View File

@ -4,6 +4,8 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import org.tomlj.Toml;
import org.tomlj.TomlParseResult;
import tech.bitgoblin.io.IOUtils;
@ -28,6 +30,10 @@ public class Config {
return this.result.getString(key);
}
public int getInt(String key) {
return Objects.requireNonNull(this.result.getDouble(key)).intValue();
}
public boolean contains(String key) {
return this.result.contains(key);
}

View File

@ -14,9 +14,9 @@ public class IOUtils {
}
public static String resolveTilda(String path) {
if (path.startsWith("~" + File.separator)) {
if (path.startsWith("~" + File.separator) || path.equals("~")) {
path = System.getProperty("user.home") + path.substring(1);
} else if (path.startsWith("~")) {
} else if ((!path.equals("~")) && path.startsWith("~")) {
// here you can implement reading homedir of other users if you care
throw new UnsupportedOperationException("Home dir expansion not implemented for explicit usernames");
}

View File

@ -0,0 +1,23 @@
package tech.bitgoblin.transcoder;
import java.util.TimerTask;
public class RunTranscoderTask extends TimerTask {
private Transcoder transcoder;
public RunTranscoderTask(Transcoder t) {
this.transcoder = t;
}
@Override
public void run() {
// archive the files
transcoder.archive();
// run the transcoder
transcoder.transcode();
// clean up ingest
transcoder.cleanup();
}
}

View File

@ -1,16 +1,14 @@
package tech.bitgoblin.video;
package tech.bitgoblin.transcoder;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.InterruptedException;
import java.lang.Process;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;
import java.util.concurrent.Executors;
import java.nio.file.StandardCopyOption;
import java.util.Timer;
import tech.bitgoblin.config.Config;
import tech.bitgoblin.io.IOUtils;
@ -31,6 +29,12 @@ public class Transcoder {
this.initDirectory();
}
// create a periodic timer task and start it
public void start() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new RunTranscoderTask(this), 10000, this.config.getInt("transcoder.interval") * 60 * 1000);
}
// transcode files in the working directory
public void transcode() {
// search for files
@ -38,6 +42,11 @@ public class Transcoder {
File repo = new File(Paths.get(this.repo_dir, "ingest").toString());
File[] sourceFiles = repo.listFiles();
if (sourceFiles.length == 0) {
System.out.println("There is nothing to transcode in " + this.repo_dir + "/ingest.");
return;
}
// transcode
System.out.println("Transcoding files in " + this.repo_dir + "/ingest...");
for (File f : sourceFiles) {
@ -74,6 +83,34 @@ public class Transcoder {
System.out.println();
}
// copies sources files to the archive directory
public void archive() {
File repo = new File(Paths.get(this.repo_dir, "ingest").toString());
File[] sourceFiles = repo.listFiles();
for (File f : sourceFiles) {
Path filePath = Path.of(f.toString());
String filename = filePath.getFileName().toString();
String archivePath = Paths.get(this.repo_dir, "archive", filename).toString();
try {
Files.copy(filePath, Paths.get(archivePath), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
// clean up the ingest directory once we're done
public void cleanup() {
File repo = new File(Paths.get(this.repo_dir, "ingest").toString());
File[] sourceFiles = repo.listFiles();
for (File f : sourceFiles) {
f.delete();
}
}
// ensures the transcoder's working directory is available
private void initDirectory() {
// create transcoder directory

View File

@ -0,0 +1,36 @@
package tech.bitgoblin.io;
import static org.junit.Assert.assertTrue;
import org.junit.AfterClass;
import org.junit.Test;
import java.io.File;
public class IOUtilsTest {
private static String testDir = "test-temp";
@Test
public void shouldCreateDirectory() {
IOUtils.createDirectory(testDir);
assertTrue(new File(testDir).exists());
}
@Test
public void shouldExpandTilda() {
String homeExpanded = IOUtils.resolveTilda("~");
assertTrue(!homeExpanded.equals("~"));
}
@Test(expected=UnsupportedOperationException.class)
public void shouldFailExpandExplicitTilda() {
IOUtils.resolveTilda("~test");
}
@AfterClass
public static void cleanup() {
new File(testDir).delete();
}
}