From 956b050e75202ed0911dabe735304728772b2394 Mon Sep 17 00:00:00 2001 From: Eddie Kohler Date: Sun, 10 Sep 2023 23:55:25 -0400 Subject: [PATCH] Keynote files are so stupid. --- lib/mimetype.php | 39 +++++++++++++++++++++++++++------------ src/paperoption.php | 6 +++--- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/lib/mimetype.php b/lib/mimetype.php index 525d5f21b..e41108ce6 100644 --- a/lib/mimetype.php +++ b/lib/mimetype.php @@ -23,6 +23,7 @@ class Mimetype { const FLAG_INCOMPRESSIBLE = 8; const FLAG_TEXTUAL = 16; const FLAG_REQUIRE_SNIFF = 32; + const FLAG_ZIPLIKE = 64; /** @var string */ public $mimetype; @@ -41,7 +42,7 @@ class Mimetype { self::PDF_TYPE => [".pdf", "PDF", self::FLAG_INLINE | self::FLAG_REQUIRE_SNIFF], self::PS_TYPE => [".ps", "PostScript", self::FLAG_COMPRESSIBLE], self::PPT_TYPE => [".ppt", "PowerPoint", self::FLAG_INCOMPRESSIBLE, "application/mspowerpoint", "application/powerpoint", "application/x-mspowerpoint"], - self::KEYNOTE_TYPE => [".key", "Keynote", self::FLAG_INCOMPRESSIBLE, "application/x-iwork-keynote-sffkey"], + self::KEYNOTE_TYPE => [".key", "Keynote", self::FLAG_INCOMPRESSIBLE | self::FLAG_ZIPLIKE, "application/x-iwork-keynote-sffkey"], "application/vnd.openxmlformats-officedocument.presentationml.presentation" => [".pptx", "PowerPoint", self::FLAG_INCOMPRESSIBLE], "video/mp4" => [".mp4", null, self::FLAG_INCOMPRESSIBLE], @@ -165,16 +166,6 @@ static function type_with_charset($type) { } } - /** @param string|Mimetype $typea - * @param string|Mimetype $typeb - * @return bool */ - static function type_equals($typea, $typeb) { - $ta = self::type($typea); - $tb = self::type($typeb); - return ($typea && $typea === $typeb) - || ($ta !== null && $ta === $tb); - } - /** @param string|Mimetype $type * @return string */ static function extension($type) { @@ -198,7 +189,7 @@ static function description($type) { } } - /** @param list $types + /** @param list $types * @return string */ static function list_description($types) { if (count($types) === 0) { @@ -211,6 +202,21 @@ static function list_description($types) { } } + /** @param list $types + * @return ?string */ + static function list_accept($types) { + $mta = []; + foreach ($types as $mt) { + if ($mt->type === self::BIN_TYPE + || ($mt->flags & self::FLAG_ZIPLIKE) !== 0) { + return null; + } else { + $mta[] = $mt->mimetype; + } + } + return empty($mta) ? null : join(",", $mta); + } + /** @param string|Mimetype $type * @return bool */ static function disposition_inline($type) { @@ -240,6 +246,15 @@ static function compressible($type) { } } + /** @param string|Mimetype $type + * @return bool */ + function matches($type) { + $xt = self::type($type); + return $xt === $this->mimetype + || (($this->flags & self::FLAG_ZIPLIKE) !== 0 + && $xt === self::ZIP_TYPE); + } + /** @return list */ static function builtins() { diff --git a/src/paperoption.php b/src/paperoption.php index 7703ebf0e..db980ec53 100644 --- a/src/paperoption.php +++ b/src/paperoption.php @@ -1472,8 +1472,8 @@ function print_web_edit(PaperTable $pt, $ov, $reqov) { if ($doc) { echo ' data-docid="', $doc->paperStorageId, '"'; } - if ($mimetypes) { - echo ' data-document-accept="', htmlspecialchars(join(",", array_map(function ($m) { return $m->mimetype; }, $mimetypes))), '"'; + if (($accept = Mimetype::list_accept($mimetypes))) { + echo ' data-document-accept="', htmlspecialchars($accept), '"'; } if ($this->max_size > 0) { echo ' data-document-max-size="', (int) $this->max_size, '"'; @@ -1529,7 +1529,7 @@ function validate_document(DocumentInfo $doc) { return true; } for ($i = 0; $i < count($mimetypes); ++$i) { - if ($mimetypes[$i]->mimetype === $doc->mimetype) + if ($mimetypes[$i]->matches($doc->mimetype)) return true; } $desc = Mimetype::list_description($mimetypes);