【发布时间】:2020-01-20 15:40:13
【问题描述】:
我想将一个 PowerPoint 文件导入我的 Symfony 应用程序。 我允许这些 mime 类型进入白名单参数。
co_upload_white_list:
- "image/*"
- "application/pdf"
- "application/x-pdf"
- "application/msword"
- "application/vnd.openxmlformats-officedocument.presentationml.presentation"
- "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
- "application/vnd.oasis.opendocument.text"
- "application/vnd.ms-excel"
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
- "application/vnd.oasis.opendocument.spreadsheet"
- "application/vnd.ms-powerpoint"
此代码工作正常,但 Symfony/Component/HttpFoundation/File/MimeType/FileBinaryMimeTypeGuesser.php 在 $file->getMimeType() 处返回的 mimetype : application / octet-stream 但它应该是 application/vnd.openxmlformats-officedocument .presentationml.presentation
use Oneup\UploaderBundle\Uploader\Naming\NamerInterface;
class UploadListener implements NamerInterface
{
[....]
public function onValidate(ValidationEvent $event)
{
/**
* $file is oneup/uploader-bundle/Uploader/File/FilesystemFile.php
*/
$file = $event->getFile();
$mimeType = $file->getMimeType();
$fileSize = $file->getSize();
$ext = $file->getExtension();
$hotel = $event->getRequest()->attributes->get('hotel');
$locale = ($hotel->getUserLocale()) ? $hotel->getUserLocale() : 'en';
if (explode("/", $mimeType)[0] != "image" && !in_array($mimeType, $this->uploadWhiteList)) {
$this->messenger->error("MimeType invalid: %mimeType%", ['%mimeType%' => $mimeType], $locale);
throw new HttpException(400, "File type invalid, mimetype is '$mimeType', allowed=".json_encode($this->uploadWhiteList));
}
[...]
}
有没有办法获得正确的 mime 类型?
【问题讨论】:
标签: symfony mime-types