Skip to content

Commit

Permalink
Keynote files are so stupid.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Sep 11, 2023
1 parent 1b5d2c7 commit 956b050
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
39 changes: 27 additions & 12 deletions lib/mimetype.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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],
Expand Down Expand Up @@ -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) {
Expand All @@ -198,7 +189,7 @@ static function description($type) {
}
}

/** @param list<string|Mimetype> $types
/** @param list<Mimetype> $types
* @return string */
static function list_description($types) {
if (count($types) === 0) {
Expand All @@ -211,6 +202,21 @@ static function list_description($types) {
}
}

/** @param list<Mimetype> $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) {
Expand Down Expand Up @@ -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<Mimetype> */
static function builtins() {
Expand Down
6 changes: 3 additions & 3 deletions src/paperoption.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '"';
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 956b050

Please sign in to comment.