From 1af75a44cc1f831e571518d3439cee3dde8b0395 Mon Sep 17 00:00:00 2001
From: Julian Rother <julianr@fsmpi.rwth-aachen.de>
Date: Mon, 4 Dec 2017 18:38:31 +0100
Subject: [PATCH] Added publish_video

---
 Makefile        |  4 +++-
 publish_video.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 publish_video.c

diff --git a/Makefile b/Makefile
index f8cf32f..b3c8025 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-TARGETS = probe probe-raw remux thumbnail transcode
+TARGETS = probe probe-raw remux thumbnail transcode publish_video
 
 CFLAGS = -I /usr/include/libxml2
 LDFLAGS= -lcurl -lavcodec -lavformat -lavfilter -lswscale -lavutil -lxml2
@@ -16,5 +16,7 @@ thumbnail: thumbnail.c *.h util/*.c
 
 transcode: transcode.c *.h util/*.c
 
+publish_video: publish_video.c *.h util/*.c
+
 clean:
 	rm -f ${TARGETS}
diff --git a/publish_video.c b/publish_video.c
new file mode 100644
index 0000000..b84810a
--- /dev/null
+++ b/publish_video.c
@@ -0,0 +1,28 @@
+#include <unistd.h>
+#include <libgen.h>
+#include <sys/stat.h>
+
+#include "util.h"
+
+int main(int argc, char *argv[])
+{
+	char *src, *dest, *destdir;
+	if (argc != 5)
+		return 1;
+	av_register_all();
+	init_env();
+	init_avlogbuf();
+	jobid = atoi(argv[1]);
+	src = mprintf("%s/%s", getenv(WORKER_TMP), jstr(jlookup(argv[4], "source"), ""));
+	dest = mprintf("%s/%s", getenv(WORKER_RELEASED), jstr(jlookup(argv[4], "path"), ""));
+	destdir = dirname(mprintf("%s/%s", getenv(WORKER_RELEASED), jstr(jlookup(argv[4], "path"), "")));
+	ping_job(jobid, "running", 0);
+	overwrite_check(dest, 0, 0);
+	if (mkdir(destdir, 02775) && errno != EEXIST)
+		job_failed("Could not create target directory \"%s\": %s", destdir, strerror(errno));
+	if (rename(src, dest))
+		job_failed("Overwriting target file failed: %s", strerror(errno));
+	ping_job(jobid, "finished", "{%s, \"log\": \"%s\"}",
+			json_fileinfo(dest), jescape(get_avlogbuf()));
+	return 0;
+}
-- 
GitLab