2021-02-08 23:07:21 +00:00
|
|
|
#include <qpdf/QPDFEmbeddedFileDocumentHelper.hh>
|
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// File attachments are stored in the /EmbeddedFiles (name tree) key of the /Names dictionary from
|
|
|
|
// the document catalog. Each entry points to a /FileSpec, which in turn points to one more Embedded
|
|
|
|
// File Streams. Note that file specs can appear in other places as well, such as file attachment
|
|
|
|
// annotations, among others.
|
2021-02-08 23:07:21 +00:00
|
|
|
//
|
|
|
|
// root -> /Names -> /EmbeddedFiles = name tree
|
|
|
|
// filename -> filespec
|
|
|
|
// <<
|
|
|
|
// /Desc ()
|
|
|
|
// /EF <<
|
|
|
|
// /F x 0 R
|
|
|
|
// /UF x 0 R
|
|
|
|
// >>
|
|
|
|
// /F (name)
|
|
|
|
// /UF (name)
|
|
|
|
// /Type /Filespec
|
|
|
|
// >>
|
|
|
|
// x 0 obj
|
|
|
|
// <<
|
|
|
|
// /Type /EmbeddedFile
|
|
|
|
// /DL filesize % not in spec?
|
|
|
|
// /Params <<
|
|
|
|
// /CheckSum <md5>
|
|
|
|
// /CreationDate (D:yyyymmddhhmmss{-hh'mm'|+hh'mm'|Z})
|
|
|
|
// /ModDate (D:yyyymmddhhmmss-hh'mm')
|
|
|
|
// /Size filesize
|
|
|
|
// /Subtype /mime#2ftype
|
|
|
|
// >>
|
|
|
|
// >>
|
|
|
|
|
|
|
|
QPDFEmbeddedFileDocumentHelper::QPDFEmbeddedFileDocumentHelper(QPDF& qpdf) :
|
|
|
|
QPDFDocumentHelper(qpdf),
|
|
|
|
m(new Members())
|
|
|
|
{
|
|
|
|
auto root = qpdf.getRoot();
|
|
|
|
auto names = root.getKey("/Names");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (names.isDictionary()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
auto embedded_files = names.getKey("/EmbeddedFiles");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (embedded_files.isDictionary()) {
|
2023-05-21 17:35:09 +00:00
|
|
|
m->embedded_files = std::make_shared<QPDFNameTreeObjectHelper>(embedded_files, qpdf);
|
2021-02-08 23:07:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFEmbeddedFileDocumentHelper::hasEmbeddedFiles() const
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
return (m->embedded_files != nullptr);
|
2021-02-08 23:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFEmbeddedFileDocumentHelper::initEmbeddedFiles()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (hasEmbeddedFiles()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto root = qpdf.getRoot();
|
|
|
|
auto names = root.getKey("/Names");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!names.isDictionary()) {
|
2023-05-21 17:35:09 +00:00
|
|
|
names = root.replaceKeyAndGetNew("/Names", QPDFObjectHandle::newDictionary());
|
2021-02-08 23:07:21 +00:00
|
|
|
}
|
|
|
|
auto embedded_files = names.getKey("/EmbeddedFiles");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!embedded_files.isDictionary()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
auto nth = QPDFNameTreeObjectHelper::newEmpty(this->qpdf);
|
|
|
|
names.replaceKey("/EmbeddedFiles", nth.getObjectHandle());
|
2023-05-21 13:42:34 +00:00
|
|
|
m->embedded_files = std::make_shared<QPDFNameTreeObjectHelper>(nth);
|
2021-02-08 23:07:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<QPDFFileSpecObjectHelper>
|
|
|
|
QPDFEmbeddedFileDocumentHelper::getEmbeddedFile(std::string const& name)
|
|
|
|
{
|
|
|
|
std::shared_ptr<QPDFFileSpecObjectHelper> result;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->embedded_files) {
|
|
|
|
auto i = m->embedded_files->find(name);
|
|
|
|
if (i != m->embedded_files->end()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
result = std::make_shared<QPDFFileSpecObjectHelper>(i->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, std::shared_ptr<QPDFFileSpecObjectHelper>>
|
|
|
|
QPDFEmbeddedFileDocumentHelper::getEmbeddedFiles()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
std::map<std::string, std::shared_ptr<QPDFFileSpecObjectHelper>> result;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->embedded_files) {
|
|
|
|
for (auto const& i: *(m->embedded_files)) {
|
2023-05-21 17:35:09 +00:00
|
|
|
result[i.first] = std::make_shared<QPDFFileSpecObjectHelper>(i.second);
|
2021-02-08 23:07:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFEmbeddedFileDocumentHelper::replaceEmbeddedFile(
|
|
|
|
std::string const& name, QPDFFileSpecObjectHelper const& fs)
|
|
|
|
{
|
|
|
|
initEmbeddedFiles();
|
2023-05-21 13:42:34 +00:00
|
|
|
m->embedded_files->insert(name, fs.getObjectHandle());
|
2021-02-08 23:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFEmbeddedFileDocumentHelper::removeEmbeddedFile(std::string const& name)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!hasEmbeddedFiles()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
auto iter = m->embedded_files->find(name);
|
|
|
|
if (iter == m->embedded_files->end()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto oh = iter->second;
|
|
|
|
iter.remove();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (oh.isIndirect()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
this->qpdf.replaceObject(oh.getObjGen(), QPDFObjectHandle::newNull());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|