Add flysystem to save resources

pull/3016/head
Julio 5 years ago
parent d8d0e7ff5e
commit f8937528a4
  1. 2
      config/packages/liip_imagine.yaml
  2. 2
      config/packages/oneup_flysystem.yaml
  3. 69
      main/inc/lib/document.lib.php
  4. 8
      main/inc/lib/plugin.lib.php
  5. 12
      src/CoreBundle/Controller/ResourceController.php
  6. 48
      src/CoreBundle/Entity/Resource/ResourceFile.php
  7. 4
      src/CoreBundle/Migrations/Schema/V200/Version20.php

@ -10,7 +10,7 @@ liip_imagine:
loaders:
resources:
filesystem:
data_root: '%kernel.root_dir%/../var/upload/resource2'
data_root: '%kernel.root_dir%/../var/upload/resource'
filter_sets:
# Chamilo filters
hotspot_question:

@ -2,7 +2,7 @@ oneup_flysystem:
adapters:
resource_adapter:
local:
directory: '%kernel.project_dir%/var/upload/resource2'
directory: '%kernel.project_dir%/var/upload/resource'
filesystems:
resources:

@ -12,6 +12,7 @@ use Chamilo\UserBundle\Entity\User;
use ChamiloSession as Session;
use Sonata\MediaBundle\Extra\ApiMediaFile;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\File;
/**
* Class DocumentManager
@ -6360,81 +6361,48 @@ class DocumentManager
$em = Database::getManager();
$title = $document->getTitle();
var_dump($title, $document->getId());
// Only create a ResourceFile and Media if there's a file involved
if ($fileType === 'file') {
$mediaManager = Container::$container->get('sonata.media.manager.media');
//$mediaManager->create();
/** @var League\Flysystem\Adapter\Local $mediaManager */
$mediaManager = Container::$container->get('oneup_flysystem.resources_filesystem');
/** @var League\Flysystem\Adapter\Local $mediaManager */
//$mediaManager = Container::$container->get('flysystem');
//$uploadFile = UploadedFile
$resourceFile = $resourceNode->getResourceFile();
if (empty($resourceFile)) {
/** @var \Chamilo\MediaBundle\Entity\Media $media */
$media = $mediaManager->create();
} else {
// If file already exists then we updated it
$media = $resourceFile->getMedia();
}
$media->setName($title);
$fileName = basename($path);
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
$media->setContext('default');
$provider = 'sonata.media.provider.file';
$isImage = in_array($extension, ['jpeg', 'jpg', 'gif', 'png']);
if ($isImage) {
$provider = 'sonata.media.provider.image';
$resourceFile = new ResourceFile();
}
$media->setProviderName($provider);
$media->setEnabled(true);
if ($content instanceof UploadedFile) {
error_log('UploadedFile');
$file = $content;
$media->setSize($file->getSize());
//$file = $content;
//$media->setSize($file->getSize());
} else {
// $path points to a file in the directory
if (file_exists($realPath) && !is_dir($realPath)) {
$media->setSize(filesize($realPath));
if ($isImage) {
$size = getimagesize($realPath);
$media->setWidth($size[0]);
$media->setHeight($size[1]);
}
$file = $realPath;
if ($debug) {
error_log("file exists: $realPath");
}
error_log('file_exists');
} else {
error_log('Content');
// We get the content and create a file
$handle = tmpfile();
fwrite($handle, $content);
$file = new ApiMediaFile($handle);
$file->setMimetype($media->getContentType());
if ($debug) {
error_log('We get content and create a file from handle');
error_log('Size: '.$file->getSize());
error_log('Content type: '.$media->getContentType());
}
$meta = stream_get_meta_data($handle);
//$file = new ApiMediaFile($handle);
error_log($meta['uri']);
$file = new UploadedFile($meta['uri'], $title, null, null, true);
$resourceFile->setFile($file);
}
}
$media->setBinaryContent($file);
$mediaManager->save($media, true);
$resourceFile = $resourceNode->getResourceFile();
if (empty($resourceFile)) {
$resourceFile = new ResourceFile();
$resourceFile->setMedia($media);
} else {
$resourceFile->setMedia($media);
}
$resourceFile->setName($title);
$em->persist($resourceFile);
$resourceNode->setResourceFile($resourceFile);
$em->persist($resourceNode);
$em->flush();
}
// By default visibility is published
@ -6481,6 +6449,7 @@ class DocumentManager
$documentId = $document->getIid();
if ($debug) {
error_log($documentId);
}

@ -125,6 +125,14 @@ class AppPlugin
return in_array($plugin, $list);
}
/**
* @deprecated
*/
public function get_installed_plugins($fromDatabase = true)
{
return $this->getInstalledPlugins($fromDatabase);
}
/**
* @param bool $fromDatabase
*

@ -28,6 +28,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Vich\UploaderBundle\Handler\DownloadHandler;
/**
* Class ResourceController.
@ -406,7 +407,7 @@ class ResourceController extends BaseController implements CourseControllerInter
*
* @return Response
*/
public function showAction(Request $request, CDocumentRepository $documentRepo, FilterService $filterService): Response
public function showAction(Request $request, CDocumentRepository $documentRepo, DownloadHandler $downloadHandler): Response
{
$file = $request->get('file');
$type = $request->get('type');
@ -440,9 +441,16 @@ class ResourceController extends BaseController implements CourseControllerInter
$media = $resourceFile->getMedia();
$format = MediaProviderInterface::FORMAT_REFERENCE;
if ($media) {
if ($resourceFile) {
switch ($type) {
case 'show':
return $downloadHandler->downloadObject($resourceFile, $fileField = 'file');
$file = $resourceFile->getFile();
var_dump($file);
return new BinaryFileResponse($file);
/** @var ImageProvider $provider */
$provider = $this->get('sonata.media.pool')->getProvider($media->getProviderName());
$filename = sprintf(

@ -6,10 +6,12 @@ namespace Chamilo\CoreBundle\Entity\Resource;
use Chamilo\MediaBundle\Entity\Media;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
*
* @Vich\Uploadable
* @ORM\Table(name="resource_file")
*/
class ResourceFile
@ -28,6 +30,27 @@ class ResourceFile
*/
protected $media;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
protected $name;
/**
* @ORM\Column(type="integer")
*
* @var integer
*
*/
protected $size;
/**
* @Vich\UploadableField(mapping="resources", fileNameProperty="name", size="size")
* @var File
*/
protected $file;
// /**
// * @var string
// *
@ -364,4 +387,27 @@ class ResourceFile
return $this;
}
/**
* @return mixed
*/
public function getFile(): ?File
{
return $this->file;
}
/**
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
*
*/
public function setFile(File $file = null): void
{
$this->file = $file;
if (null !== $file) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
}
}

@ -942,6 +942,10 @@ class Version20 extends AbstractMigrationChamilo
$this->addSql("INSERT INTO settings_options (variable, value, display_text) VALUES ('configure_exercise_visibility_in_course','true','Yes')");
$this->addSql("INSERT INTO settings_options (variable, value, display_text) VALUES ('configure_exercise_visibility_in_course','false','No')");
}
//ALTER TABLE resource_file ADD name VARCHAR(255) NOT NULL, ADD size INT NOT NULL;
//ALTER TABLE resource_file_audit ADD name VARCHAR(255) DEFAULT NULL, ADD size INT DEFAULT NULL;
}
/**

Loading…
Cancel
Save