Skip to content

Commit 38f23c3

Browse files
authored
Merge pull request #7686 from ant-media/feature/vod-metadata-rest
VOD metadata can now be added via rest when uploading vod file manually.
2 parents 969cdb7 + 35e6728 commit 38f23c3

File tree

5 files changed

+97
-79
lines changed

5 files changed

+97
-79
lines changed

src/main/java/io/antmedia/rest/RestServiceBase.java

Lines changed: 52 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,98 +1251,76 @@ protected String getStreamsDirectory(String appScopeName) {
12511251
return String.format("%s/webapps/%s/%s", System.getProperty("red5.root"), appScopeName, "streams");
12521252
}
12531253

1254-
protected Result uploadVoDFile(String fileName, InputStream inputStream) {
1255-
boolean success = false;
1256-
String message = "";
1257-
String id= null;
1254+
public Result uploadVoDFile(String fileName, InputStream inputStream) {
1255+
return uploadVoDFile(fileName, inputStream, null);
1256+
}
1257+
1258+
public Result uploadVoDFile(String fileName, InputStream inputStream, String metadata) {
1259+
String id = null;
12581260
String appScopeName = getScope().getName();
12591261
String fileExtension = FilenameUtils.getExtension(fileName);
1260-
try {
1261-
1262-
String[] supportedFormats = new String[] {"mp4", "webm", "mov", "avi", "mp3", "wmv"};
1263-
1264-
if (ArrayUtils.contains(supportedFormats, fileExtension)) {
1265-
1266-
1267-
IStatsCollector statsCollector = (IStatsCollector) getAppContext().getBean(IStatsCollector.BEAN_NAME);
1268-
String vodUploadFinishScript = getAppSettings().getVodUploadFinishScript();
1269-
if (StringUtils.isNotBlank(vodUploadFinishScript) && !statsCollector.enoughResource())
1270-
{
1271-
logger.info("Not enough resource to upload VoD file");
1272-
message = "Not enough system resources available to upload and process VoD File";
1273-
}
1274-
else
1275-
{
1276-
1277-
File streamsDirectory = new File(
1278-
getStreamsDirectory(appScopeName));
1279-
1280-
// if the directory does not exist, create it
1281-
if (!streamsDirectory.exists()) {
1282-
streamsDirectory.mkdirs();
1283-
}
1284-
String vodId = RandomStringUtils.secure().nextNumeric(24);
1285-
1286-
1287-
File savedFile = new File(streamsDirectory, vodId + "." + fileExtension);
1288-
1289-
if (!savedFile.toPath().normalize().startsWith(streamsDirectory.toPath().normalize())) {
1290-
throw new IOException("Entry is outside of the target directory");
1291-
}
1292-
1293-
int read = 0;
1294-
byte[] bytes = new byte[2048];
1295-
try (OutputStream outpuStream = new FileOutputStream(savedFile))
1296-
{
1297-
1298-
while ((read = inputStream.read(bytes)) != -1) {
1299-
outpuStream.write(bytes, 0, read);
1300-
}
1301-
outpuStream.flush();
1262+
1263+
String[] supportedFormats = new String[] {"mp4", "webm", "mov", "avi", "mp3", "wmv"};
1264+
if (!ArrayUtils.contains(supportedFormats, fileExtension)) {
1265+
//this message has been used in the frontend(webpanel) pay attention
1266+
return new Result(false, null, "notSupportedFileType");
1267+
}
13021268

1303-
long fileSize = savedFile.length();
1304-
long unixTime = System.currentTimeMillis();
1269+
IStatsCollector statsCollector = (IStatsCollector) getAppContext().getBean(IStatsCollector.BEAN_NAME);
1270+
String vodUploadFinishScript = getAppSettings().getVodUploadFinishScript();
1271+
if (StringUtils.isNotBlank(vodUploadFinishScript) && !statsCollector.enoughResource()) {
1272+
logger.info("Not enough resource to upload VoD file");
1273+
return new Result(false, null, "Not enough system resources available to upload and process VoD File");
1274+
}
13051275

1306-
String path = savedFile.getPath();
1276+
try {
1277+
File streamsDirectory = new File(getStreamsDirectory(appScopeName));
1278+
if (!streamsDirectory.exists()) {
1279+
streamsDirectory.mkdirs();
1280+
}
13071281

1282+
String vodId = RandomStringUtils.secure().nextNumeric(24);
1283+
File savedFile = new File(streamsDirectory, vodId + "." + fileExtension);
13081284

1309-
String relativePath = AntMediaApplicationAdapter.getRelativePath(path);
1285+
if (!savedFile.toPath().normalize().startsWith(streamsDirectory.toPath().normalize())) {
1286+
throw new IOException("Entry is outside of the target directory");
1287+
}
13101288

1311-
VoD newVod = new VoD(fileName, "file", relativePath, fileName, unixTime, 0, Muxer.getDurationInMs(savedFile,fileName), fileSize,
1312-
VoD.UPLOADED_VOD, vodId, null);
1289+
int read = 0;
1290+
byte[] bytes = new byte[2048];
1291+
try (OutputStream outpuStream = new FileOutputStream(savedFile)) {
1292+
while ((read = inputStream.read(bytes)) != -1) {
1293+
outpuStream.write(bytes, 0, read);
1294+
}
1295+
outpuStream.flush();
13131296

1314-
if (StringUtils.isNotBlank(vodUploadFinishScript)) {
1315-
newVod.setProcessStatus(VoD.PROCESS_STATUS_INQUEUE);
1316-
}
1297+
long fileSize = savedFile.length();
1298+
long unixTime = System.currentTimeMillis();
1299+
String relativePath = AntMediaApplicationAdapter.getRelativePath(savedFile.getPath());
13171300

1318-
id = getDataStore().addVod(newVod);
1301+
VoD newVod = new VoD(fileName, "file", relativePath, fileName, unixTime, 0,
1302+
Muxer.getDurationInMs(savedFile, fileName), fileSize, VoD.UPLOADED_VOD, vodId, null);
13191303

1320-
if(id != null) {
1321-
success = true;
1322-
message = id;
1304+
if (StringUtils.isNotBlank(metadata)) {
1305+
newVod.setMetadata(metadata);
1306+
}
13231307

1324-
if (StringUtils.isNotBlank(vodUploadFinishScript))
1325-
{
1326-
startVoDScriptProcess(vodUploadFinishScript, savedFile, newVod, id);
1308+
if (StringUtils.isNotBlank(vodUploadFinishScript)) {
1309+
newVod.setProcessStatus(VoD.PROCESS_STATUS_INQUEUE);
1310+
}
13271311

1328-
}
1312+
id = getDataStore().addVod(newVod);
13291313

1330-
}
1331-
}
1314+
if (id != null && StringUtils.isNotBlank(vodUploadFinishScript)) {
1315+
startVoDScriptProcess(vodUploadFinishScript, savedFile, newVod, id);
13321316
}
13331317
}
1334-
else {
1335-
//this message has been used in the frontend(webpanel) pay attention
1336-
message = "notSupportedFileType";
1337-
}
1338-
1339-
}
1340-
catch (IOException iox) {
1318+
} catch (IOException iox) {
13411319
logger.error(iox.getMessage());
1320+
return new Result(false, null, "");
13421321
}
13431322

1344-
1345-
return new Result(success, id, message);
1323+
return new Result(id != null, id, id != null ? id : "");
13461324
}
13471325

13481326
public void startVoDScriptProcess(String vodUploadFinishScript, File savedFile, VoD newVod, String vodId) {

src/main/java/io/antmedia/rest/VoDRestService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,9 @@ public Result deleteVoDsBulk(
171171
@Override
172172
public Result uploadVoDFile(
173173
@Parameter(description = "Name of the VoD File", required = true) @QueryParam("name") String fileName,
174-
@Parameter(description = "VoD file", required = true) @FormDataParam("file") InputStream inputStream) {
175-
return super.uploadVoDFile(fileName, inputStream);
174+
@Parameter(description = "VoD file", required = true) @FormDataParam("file") InputStream inputStream,
175+
@Parameter(description = "Custom metadata for the VoD file", required = false) @FormDataParam("metadata") String metadata) {
176+
return super.uploadVoDFile(fileName, inputStream, metadata);
176177
}
177178

178179

src/main/java/io/antmedia/streamsource/StreamFetcherManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,7 @@ public void controlStreamFetchers(boolean restart) {
567567
//stream blocked means there is a connection to stream source and it's waiting to read a new packet
568568
//Most of the time the problem is related to the stream source side.
569569

570-
if (!streamScheduler.isStreamBlocked() && !streamScheduler.isStreamAlive() &&
571-
broadcast != null && AntMediaApplicationAdapter.BROADCAST_STATUS_TERMINATED_UNEXPECTEDLY.equals(broadcast.getStatus())) {
570+
if (!streamScheduler.isStreamBlocked() && !streamScheduler.isStreamAlive() && AntMediaApplicationAdapter.BROADCAST_STATUS_TERMINATED_UNEXPECTEDLY.equals(broadcast.getStatus())) {
572571
// if it's not blocked and it's not alive, stop the stream
573572
logger.info("Stopping the stream because it is not getting updated(aka terminated_unexpectedly) and it will start for the streamId:{}", streamScheduler.getStreamId());
574573
stopStreaming(streamScheduler.getStreamId(), false);

src/test/java/io/antmedia/integration/RestServiceV2Test.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,10 @@ public static String callAddStreamSource(Broadcast broadcast, boolean autoStart)
568568

569569

570570
public static Result callUploadVod(File file) throws Exception {
571+
return callUploadVod(file, null);
572+
}
573+
574+
public static Result callUploadVod(File file, String metadata) throws Exception {
571575

572576
String url = ROOT_SERVICE_URL + "/v2/vods/create?name=" + file.getName();
573577
HttpClient client = HttpClients.custom().setRedirectStrategy(new LaxRedirectStrategy()).build();
@@ -580,6 +584,10 @@ public static Result callUploadVod(File file) throws Exception {
580584

581585
builder.addPart("file", fileBody);
582586

587+
if (metadata != null) {
588+
builder.addTextBody("metadata", metadata);
589+
}
590+
583591
HttpEntity entity = builder.build();
584592
post.setEntity(entity);
585593
HttpResponse response = client.execute(post);
@@ -1161,7 +1169,21 @@ public void testUploadVoDFile() {
11611169
//file should be deleted
11621170
assertFalse(MuxingTest.isURLAvailable("http://" + SERVER_ADDR + ":5080/LiveApp/streams/" + vodId + ".mp4"));
11631171

1172+
// Test upload with metadata
1173+
String testMetadata = "{\"customField\":\"testValue\"}";
1174+
try {
1175+
result = callUploadVod(file, testMetadata);
1176+
} catch (Exception e) {
1177+
e.printStackTrace();
1178+
}
1179+
assertTrue(result.isSuccess());
1180+
String vodIdWithMetadata = result.getMessage();
1181+
1182+
VoD vodWithMetadata = callGetVoD(vodIdWithMetadata);
1183+
assertNotNull(vodWithMetadata);
1184+
assertEquals(testMetadata, vodWithMetadata.getMetadata());
11641185

1186+
deleteVoD(vodIdWithMetadata);
11651187

11661188
}
11671189

src/test/java/io/antmedia/test/rest/VoDRestServiceV2UnitTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,26 @@ public void testUploadVodFile() throws FileNotFoundException, IOException {
366366
assertEquals(3, store.getTotalVodNumber());
367367

368368
assertEquals(3, restServiceReal.getTotalVodNumber().getNumber());
369+
370+
// Test upload with metadata
371+
try (FileInputStream inputStream4 = new FileInputStream("src/test/resources/sample_MP4_480.mp4")) {
372+
String testMetadata = "{\"customField\":\"value\"}";
373+
Result result4 = restServiceReal.uploadVoDFile(fileName, inputStream4, testMetadata);
374+
assertTrue(result4.isSuccess());
375+
376+
VoD vodWithMetadata = restServiceReal.getVoD(result4.getDataId());
377+
assertEquals(testMetadata, vodWithMetadata.getMetadata());
378+
}
379+
380+
// Test upload without metadata (null) - should work, metadata should be null
381+
try (FileInputStream inputStream5 = new FileInputStream("src/test/resources/sample_MP4_480.mp4")) {
382+
Result result5 = restServiceReal.uploadVoDFile(fileName, inputStream5, null);
383+
assertTrue(result5.isSuccess());
384+
385+
VoD vodWithoutMetadata = restServiceReal.getVoD(result5.getDataId());
386+
assertNull(vodWithoutMetadata.getMetadata());
387+
}
369388
}
370-
371389

372390

373391
}

0 commit comments

Comments
 (0)