Merge branch 'master' of github.com:chamilo/chamilo-lms

pull/3016/head
Julio Montoya 6 years ago
commit 3e3f6e435d
  1. 5
      composer.json
  2. 1
      config/bundles.php
  3. 2
      config/packages/liip_imagine.yaml
  4. 38
      config/packages/oneup_flysystem.yaml
  5. 13
      config/packages/oneup_uploader.yaml
  6. 13
      config/packages/vich_uploader.yaml
  7. 3
      config/services.yaml
  8. 28
      main/inc/lib/database.lib.php
  9. 69
      main/inc/lib/document.lib.php
  10. 8
      main/inc/lib/plugin.lib.php
  11. 12
      src/CoreBundle/Controller/ResourceController.php
  12. 6
      src/CoreBundle/Controller/ResourceDownloadController.php
  13. 48
      src/CoreBundle/Entity/Resource/ResourceFile.php
  14. 4
      src/CoreBundle/Migrations/Schema/V200/Version20.php
  15. 2
      src/CoreBundle/Resources/views/Resource/upload.html.twig

@ -102,7 +102,7 @@
"stephpy/timeline-bundle": "~3.0",
"stof/doctrine-extensions-bundle": "~1.3",
"sunra/php-simple-html-dom-parser": "~1.5.0",
"sylius/resource-bundle": "~1.5",
"sylius/resource-bundle": "~1.6",
"friendsofsymfony/jsrouting-bundle": "~2.0",
"gedmo/doctrine-extensions": "~2.4",
@ -159,7 +159,8 @@
"simplethings/entity-audit-bundle": "^1.0",
"masterminds/html5": "2.*",
"league/csv": "^9.1",
"brumann/polyfill-unserialize": "^1.0"
"brumann/polyfill-unserialize": "^1.0",
"vich/uploader-bundle": "1.8.*"
},
"config": {
"component-dir": "public/assets"

@ -78,4 +78,5 @@ return [
Symfony\WebpackEncoreBundle\WebpackEncoreBundle::class => ['all' => true],
Liip\ImagineBundle\LiipImagineBundle::class => ['all' => true],
HtmlSanitizer\Bundle\HtmlSanitizerBundle::class => ['all' => true],
Vich\UploaderBundle\VichUploaderBundle::class => ['all' => true],
];

@ -8,7 +8,7 @@ liip_imagine:
web_path:
cache_prefix: '../var/cache/resource'
loaders:
default:
resources:
filesystem:
data_root: '%kernel.root_dir%/../var/upload/resource'
filter_sets:

@ -1,43 +1,11 @@
oneup_flysystem:
adapters:
courses_adapter:
local:
directory: '%kernel.project_dir%/var/courses'
home_adapter:
local:
directory: '%kernel.project_dir%/var/home'
resource_adapter:
local:
directory: '%kernel.project_dir%/var/upload/resource'
# resource_private_adapter:
# local:
# directory: '%kernel.project_dir%/var/upload/resource'
#
# resource_public_adapter:
# local:
# directory: '%kernel.project_dir%/public/upload/resource'
filesystems:
courses:
adapter: courses_adapter
visibility: private
cache: ~
alias: ~
mount: ~
home:
adapter: home_adapter
visibility: private
alias: home_filesystem
resource:
resources:
adapter: resource_adapter
# resource_public:
# adapter: resource_public_adapter
#
# resource_private:
# adapter: resource_private_adapter
mount: resources_fs
visibility: private

@ -1,24 +1,13 @@
# Read the documentation: https://github.com/1up-lab/OneupUploaderBundle/blob/master/Resources/doc/index.md
oneup_uploader:
mappings:
courses:
#frontend: dropzone # or any uploader you use in the frontend
#frontend: blueimp
frontend: custom
storage:
type: flysystem
filesystem: oneup_flysystem.courses_filesystem
root_folder: true
custom_frontend:
class: Chamilo\CoreBundle\Controller\ResourceUploaderController
name: courses
resource:
#frontend: dropzone # or any uploader you use in the frontend
#frontend: blueimp
frontend: custom
storage:
type: flysystem
filesystem: oneup_flysystem.resource_filesystem
filesystem: oneup_flysystem.resources_filesystem
root_folder: true
custom_frontend:
class: Chamilo\CoreBundle\Controller\ResourceUploaderController

@ -0,0 +1,13 @@
vich_uploader:
db_driver: orm
storage: flysystem
mappings:
resources:
uri_prefix: '/'
upload_destination: resources_fs
inject_on_load: true
namer: Vich\UploaderBundle\Naming\Base64Namer
directory_namer:
service: Vich\UploaderBundle\Naming\SubdirDirectoryNamer
options: {chars_per_dir: 1, dirs: 3}

@ -30,6 +30,9 @@ services:
exclude: '../src/PageBundle/{Entity,Migrations,Tests}'
public: true
Vich\UploaderBundle\Naming\SubdirDirectoryNamer:
public: true
Chamilo\ThemeBundle\:
autowire: true
resource: '../src/ThemeBundle/*'

@ -158,21 +158,6 @@ class Database
return $result->rowCount();
}
/**
* Escape MySQL wildchars _ and % in LIKE search.
*
* @param string $text The string to escape
*
* @return string The escaped string
*/
public static function escape_sql_wildcards($text)
{
$text = api_preg_replace("/_/", "\_", $text);
$text = api_preg_replace("/%/", "\%", $text);
return $text;
}
/**
* Escapes a string to insert into the database as text.
*
@ -250,19 +235,6 @@ class Database
return $result->fetch(PDO::FETCH_NUM);
}
/**
* Frees all the memory associated with the provided result identifier.
*
* @return bool|null Returns TRUE on success or FALSE on failure.
* Notes: Use this method if you are concerned about how much memory is being
* used for queries that return large result sets.
* Anyway, all associated result memory is automatically freed at the end of the script's execution.
*/
public static function free_result(Statement $result)
{
$result->closeCursor();
}
/**
* Gets the ID of the last item inserted into the database.
*

@ -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(

@ -35,7 +35,7 @@ class ResourceDownloadController extends BaseController
//$helper = $this->container->get('oneup_uploader.templating.uploader_helper');
//$endpoint = $helper->endpoint('courses');
return $this->render(
'@ChamiloTheme/Resource/upload.html.twig',
'@ChamiloCore/Resource/upload.html.twig',
[
'identifier' => $id,
'type' => $type,
@ -58,7 +58,7 @@ class ResourceDownloadController extends BaseController
{
try {
/** @var Filesystem $fs */
$fs = $this->container->get('oneup_flysystem.courses_filesystem');
$fs = $this->container->get('oneup_flysystem.resources_filesystem');
$file = $request->get('file');
$path = $course.'/document/'.$file;
@ -117,7 +117,7 @@ class ResourceDownloadController extends BaseController
{
try {
/** @var Filesystem $fs */
$fs = $this->container->get('oneup_flysystem.courses_filesystem');
$fs = $this->container->get('oneup_flysystem.resources_filesystem');
$file = $request->get('file');
$path = $course.'/document/'.$file;

@ -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;
}
/**

@ -15,7 +15,7 @@
<input id="fileupload"
type="file"
name="files[]"
data-url="{{ oneup_uploader_endpoint('courses') }}"
data-url="{{ oneup_uploader_endpoint('resource') }}"
multiple
/>
{% endblock %}

Loading…
Cancel
Save