Speculative fix for assertion failure (DoM #2839).
[libdcp.git] / src / pkl.cc
index 22589b4d19a21962efbd0699e4b4263bc4f60952..57eda9dad025dd285ed725f2b8614b570417845e 100644 (file)
@@ -39,6 +39,7 @@
 
 #include "dcp_assert.h"
 #include "exceptions.h"
+#include "filesystem.h"
 #include "pkl.h"
 #include "raw_convert.h"
 #include "util.h"
@@ -64,14 +65,14 @@ PKL::PKL (boost::filesystem::path file)
        : _file (file)
 {
        cxml::Document pkl ("PackingList");
-       pkl.read_file (file);
+       pkl.read_file(dcp::filesystem::fix_long_path(file));
 
        if (pkl.namespace_uri() == pkl_interop_ns) {
                _standard = Standard::INTEROP;
        } else if (pkl.namespace_uri() == pkl_smpte_ns) {
                _standard = Standard::SMPTE;
        } else {
-               boost::throw_exception (XMLError ("Unrecognised packing list namesapce " + pkl.namespace_uri()));
+               boost::throw_exception(XMLError("Unrecognised packing list namespace " + pkl.namespace_uri()));
        }
 
        _id = remove_urn_uuid (pkl.string_child ("Id"));
@@ -81,20 +82,20 @@ PKL::PKL (boost::filesystem::path file)
        _creator = pkl.string_child ("Creator");
 
        for (auto i: pkl.node_child("AssetList")->node_children("Asset")) {
-               _asset_list.push_back (make_shared<Asset>(i));
+               _assets.push_back(make_shared<Asset>(i));
        }
 }
 
 
 void
-PKL::add_asset (std::string id, boost::optional<std::string> annotation_text, std::string hash, int64_t size, std::string type)
+PKL::add_asset(std::string id, boost::optional<std::string> annotation_text, std::string hash, int64_t size, std::string type, std::string original_filename)
 {
-       _asset_list.push_back (make_shared<Asset>(id, annotation_text, hash, size, type));
+       _assets.push_back(make_shared<Asset>(id, annotation_text, hash, size, type, original_filename));
 }
 
 
 void
-PKL::write (boost::filesystem::path file, shared_ptr<const CertificateChain> signer) const
+PKL::write_xml (boost::filesystem::path file, shared_ptr<const CertificateChain> signer) const
 {
        xmlpp::Document doc;
        xmlpp::Element* pkl;
@@ -113,7 +114,7 @@ PKL::write (boost::filesystem::path file, shared_ptr<const CertificateChain> sig
        pkl->add_child("Creator")->add_child_text (_creator);
 
        auto asset_list = pkl->add_child("AssetList");
-       for (auto i: _asset_list) {
+       for (auto i: _assets) {
                auto asset = asset_list->add_child("Asset");
                asset->add_child("Id")->add_child_text ("urn:uuid:" + i->id());
                if (i->annotation_text()) {
@@ -122,6 +123,9 @@ PKL::write (boost::filesystem::path file, shared_ptr<const CertificateChain> sig
                asset->add_child("Hash")->add_child_text (i->hash());
                asset->add_child("Size")->add_child_text (raw_convert<string>(i->size()));
                asset->add_child("Type")->add_child_text (i->type());
+               if (auto filename = i->original_filename()) {
+                       asset->add_child("OriginalFileName")->add_child_text(*filename);
+               }
        }
 
        indent (pkl, 0);
@@ -130,7 +134,7 @@ PKL::write (boost::filesystem::path file, shared_ptr<const CertificateChain> sig
                signer->sign (pkl, _standard);
        }
 
-       doc.write_to_file_formatted (file.string(), "UTF-8");
+       doc.write_to_file_formatted(dcp::filesystem::fix_long_path(file).string(), "UTF-8");
        _file = file;
 }
 
@@ -138,7 +142,7 @@ PKL::write (boost::filesystem::path file, shared_ptr<const CertificateChain> sig
 optional<string>
 PKL::hash (string id) const
 {
-       for (auto i: _asset_list) {
+       for (auto i: _assets) {
                if (i->id() == id) {
                        return i->hash();
                }
@@ -151,7 +155,7 @@ PKL::hash (string id) const
 optional<string>
 PKL::type (string id) const
 {
-       for (auto i: _asset_list) {
+       for (auto i: _assets) {
                if (i->id() == id) {
                        return i->type();
                }
@@ -159,3 +163,10 @@ PKL::type (string id) const
 
        return {};
 }
+
+
+void
+PKL::clear_assets()
+{
+       _assets.clear();
+}