Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     for ($i = 0; $i < self::FAKE_FILE_COUNT; $i += 1) {
         $image = $this->createImage();
         $file = new File();
         $file->setFile($image);
         $file->setSize($image->getSize());
         $file->setOriginalName($image->getClientOriginalName());
         $file->setMimeType($image->getMimeType());
         $file->setFolder($this->getReference('folder'));
         $this->addReference('file-' . $i, $file);
         $manager->persist($file);
     }
     $manager->flush();
 }
Пример #2
0
 /**
  * @Route("/upload/{id}", defaults={"id" = null})
  *
  * @param Folder  $folder
  * @param Request $request
  *
  * @return JsonResponse
  */
 public function uploadAction(Request $request, Folder $folder = null)
 {
     $error_list = [];
     $upload_list = [];
     if ($request->isMethod('POST')) {
         $em = $this->getDoctrine()->getManager();
         $validator = $this->get('validator');
         $translator = $this->get('translator');
         $serializer = new Serializer([$this->get('tg_file.normalizer')]);
         /** @var UploadedFile $uploaded_file */
         foreach ($request->files as $uploaded_file) {
             $file = new File();
             $file->setFile($uploaded_file);
             $file->setSize($uploaded_file->getSize());
             // client original filename could be null, assume current base name by default
             $originalName = $uploaded_file->getClientOriginalName();
             if ($originalName === null) {
                 $originalName = $uploaded_file->getBasename();
             }
             $file->setOriginalName($originalName);
             // mime type could be null, assume generic binary file by default
             $mimeType = $uploaded_file->getMimeType();
             if ($mimeType === null) {
                 $mimeType = 'application/octet-stream';
             }
             $file->setMimeType($mimeType);
             if (null !== $folder) {
                 $file->setFolder($folder);
             }
             $errors = $validator->validate($file);
             if (0 === count($errors)) {
                 $em->persist($file);
                 $upload_list[] = $serializer->normalize($file);
             } else {
                 $name = $uploaded_file->getClientOriginalName();
                 $error_list[$name] = [];
                 /** @var ConstraintViolation $error */
                 foreach ($errors as $error) {
                     $error_list[$name][] = $translator->trans($error->getMessage());
                 }
             }
         }
         $em->flush();
     }
     return new JsonResponse(['errors' => $error_list, 'uploads' => $upload_list]);
 }
Пример #3
0
 /**
  * @param File $file
  *
  * @Route("/download/{id}", name="tg_file_download")
  * @return Response
  */
 public function downloadAction(File $file)
 {
     $fs = $this->container->get('oneup_flysystem.mount_manager')->getFilesystem('media_fs');
     $name = $file->getName();
     return new Response($fs->read($name), 200, ['Cache-Control', 'private', 'Content-Type' => $fs->getMimetype($name), 'Content-Disposition' => 'attachment; filename="' . $file->getOriginalName() . '";', 'Content-length' => $fs->getSize($name)]);
 }
Пример #4
0
 /**
  * @param File $file
  *
  * @return mixed
  */
 private function getExtension(File $file)
 {
     return strtolower(pathinfo($file->getName(), PATHINFO_EXTENSION));
 }