/** * Decides if we should download the file instead of generating a preview * * @param File $file * @param bool $animatedPreview * * @return bool */ public function isPreviewRequired($file, $animatedPreview) { $mime = $file->getMimeType(); if ($mime === 'image/svg+xml') { return $this->isSvgPreviewRequired(); } if ($mime === 'image/gif') { return $this->isGifPreviewRequired($file, $animatedPreview); } return true; }
/** * Downloads the requested file * * @param File $file * @param bool $base64Encode * * @return false|array */ public function downloadFile($file, $base64Encode = false) { try { $this->logger->debug("[DownloadService] File to Download: {name}", ['name' => $file->getName()]); $download = ['preview' => $file->getContent(), 'mimetype' => $file->getMimeType()]; if ($base64Encode) { $download['preview'] = $this->encode($download['preview']); } } catch (\Exception $exception) { $download = false; } return $download; }
/** * Downloads the requested file * * @param File $file * @param bool $base64Encode * * @return array|false * @throws NotFoundServiceException */ public function downloadFile($file, $base64Encode = false) { try { $this->logger->debug("[DownloadService] File to Download: {name}", ['name' => $file->getName()]); $download = ['preview' => $file->getContent(), 'mimetype' => $file->getMimeType()]; if ($base64Encode) { $download['preview'] = $this->encode($download['preview']); } return $download; } catch (\Exception $exception) { throw new NotFoundServiceException('There was a problem accessing the file'); } }
/** * Prepares an empty Thumbnail array to send back * * When we can't even get the file information, we send an empty mimeType * * @param File $file * @param int $status * * @return array<string,null|string> */ private function prepareEmptyThumbnail($file, $status) { $thumbnail = []; if ($status !== Http::STATUS_NOT_FOUND) { $mimeType = ''; if ($file) { $mimeType = $file->getMimeType(); } $thumbnail = ['preview' => null, 'mimetype' => $mimeType]; } return $thumbnail; }
/** * index a file * * @param File $file the file to be indexed * @param bool $commit * * @return bool true when something was stored in the index, false otherwise (eg, folders are not indexed) * @throws NotIndexedException when an unsupported file type is encountered */ public function indexFile(File $file, $commit = true) { // we decide how to index on mime type or file extension $mimeType = $file->getMimeType(); $fileExtension = strtolower(pathinfo($file->getName(), PATHINFO_EXTENSION)); // initialize plain lucene document $doc = new Document(); // index content for local files only $storage = $file->getStorage(); if ($storage->isLocal()) { $path = $storage->getLocalFile($file->getInternalPath()); //try to use special lucene document types if ('text/html' === $mimeType) { //TODO could be indexed, even if not local $doc = HTML::loadHTML($file->getContent()); } else { if ('text/' === substr($mimeType, 0, 5) || 'application/x-tex' === $mimeType) { $body = $file->getContent(); if ($body != '') { $doc->addField(Document\Field::UnStored('body', $body)); } } else { if ('application/pdf' === $mimeType) { $doc = Pdf::loadPdf($file->getContent()); // the zend classes only understand docx and not doc files } else { if ($fileExtension === 'docx') { $doc = Document\Docx::loadDocxFile($path); //} else if ('application/msexcel' === $mimeType) { } else { if ($fileExtension === 'xlsx') { $doc = Document\Xlsx::loadXlsxFile($path); //} else if ('application/mspowerpoint' === $mimeType) { } else { if ($fileExtension === 'pptx') { $doc = Document\Pptx::loadPptxFile($path); } else { if ($fileExtension === 'odt') { $doc = Odt::loadOdtFile($path); } else { if ($fileExtension === 'ods') { $doc = Ods::loadOdsFile($path); } else { throw new NotIndexedException(); } } } } } } } } } // Store filecache id as unique id to lookup by when deleting $doc->addField(Document\Field::Keyword('fileId', $file->getId())); // Store document path for the search results $doc->addField(Document\Field::Text('path', $file->getPath(), 'UTF-8')); $doc->addField(Document\Field::unIndexed('mtime', $file->getMTime())); $doc->addField(Document\Field::unIndexed('size', $file->getSize())); $doc->addField(Document\Field::unIndexed('mimetype', $mimeType)); $this->index->updateFile($doc, $file->getId(), $commit); return true; }
/** * @param File $file */ public function addAttachmentFromFiles(File $file) { $part = new Horde_Mime_Part(); $part->setCharset('us-ascii'); $part->setDisposition('attachment'); $part->setName($file->getName()); $part->setContents($file->getContent()); $part->setType($file->getMimeType()); $this->attachments[] = $part; }
/** * @param File $file * @param bool $previewRequired * * @return array <string,mixed> */ private function mockPreviewData($file, $previewRequired) { $mimeType = $previewRequired ? 'image/png' : $file->getMimeType(); $preview = ['preview' => $file->getContent(), 'mimetype' => $mimeType]; return $preview; }
/** * Returns true if the file is of a supported media type and adds it to the array of items to * return * * @todo We could potentially check if the file is readable ($file->stat() maybe) in order to * only return valid files, but this may slow down operations * * @param File $file the file to test * * @return bool */ private function isPreviewAvailable($file) { try { $mimeType = $file->getMimeType(); if (in_array($mimeType, $this->supportedMediaTypes)) { $this->addFileToImagesArray($mimeType, $file); return true; } } catch (\Exception $exception) { return false; } return false; }