Skip to content

Commit

Permalink
cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcazacu committed Aug 24, 2018
1 parent abf27d0 commit a1eecd5
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 38 deletions.
4 changes: 1 addition & 3 deletions src/main/java/com/netflix/imflibrary/IMPDelivery.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
import com.netflix.imflibrary.st0429_9.BasicMapProfileV2FileSet;
import com.netflix.imflibrary.st0429_9.BasicMapProfileV2MappedFileSet;
import com.netflix.imflibrary.utils.FileLocator;
import com.netflix.imflibrary.utils.S3FileLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

import javax.annotation.concurrent.Immutable;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -66,7 +64,7 @@ public IMPDelivery(BasicMapProfileV2FileSet basicMapProfileV2FileSet) throws IOE
for (AssetMap.Asset packingListAsset : packingListAssets)
{
URI absolutePackingListURI = basicMapProfileV2FileSet.getAbsoluteAssetMapURI().resolve(packingListAsset.getPath());
PackingList packingList = new PackingList(new S3FileLocator(absolutePackingListURI));
PackingList packingList = new PackingList(FileLocator.fromLocation(absolutePackingListURI));

List<IMPAsset> referencedAssets = new ArrayList<>();
for (PackingList.Asset referencedAsset : packingList.getAssets())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import com.netflix.imflibrary.IMFErrorLogger;
import com.netflix.imflibrary.IMFErrorLoggerImpl;
import com.netflix.imflibrary.st0377.HeaderPartition;
import com.netflix.imflibrary.utils.*;
import com.netflix.imflibrary.utils.ErrorLogger;
import com.netflix.imflibrary.utils.FileLocator;
import com.netflix.imflibrary.utils.RegXMLLibHelper;
import com.netflix.imflibrary.utils.ResourceByteRangeProvider;
import com.netflix.imflibrary.writerTools.CompositionPlaylistBuilder_2013;
import com.sandflow.smpte.klv.Triplet;
import com.netflix.imflibrary.KLVPacket;
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/com/netflix/imflibrary/app/IMPAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,24 @@ private static List<ErrorLogger.ErrorObject> conformVirtualTrack(ResourceByteRan
return imfErrorLogger.getErrors();
}

public static Map<String, List<ErrorLogger.ErrorObject>> analyzePackage(FileLocator fileLocator) throws IOException {
public static Map<String, List<ErrorLogger.ErrorObject>> analyzePackage(FileLocator rootFileLocator) throws IOException {
Map<String, List<ErrorLogger.ErrorObject>> errorMap = new HashMap<>();
IMFErrorLogger imfErrorLogger = new IMFErrorLoggerImpl();
List<PayloadRecord> headerPartitionPayloadRecords = new ArrayList<>();
try {
BasicMapProfileV2MappedFileSet mapProfileV2MappedFileSet = new BasicMapProfileV2MappedFileSet(fileLocator);
BasicMapProfileV2MappedFileSet mapProfileV2MappedFileSet = new BasicMapProfileV2MappedFileSet(rootFileLocator);
imfErrorLogger.addAllErrors(mapProfileV2MappedFileSet.getErrors());
IMFErrorLogger assetMapErrorLogger = new IMFErrorLoggerImpl();

try {

AssetMap assetMap = new AssetMap(FileLocator.fromLocation(mapProfileV2MappedFileSet.getAbsoluteAssetMapURI()));
assetMapErrorLogger.addAllErrors(assetMap.getErrors());

for (AssetMap.Asset packingListAsset : assetMap.getPackingListAssets()) {
IMFErrorLogger packingListErrorLogger = new IMFErrorLoggerImpl();
try {
PackingList packingList = new PackingList(FileLocator.fromLocation(fileLocator, packingListAsset.getPath().toString()));
PackingList packingList = new PackingList(FileLocator.fromLocation(rootFileLocator, packingListAsset.getPath().toString()));
packingListErrorLogger.addAllErrors(packingList.getErrors());

for (PackingList.Asset asset : packingList.getAssets()) {
Expand All @@ -256,7 +257,7 @@ public static Map<String, List<ErrorLogger.ErrorObject>> analyzePackage(FileLoca
IMFErrorLogger.IMFErrors.ErrorLevels.FATAL, String.format("Failed to get path for Asset with ID = %s", asset.getUUID().toString()));
continue;
}
FileLocator assetFile = FileLocator.fromLocation(fileLocator, assetMap.getPath(asset.getUUID()).toString());
FileLocator assetFile = FileLocator.fromLocation(rootFileLocator, assetMap.getPath(asset.getUUID()).toString());

if(!assetFile.exists()) {
packingListErrorLogger.addError(IMFErrorLogger.IMFErrors.ErrorCodes.IMF_PKL_ERROR,
Expand Down Expand Up @@ -294,9 +295,9 @@ public static Map<String, List<ErrorLogger.ErrorObject>> analyzePackage(FileLoca
}
}

List<ApplicationComposition> applicationCompositionList = analyzeApplicationCompositions( fileLocator, assetMap, packingList, headerPartitionPayloadRecords, packingListErrorLogger, errorMap);
List<ApplicationComposition> applicationCompositionList = analyzeApplicationCompositions( rootFileLocator, assetMap, packingList, headerPartitionPayloadRecords, packingListErrorLogger, errorMap);

analyzeOutputProfileLists( fileLocator, assetMap, packingList, applicationCompositionList, packingListErrorLogger, errorMap);
analyzeOutputProfileLists( rootFileLocator, assetMap, packingList, applicationCompositionList, packingListErrorLogger, errorMap);

} catch (IMFException e) {
packingListErrorLogger.addAllErrors(e.getErrors());
Expand All @@ -313,7 +314,7 @@ public static Map<String, List<ErrorLogger.ErrorObject>> analyzePackage(FileLoca
}
} catch (IMFException e) {
imfErrorLogger.addAllErrors(e.getErrors());
errorMap.put(fileLocator.getName(), imfErrorLogger.getErrors());
errorMap.put(rootFileLocator.getName(), imfErrorLogger.getErrors());
}


Expand Down Expand Up @@ -342,7 +343,7 @@ public static List<ErrorLogger.ErrorObject> validateEssencePartition(ResourceByt
return trackFileErrorLogger.getErrors();
}

public static List<OutputProfileList> analyzeOutputProfileLists(FileLocator fileLocator,
public static List<OutputProfileList> analyzeOutputProfileLists(FileLocator rootFileLocator,
AssetMap assetMap,
PackingList packingList,
List<ApplicationComposition> applicationCompositionList,
Expand All @@ -359,7 +360,7 @@ public static List<OutputProfileList> analyzeOutputProfileLists(FileLocator file
IMFErrorLogger.IMFErrors.ErrorLevels.FATAL, String.format("Failed to get path for Asset with ID = %s", asset.getUUID().toString()));
continue;
}
FileLocator assetFile = FileLocator.fromLocation(fileLocator, assetMap.getPath(asset.getUUID()).toString());
FileLocator assetFile = FileLocator.fromLocation(rootFileLocator, assetMap.getPath(asset.getUUID()).toString());

if(!assetFile.exists()) {
packingListErrorLogger.addError(IMFErrorLogger.IMFErrors.ErrorCodes.IMF_PKL_ERROR,
Expand Down Expand Up @@ -400,7 +401,7 @@ public static List<OutputProfileList> analyzeOutputProfileLists(FileLocator file
}


public static List<ApplicationComposition> analyzeApplicationCompositions( FileLocator rootFile,
public static List<ApplicationComposition> analyzeApplicationCompositions( FileLocator rootFileLocator,
AssetMap assetMap,
PackingList packingList,
List<PayloadRecord> headerPartitionPayloadRecords,
Expand All @@ -420,7 +421,7 @@ public static List<ApplicationComposition> analyzeApplicationCompositions( FileL
IMFErrorLogger.IMFErrors.ErrorLevels.FATAL, String.format("Failed to get path for Asset with ID = %s", asset.getUUID().toString()));
continue;
}
FileLocator assetFile = FileLocator.fromLocation(rootFile, assetMap.getPath(asset.getUUID()).toString());
FileLocator assetFile = FileLocator.fromLocation(rootFileLocator, assetMap.getPath(asset.getUUID()).toString());

if(!assetFile.exists()) {
packingListErrorLogger.addError(IMFErrorLogger.IMFErrors.ErrorCodes.IMF_PKL_ERROR,
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/netflix/imflibrary/app/IMPFixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,18 @@ private static PayloadRecord getHeaderPartitionPayloadRecord(ResourceByteRangePr

}

public static List<ErrorLogger.ErrorObject> analyzePackageAndWrite(FileLocator fileLocator, File targetFile, String versionCPLSchema, Boolean copyTrackfile, Boolean generateHash) throws
public static List<ErrorLogger.ErrorObject> analyzePackageAndWrite(FileLocator rootFileLocator, File targetFile, String versionCPLSchema, Boolean copyTrackfile, Boolean generateHash) throws
IOException, ParserConfigurationException, SAXException, JAXBException, URISyntaxException, NoSuchAlgorithmException {
IMFErrorLogger imfErrorLogger = new IMFErrorLoggerImpl();
List<PayloadRecord> headerPartitionPayloadRecords = new ArrayList<>();
BasicMapProfileV2MappedFileSet mapProfileV2MappedFileSet = new BasicMapProfileV2MappedFileSet(fileLocator);
BasicMapProfileV2MappedFileSet mapProfileV2MappedFileSet = new BasicMapProfileV2MappedFileSet(rootFileLocator);
AssetMap assetMap = new AssetMap(new S3FileLocator(mapProfileV2MappedFileSet.getAbsoluteAssetMapURI()));
for (AssetMap.Asset packingListAsset : assetMap.getPackingListAssets()) {
PackingList packingList = new PackingList(new S3FileLocator(fileLocator.getAbsolutePath() + packingListAsset.getPath().toString()));
PackingList packingList = new PackingList(FileLocator.fromLocation(rootFileLocator, packingListAsset.getPath().toString()));
Map<UUID, IMPBuilder.IMFTrackFileMetadata> imfTrackFileMetadataMap = new HashMap<>();

for (PackingList.Asset asset : packingList.getAssets()) {
FileLocator assetFile = new S3FileLocator(fileLocator.getAbsolutePath() + assetMap.getPath(asset.getUUID()).toString());
FileLocator assetFile = FileLocator.fromLocation(rootFileLocator,assetMap.getPath(asset.getUUID()).toString());
ResourceByteRangeProvider resourceByteRangeProvider = assetFile.getResourceByteRangeProvider();

if (asset.getType().equals(PackingList.Asset.APPLICATION_MXF_TYPE)) {
Expand All @@ -208,7 +208,7 @@ public static List<ErrorLogger.ErrorObject> analyzePackageAndWrite(FileLocator f
);
if(copyTrackfile) {
File outputFile = new File(targetFile.toString() + File.separator + assetFile.getName());
// Files.copy(assetFile.toPath(), outputFile.toPath(), REPLACE_EXISTING);
FileLocator.copy(assetFile, outputFile.toPath());
}
}
}
Expand All @@ -218,7 +218,7 @@ public static List<ErrorLogger.ErrorObject> analyzePackageAndWrite(FileLocator f


for (PackingList.Asset asset : packingList.getAssets()) {
FileLocator assetFile = new S3FileLocator(fileLocator.getAbsolutePath() + assetMap.getPath(asset.getUUID()).toString());
FileLocator assetFile = FileLocator.fromLocation(rootFileLocator, assetMap.getPath(asset.getUUID()).toString());
ResourceByteRangeProvider resourceByteRangeProvider = assetFile.getResourceByteRangeProvider();
if (asset.getType().equals(PackingList.Asset.TEXT_XML_TYPE) && ApplicationComposition.isCompositionPlaylist(resourceByteRangeProvider)) {
ApplicationComposition applicationComposition = ApplicationCompositionFactory.getApplicationComposition(resourceByteRangeProvider, new IMFErrorLoggerImpl());
Expand Down Expand Up @@ -313,9 +313,9 @@ public static void main(String args[]) throws
}

String inputFileName = args[0];
File inputFile = new File(inputFileName);
if (!inputFile.exists()) {
logger.error(String.format("File %s does not exist", inputFile.getAbsolutePath()));
FileLocator inputFileLocator = FileLocator.fromLocation(inputFileName);
if (!inputFileLocator.exists()) {
logger.error(String.format("File %s does not exist", inputFileLocator.getAbsolutePath()));
System.exit(-1);
}

Expand Down Expand Up @@ -354,12 +354,12 @@ else if(curArg.equalsIgnoreCase("--no-hash") || curArg.equalsIgnoreCase("-nh"))
}
}

if (!inputFile.exists() || !inputFile.isDirectory()) {
if (!inputFileLocator.exists() || !inputFileLocator.isDirectory()) {
logger.error(String.format("Invalid input package path"));
}
else
{
List<ErrorLogger.ErrorObject> errors = analyzePackageAndWrite(new S3FileLocator("s3://"), outputFile, versionCPLSchema, copyTrackFile, generateHash);
List<ErrorLogger.ErrorObject> errors = analyzePackageAndWrite(inputFileLocator, outputFile, versionCPLSchema, copyTrackFile, generateHash);
if (errors.size() > 0) {
logger.info(String.format("IMPWriter encountered errors:"));
for (ErrorLogger.ErrorObject errorObject : errors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ private String getPKLContext(){

/**
* Constructor for a {@link com.netflix.imflibrary.st0429_8.PackingList PackingList} object that corresponds to a PackingList XML document
* @param packingListXMLFile the input XML file
* @param packingListXMLFileLocator the input XML file locator
* @throws IOException - any I/O related error is exposed through an IOException
*/
public PackingList(FileLocator packingListXMLFile) throws IOException {
this(packingListXMLFile.getResourceByteRangeProvider());
public PackingList(FileLocator packingListXMLFileLocator) throws IOException {
this(packingListXMLFileLocator.getResourceByteRangeProvider());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/netflix/imflibrary/st0429_9/AssetMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ private String getAssetMapContext(){
}
/**
* Constructor for an {@link com.netflix.imflibrary.st0429_9.AssetMap AssetMap} object from an XML file that contains an AssetMap document
* @param assetMapXmlFile the input XML file
* @param assetMapXmlFileLocator the input XML file locator
* @throws IOException - any I/O related error is exposed through an IOException
*/
public AssetMap(FileLocator assetMapXmlFile) throws IOException
public AssetMap(FileLocator assetMapXmlFileLocator) throws IOException
{
this(getFileAsResourceByteRangeProvider(assetMapXmlFile));
this(getFileAsResourceByteRangeProvider(assetMapXmlFileLocator));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public final class BasicMapProfileV2FileSet
* Constructor for a {@link BasicMapProfileV2FileSet BasicMapProfilev2FileSet} from a {@link BasicMapProfileV2MappedFileSet MappedFileSet} object. Construction
* succeeds if the constraints specified in Section A.1 in Annex A of st0429-9:2014 are satisfied
* @param basicMapProfileV2MappedFileSet the Mapped File Set object corresponding to this object
* @throws IOException - forwarded from {@link BasicMapProfileV2MappedFileSet#BasicMapProfileV2MappedFileSet(FileLocator) MappedFileSet} constructor
* @throws SAXException - forwarded from {@link BasicMapProfileV2MappedFileSet#BasicMapProfileV2MappedFileSet(FileLocator) MappedFileSet} constructor
* @throws JAXBException - forwarded from {@link BasicMapProfileV2MappedFileSet#BasicMapProfileV2MappedFileSet(FileLocator) MappedFileSet} constructor
* @throws URISyntaxException - forwarded from {@link BasicMapProfileV2MappedFileSet#BasicMapProfileV2MappedFileSet(FileLocator) MappedFileSet} constructor
*/
public BasicMapProfileV2FileSet(BasicMapProfileV2MappedFileSet basicMapProfileV2MappedFileSet) throws IOException, SAXException, JAXBException, URISyntaxException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ public final class BasicMapProfileV2MappedFileSet
private final IMFErrorLogger imfErrorLogger;
/**
* Constructor for a MappedFileSet object from a file representing the root of a directory tree
* @param fileLocator the directory which serves as the tree root of the Mapped File Set
* @param rootFileLocator the directory which serves as the tree root of the Mapped File Set
* @throws IOException - forwarded from {@link AssetMap#AssetMap(FileLocator) AssetMap} constructor
*/
public BasicMapProfileV2MappedFileSet(FileLocator fileLocator) throws IOException
public BasicMapProfileV2MappedFileSet(FileLocator rootFileLocator) throws IOException
{
imfErrorLogger = new IMFErrorLoggerImpl();
if (!fileLocator.isDirectory())
if (!rootFileLocator.isDirectory())
{
String message = String.format("Root file %s corresponding to the mapped file set is not a " +
"directory", fileLocator.getAbsolutePath());
"directory", rootFileLocator.getAbsolutePath());
imfErrorLogger.addError(IMFErrorLogger.IMFErrors.ErrorCodes.IMF_AM_ERROR, IMFErrorLogger.IMFErrors
.ErrorLevels.FATAL,
message);
Expand All @@ -74,12 +75,12 @@ public boolean accept(File rootFile, String name)
}
};

FileLocator[] files = fileLocator.listFiles(filenameFilter);
FileLocator[] files = rootFileLocator.listFiles(filenameFilter);
if ((files == null) || (files.length != 1))
{
String message = String.format("Found %d files with name %s in mapped file set rooted at %s, " +
"exactly 1 is allowed", (files == null) ? 0 : files.length, BasicMapProfileV2MappedFileSet
.ASSETMAP_FILE_NAME, fileLocator.getAbsolutePath());
.ASSETMAP_FILE_NAME, rootFileLocator.getAbsolutePath());
imfErrorLogger.addError(IMFErrorLogger.IMFErrors.ErrorCodes.IMF_AM_ERROR, IMFErrorLogger.IMFErrors
.ErrorLevels.FATAL,
message);
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/netflix/imflibrary/utils/FileLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@

package com.netflix.imflibrary.utils;

import sun.misc.IOUtils;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

/**
* This interface is the supertype for classes representing an file locator
Expand Down Expand Up @@ -54,6 +60,19 @@ public static FileLocator fromLocation(URI location) {
return new LocalFileLocator(location);
}

public static Path copy(FileLocator fileLocator, Path outputFilePath) throws IOException {
InputStream inputStream = fileLocator.getInputStream();

Files.copy(
inputStream,
outputFilePath,
StandardCopyOption.REPLACE_EXISTING);

inputStream.close();

return outputFilePath;
}

/**
* Tests whether the file denoted by this abstract pathname is a
* directory.
Expand Down

0 comments on commit a1eecd5

Please sign in to comment.