2021-02-08 23:07:21 +00:00
|
|
|
#include <qpdf/QPDFFileSpecObjectHelper.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2021-02-08 23:07:21 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
2021-02-08 23:07:21 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
|
|
|
|
#include <string>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <vector>
|
2021-02-08 23:07:21 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFFileSpecObjectHelper::QPDFFileSpecObjectHelper(QPDFObjectHandle oh) :
|
2021-02-08 23:07:21 +00:00
|
|
|
QPDFObjectHelper(oh)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!oh.isDictionary()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
oh.warnIfPossible("Embedded file object is not a dictionary");
|
|
|
|
return;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!oh.isDictionaryOfType("/Filespec")) {
|
2021-02-08 23:07:21 +00:00
|
|
|
oh.warnIfPossible("Embedded file object's type is not /Filespec");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 17:35:09 +00:00
|
|
|
static std::vector<std::string> name_keys = {"/UF", "/F", "/Unix", "/DOS", "/Mac"};
|
2021-02-08 23:07:21 +00:00
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFFileSpecObjectHelper::getDescription()
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
auto desc = this->oh.getKey("/Desc");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (desc.isString()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
result = desc.getUTF8Value();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFFileSpecObjectHelper::getFilename()
|
|
|
|
{
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& i: name_keys) {
|
2021-02-08 23:07:21 +00:00
|
|
|
auto k = this->oh.getKey(i);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (k.isString()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
return k.getUTF8Value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, std::string>
|
|
|
|
QPDFFileSpecObjectHelper::getFilenames()
|
|
|
|
{
|
|
|
|
std::map<std::string, std::string> result;
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& i: name_keys) {
|
2021-02-08 23:07:21 +00:00
|
|
|
auto k = this->oh.getKey(i);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (k.isString()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
result[i] = k.getUTF8Value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFFileSpecObjectHelper::getEmbeddedFileStream(std::string const& key)
|
|
|
|
{
|
|
|
|
auto ef = this->oh.getKey("/EF");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!ef.isDictionary()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
return QPDFObjectHandle::newNull();
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!key.empty()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
return ef.getKey(key);
|
|
|
|
}
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& i: name_keys) {
|
2021-02-08 23:07:21 +00:00
|
|
|
auto k = ef.getKey(i);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (k.isStream()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
return k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QPDFObjectHandle::newNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFFileSpecObjectHelper::getEmbeddedFileStreams()
|
|
|
|
{
|
|
|
|
return this->oh.getKey("/EF");
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFFileSpecObjectHelper
|
|
|
|
QPDFFileSpecObjectHelper::createFileSpec(
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF& qpdf, std::string const& filename, std::string const& fullpath)
|
2021-02-08 23:07:21 +00:00
|
|
|
{
|
|
|
|
return createFileSpec(
|
2022-04-02 21:14:10 +00:00
|
|
|
qpdf,
|
|
|
|
filename,
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFEFStreamObjectHelper::createEFStream(qpdf, QUtil::file_provider(fullpath)));
|
2021-02-08 23:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFFileSpecObjectHelper
|
|
|
|
QPDFFileSpecObjectHelper::createFileSpec(
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF& qpdf, std::string const& filename, QPDFEFStreamObjectHelper efsoh)
|
2021-02-08 23:07:21 +00:00
|
|
|
{
|
|
|
|
auto oh = qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary());
|
|
|
|
oh.replaceKey("/Type", QPDFObjectHandle::newName("/Filespec"));
|
|
|
|
QPDFFileSpecObjectHelper result(oh);
|
|
|
|
result.setFilename(filename);
|
|
|
|
auto ef = QPDFObjectHandle::newDictionary();
|
2022-05-17 22:28:50 +00:00
|
|
|
ef.replaceKey("/F", efsoh.getObjectHandle());
|
|
|
|
ef.replaceKey("/UF", efsoh.getObjectHandle());
|
2021-02-08 23:07:21 +00:00
|
|
|
oh.replaceKey("/EF", ef);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFFileSpecObjectHelper&
|
|
|
|
QPDFFileSpecObjectHelper::setDescription(std::string const& desc)
|
|
|
|
{
|
|
|
|
this->oh.replaceKey("/Desc", QPDFObjectHandle::newUnicodeString(desc));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFFileSpecObjectHelper&
|
|
|
|
QPDFFileSpecObjectHelper::setFilename(
|
2022-04-02 21:14:10 +00:00
|
|
|
std::string const& unicode_name, std::string const& compat_name)
|
2021-02-08 23:07:21 +00:00
|
|
|
{
|
|
|
|
auto uf = QPDFObjectHandle::newUnicodeString(unicode_name);
|
|
|
|
this->oh.replaceKey("/UF", uf);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (compat_name.empty()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFileSpecObjectHelper empty compat_name");
|
|
|
|
this->oh.replaceKey("/F", uf);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2021-02-08 23:07:21 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFileSpecObjectHelper non-empty compat_name");
|
|
|
|
this->oh.replaceKey("/F", QPDFObjectHandle::newString(compat_name));
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|