Skip to content

Commit

Permalink
Refactor createTempUploadedFile function move into a class
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed Aug 6, 2021
1 parent 9ac6fac commit 6eaa27f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
24 changes: 24 additions & 0 deletions src/CoreBundle/Component/Utils/CreateUploadedFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

/* For licensing terms, see /license.txt */

namespace Chamilo\CoreBundle\Component\Utils;

use Symfony\Component\HttpFoundation\File\UploadedFile;

class CreateUploadedFile
{
public static function fromString(string $fileName, string $mimeType, string $content): UploadedFile
{
/*$handle = tmpfile();
fwrite($handle, $content);
$meta = stream_get_meta_data($handle);*/

$tmpFilename = tempnam(sys_get_temp_dir(), 'resource_file_');
file_put_contents($tmpFilename, $content);

return new UploadedFile($tmpFilename, $fileName, $mimeType, null, true);
}
}
3 changes: 2 additions & 1 deletion src/CoreBundle/Controller/Api/BaseResourceFileAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Chamilo\CoreBundle\Controller\Api;

use Chamilo\CoreBundle\Component\Utils\CreateUploadedFile;
use Chamilo\CoreBundle\Entity\AbstractResource;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\ResourceLink;
Expand Down Expand Up @@ -239,7 +240,7 @@ protected function handleCreateFileRequest(AbstractResource $resource, ResourceR

// Get data in content and create a HTML file.
if (!$fileParsed && $content) {
$uploadedFile = $resourceRepository->createTempUploadedFile($title.'.html', 'text/html', $content);
$uploadedFile = CreateUploadedFile::fromString($title.'.html', 'text/html', $content);
$resource->setUploadFile($uploadedFile);
$fileParsed = true;
}
Expand Down
11 changes: 11 additions & 0 deletions src/CoreBundle/Repository/AssetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Chamilo\CoreBundle\Repository;

use Chamilo\CoreBundle\Component\Utils\CreateUploadedFile;
use Chamilo\CoreBundle\Entity\Asset;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -116,6 +117,16 @@ public function createFromRequest(Asset $asset, array $file): Asset
return $asset;
}

public function createFromString(Asset $asset, string $mimeType, string $content): Asset
{
$file = CreateUploadedFile::fromString($asset->getTitle(), $mimeType, $content);
$asset->setFile($file);
$this->getEntityManager()->persist($asset);
$this->getEntityManager()->flush();

return $asset;
}

public function delete(Asset $asset = null): void
{
if (null !== $asset) {
Expand Down
15 changes: 2 additions & 13 deletions src/CoreBundle/Repository/ResourceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Chamilo\CoreBundle\Component\Resource\Settings;
use Chamilo\CoreBundle\Component\Resource\Template;
use Chamilo\CoreBundle\Component\Utils\CreateUploadedFile;
use Chamilo\CoreBundle\Entity\AbstractResource;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\ResourceFile;
Expand Down Expand Up @@ -220,21 +221,9 @@ public function addFileFromPath(ResourceInterface $resource, string $fileName, s
return null;
}

public function createTempUploadedFile(string $fileName, string $mimeType, string $content): UploadedFile
{
/*$handle = tmpfile();
fwrite($handle, $content);
$meta = stream_get_meta_data($handle);*/

$tmpFilename = tempnam(sys_get_temp_dir(), 'resource_file_');
file_put_contents($tmpFilename, $content);

return new UploadedFile($tmpFilename, $fileName, $mimeType, null, true);
}

public function addFileFromString(ResourceInterface $resource, string $fileName, string $mimeType, string $content, bool $flush = true): ?ResourceFile
{
$file = $this->createTempUploadedFile($fileName, $mimeType, $content);
$file = CreateUploadedFile::fromString($fileName, $mimeType, $content);

return $this->addFile($resource, $file, '', $flush);
}
Expand Down

0 comments on commit 6eaa27f

Please sign in to comment.