Skip to content

Commit af89581

Browse files
committed
Refactored VOD rest method, so that sonar passes cognitive complexity check.
1 parent cd6b0a5 commit af89581

File tree

1 file changed

+47
-77
lines changed

1 file changed

+47
-77
lines changed

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

Lines changed: 47 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,101 +1256,71 @@ public Result uploadVoDFile(String fileName, InputStream inputStream) {
12561256
}
12571257

12581258
public Result uploadVoDFile(String fileName, InputStream inputStream, String metadata) {
1259-
boolean success = false;
1260-
String message = "";
1261-
String id= null;
1259+
String id = null;
12621260
String appScopeName = getScope().getName();
12631261
String fileExtension = FilenameUtils.getExtension(fileName);
1264-
try {
1265-
1266-
String[] supportedFormats = new String[] {"mp4", "webm", "mov", "avi", "mp3", "wmv"};
1267-
1268-
if (ArrayUtils.contains(supportedFormats, fileExtension)) {
1269-
1270-
1271-
IStatsCollector statsCollector = (IStatsCollector) getAppContext().getBean(IStatsCollector.BEAN_NAME);
1272-
String vodUploadFinishScript = getAppSettings().getVodUploadFinishScript();
1273-
if (StringUtils.isNotBlank(vodUploadFinishScript) && !statsCollector.enoughResource())
1274-
{
1275-
logger.info("Not enough resource to upload VoD file");
1276-
message = "Not enough system resources available to upload and process VoD File";
1277-
}
1278-
else
1279-
{
1280-
1281-
File streamsDirectory = new File(
1282-
getStreamsDirectory(appScopeName));
1283-
1284-
// if the directory does not exist, create it
1285-
if (!streamsDirectory.exists()) {
1286-
streamsDirectory.mkdirs();
1287-
}
1288-
String vodId = RandomStringUtils.secure().nextNumeric(24);
1289-
1290-
1291-
File savedFile = new File(streamsDirectory, vodId + "." + fileExtension);
1292-
1293-
if (!savedFile.toPath().normalize().startsWith(streamsDirectory.toPath().normalize())) {
1294-
throw new IOException("Entry is outside of the target directory");
1295-
}
1296-
1297-
int read = 0;
1298-
byte[] bytes = new byte[2048];
1299-
try (OutputStream outpuStream = new FileOutputStream(savedFile))
1300-
{
1301-
1302-
while ((read = inputStream.read(bytes)) != -1) {
1303-
outpuStream.write(bytes, 0, read);
1304-
}
1305-
outpuStream.flush();
1306-
1307-
long fileSize = savedFile.length();
1308-
long unixTime = System.currentTimeMillis();
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+
}
13091268

1310-
String path = savedFile.getPath();
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+
}
13111275

1276+
try {
1277+
File streamsDirectory = new File(getStreamsDirectory(appScopeName));
1278+
if (!streamsDirectory.exists()) {
1279+
streamsDirectory.mkdirs();
1280+
}
13121281

1313-
String relativePath = AntMediaApplicationAdapter.getRelativePath(path);
1282+
String vodId = RandomStringUtils.secure().nextNumeric(24);
1283+
File savedFile = new File(streamsDirectory, vodId + "." + fileExtension);
13141284

1315-
VoD newVod = new VoD(fileName, "file", relativePath, fileName, unixTime, 0, Muxer.getDurationInMs(savedFile,fileName), fileSize,
1316-
VoD.UPLOADED_VOD, vodId, null);
1285+
if (!savedFile.toPath().normalize().startsWith(streamsDirectory.toPath().normalize())) {
1286+
throw new IOException("Entry is outside of the target directory");
1287+
}
13171288

1318-
if (StringUtils.isNotBlank(metadata)) {
1319-
newVod.setMetadata(metadata);
1320-
}
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();
13211296

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

1326-
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);
13271303

1328-
if(id != null) {
1329-
success = true;
1330-
message = id;
1304+
if (StringUtils.isNotBlank(metadata)) {
1305+
newVod.setMetadata(metadata);
1306+
}
13311307

1332-
if (StringUtils.isNotBlank(vodUploadFinishScript))
1333-
{
1334-
startVoDScriptProcess(vodUploadFinishScript, savedFile, newVod, id);
1308+
if (StringUtils.isNotBlank(vodUploadFinishScript)) {
1309+
newVod.setProcessStatus(VoD.PROCESS_STATUS_INQUEUE);
1310+
}
13351311

1336-
}
1312+
id = getDataStore().addVod(newVod);
13371313

1338-
}
1339-
}
1314+
if (id != null && StringUtils.isNotBlank(vodUploadFinishScript)) {
1315+
startVoDScriptProcess(vodUploadFinishScript, savedFile, newVod, id);
13401316
}
13411317
}
1342-
else {
1343-
//this message has been used in the frontend(webpanel) pay attention
1344-
message = "notSupportedFileType";
1345-
}
1346-
1347-
}
1348-
catch (IOException iox) {
1318+
} catch (IOException iox) {
13491319
logger.error(iox.getMessage());
1320+
return new Result(false, null, "");
13501321
}
13511322

1352-
1353-
return new Result(success, id, message);
1323+
return new Result(id != null, id, id != null ? id : "");
13541324
}
13551325

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

0 commit comments

Comments
 (0)