GridFS splits large files into small pieces. Pieces are stored in one collection (fs.chunks), and metadata about a file in another collection (fs.files). When you make a request to a file, GridFS makes a request to the collection with pieces and returns the entire file.
namespace Dennis\UploadBundle\Document; use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; /** * @MongoDB\Document */ class Upload { /** @MongoDB\Id */ private $id; /** @MongoDB\File */ private $file; /** @MongoDB\String */ private $filename; /** @MongoDB\String */ private $mimeType; /** @MongoDB\Date */ private $uploadDate; /** @MongoDB\Int */ private $length; /** @MongoDB\Int */ private $chunkSize; /** @MongoDB\String */ private $md5; public function getFile() { return $this->file; } public function setFile($file) { $this->file = $file; } public function getFilename() { return $this->filename; } public function setFilename($filename) { $this->filename = $filename; } public function getMimeType() { return $this->mimeType; } public function setMimeType($mimeType) { $this->mimeType = $mimeType; } public function getChunkSize() { return $this->chunkSize; } public function getLength() { return $this->length; } public function getMd5() { return $this->md5; } public function getUploadDate() { return $this->uploadDate; } }
namespace Dennis\UploadBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class UploadController extends Controller { public function newAction(Request $request) { $form = $this->createFormBuilder(array()) ->add('upload', 'file') ->getForm(); if ($request->isMethod('POST')) { $form->bind($request); // ... } return array('form' => $form->createView()); } }
use Dennis\UploadBundle\Document\Upload; public function newAction(Request $request) { // ... $data = $form->getData(); /** @var $upload \Symfony\Component\HttpFoundation\File\UploadedFile */ $upload = $data['upload']; $document = new Upload(); $document->setFile($upload->getPathname()); $document->setFilename($upload->getClientOriginalName()); $document->setMimeType($upload->getClientMimeType()); $dm = $this->get('doctrine.odm.mongodb.document_manager'); $dm->persist($document); $dm->flush(); }
/** * @Route("/{id}", name="upload_show") */ public function showAction($id) { $upload = $this->get('doctrine.odm.mongodb.document_manager') ->getRepository('DennisUploadBundle:Upload') ->find($id); if (null === $upload) { throw $this->createNotFoundException(sprintf('Upload with id "%s" could not be found', $id)); } $response = new Response(); $response->headers->set('Content-Type', $upload->getMimeType()); $response->setContent($upload->getFile()->getBytes()); return $response; }
public function showAction($id) { // ... $response = new StreamedResponse(); $response->headers->set('Content-Type', $upload->getMimeType()); $stream = $upload->getFile()->getResource(); $response->setCallback(function () use ($stream) { fpassthru($stream); }); return $response; }
Source: https://habr.com/ru/post/314840/
All Articles