/** * @param string|\SplFileInfo $file * @param string $filename * @return string[] */ public function correct($file, string $filename) : array { if (!(new validator\FilenameValidator())->validate($filename)) { throw new SyntaxException(sprintf(_('「%s」は妥当なファイル名ではありません。'), $filename)); } $type = is_string($file) ? $this->finfo->buffer($file) : $this->finfo->file($file->getRealPath()); if (empty(self::VALID_EXTENSIONS[$type])) { throw new SyntaxException(sprintf(_('「%s」は妥当な画像、音声、動画ファイルではありません。'), $filename)); } $extension = (new \SplFileInfo($filename))->getExtension(); if ($type === 'video/mp4' && $extension === 'm4a') { $type = 'audio/mp4'; } elseif (!in_array($extension, $type === 'video/mp4' ? array_merge(self::VALID_EXTENSIONS['audio/mp4'], self::VALID_EXTENSIONS['video/mp4']) : self::VALID_EXTENSIONS[$type])) { throw new SyntaxException(sprintf(_('「%s」の拡張子は次のいずれかにしなければなりません:'), $filename) . ' ' . implode(', ', self::VALID_EXTENSIONS[$type])); } $topLevelType = explode('/', $type)[0]; $this->checkFileSize(is_string($file) ? mb_strlen($file, 'ASCII') : $file->getSize(), $topLevelType, $filename); if ($topLevelType === 'image') { $validator = new validator\ImageValidator($type, $filename); $validator->setLogger($this->logger); $file = $validator->correct(is_string($file) ? $file : (new Parser())->getBinary($file)); } return ['bytes' => $file, 'type' => $type === 'image/svg+xml' ? 'image/svg+xml; charset=UTF-8' : $type, 'name' => $filename]; }
/** * @param \SplFileInfo $file * @param string|null $filename * @param string|null $title * @throws SyntaxException * @return Dictionary */ public function parse(\SplFileInfo $file, string $filename = null, string $title = null) : Dictionary { if ($file instanceof \SplTempFileObject) { $binary = (new Parser())->getBinary($file); } $byteFormatter = new \ScriptFUSION\Byte\ByteFormatter(); $fileSize = isset($binary) ? strlen(bin2hex($binary)) / 2 : $file->getSize(); if ($fileSize > self::MAX_COMPRESSED_ARCHIVE_SIZE) { throw new SyntaxException(sprintf(_('ファイルサイズは %1$s 以下にしてください: 現在 %2$s'), $byteFormatter->format(self::MAX_COMPRESSED_ARCHIVE_SIZE), $byteFormatter->format($fileSize))); } elseif ($fileSize > self::MAX_RECOMMENDED_COMPRESSED_ARCHIVE_SIZE) { $this->logger->warning(sprintf(_('ファイルサイズは %1$s 以下にすべきです: 現在 %2$s'), $byteFormatter->format(self::MAX_RECOMMENDED_COMPRESSED_ARCHIVE_SIZE), $byteFormatter->format($fileSize))); } $finfo = new Finfo(FILEINFO_MIME_TYPE); $type = isset($binary) ? $finfo->buffer($binary) : $finfo->file($file->getRealPath()); switch ($type) { case 'application/zip': $this->header = true; $csvFile = $this->parseArchive($file); $csv = new \SplFileInfo($this->generateTempFile($csvFile)); $files = new \FilesystemIterator($csvFile->getPath(), \FilesystemIterator::SKIP_DOTS); unlink($csvFile); break; case 'text/csv': case 'text/plain': $csv = $file; break; default: throw new SyntaxException(_('汎用辞書はCSVファイルかZIPファイルでなければなりません。')); } $filesCount = (isset($files) ? iterator_count($files) : count($this->filenames)) + 1; if ($filesCount > self::MAX_FILES) { throw new SyntaxException(sprintf(_('アーカイブ中のファイル数は %1$s 個以下にしてください: 現在 %2$s 個'), self::MAX_FILES, $filesCount)); } elseif ($filesCount > self::MAX_RECOMMENDED_FILES) { $this->logger->warning(sprintf(_('アーカイブ中のファイル数は %1$s 個以下にすべきです: 現在 %2$s 個'), self::MAX_RECOMMENDED_FILES, $filesCount)); } $dictionary = new Dictionary($files ?? $this->filenames); $dictionary->setLogger($this->logger); $this->parseCSVFile($dictionary, $csv, $this->header); if (!$dictionary->getWords()) { throw new SyntaxException(_('CSVファイルが空です。')); } $metadata = $dictionary->getMetadata(); if (!isset($metadata['@title'])) { if (!is_null($title)) { $metadata['@title'] = $title; } elseif (!is_null($filename)) { $titleFromFilename = $this->getTitleFromFilename($filename); if ($titleFromFilename !== '') { $metadata['@title'] = $titleFromFilename; } } $dictionary->setMetadata($metadata); } return $dictionary; }