Skip to content

Commit

Permalink
Merge pull request #110 from XIMDEX/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
FranEG80 authored Oct 2, 2024
2 parents 7a75eed + b18e83a commit 3de57ba
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ backend/storage/media-library/*
backend/php7.4.log
backend/storage/semantic/
backend/public/

!backend/public/ViewerPDF/
!backend/public/ViewerPDF/**
79 changes: 73 additions & 6 deletions backend/app/Http/Controllers/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
use App\Models\Copy;
use App\Services\ExternalApis\ScormService;

use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Carbon\Carbon;


class ResourceController extends Controller
{
/**
Expand Down Expand Up @@ -372,11 +377,14 @@ public function deleteCategory(DamResource $damResource, Category $category)
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
* @throws \Exception
*/
public function render($damUrl, $size = null)
public function render($damUrl, $size = 'default')
{
$mediaId = DamUrlUtil::decodeUrl($damUrl);
if (Cache::has("{$mediaId}__{$size}")) {
return Cache::get("{$mediaId}__$size");
$response = Cache::get("{$mediaId}__$size");
if (is_string($response)) {
Cache::delete("{$mediaId}__$size");
}
}
$method = request()->method();
return $this->renderResource($mediaId, $method, $size, null, false);
Expand All @@ -393,6 +401,39 @@ private function renderResource($mediaId, $method = null, $size = null, $renderK
$mimeType = $media->mime_type;
$fileType = explode('/', $mimeType)[0];

if ($fileType == 'image' && $size === 'default') {
$path = explode('storage/app/', $media->getPath())[1];
if (!Storage::exists($path)) {
abort(404);
}

$file = Storage::get($path);
$type = $mimeType;
$lastModified = Storage::lastModified($path);

$response = new StreamedResponse();
$response->setCallback(function () use ($file) {
echo $file;
});

$response->headers->set('Content-Type', $type);
$response->headers->set('Content-Length', strlen($file));
$response->headers->set('Content-Disposition', sprintf('inline; filename="%s"', $mediaFileName));

$maxAge = 3600 * 24 * 7;
$response->headers->set('Cache-Control', 'public, max-age=' . $maxAge . ', immutable');
$response->headers->set('Expires', gmdate('D, d M Y H:i:s', time() + $maxAge) . ' GMT');

$response_cache = new Response(
$file,
Response::HTTP_OK,
$response->headers->all()
);

Cache::put("{$mediaId}__$size", $response_cache);
return $response;
}

if ($fileType == 'video' || $fileType == 'image') {
$sizeValue = $this->getResourceSize($fileType, $size);
$availableSizes = $this->getAvailableResourceSizes();
Expand All @@ -404,12 +445,38 @@ private function renderResource($mediaId, $method = null, $size = null, $renderK

if ($fileType == 'image' || ($fileType == 'video' && in_array($size, ['medium', 'small', 'thumbnail']))) {
$response = response()->file($compressed->basePath());
// $response = $compressed->response('jpeg', $availableSizes[$fileType]['sizes'][$size] === 'raw' ? 100 : $availableSizes[$fileType]['qualities'][$size]);
$response->headers->set('Content-Disposition', sprintf('inline; filename="%s"', $mediaFileName));

$response->headers->set('Cache-Control', 'public, max-age=86400');
$response->headers->set('Expires', gmdate('D, d M Y H:i:s', time() + 86400) . ' GMT');
// Cache::put("{$mediaId}__$size", serialize($response));
$maxAge = 3600 * 24 * 7;
$response->headers->set('Cache-Control', 'public, max-age=' . $maxAge . ', immutable');
$response->headers->set('Expires', gmdate('D, d M Y H:i:s', time() + $maxAge) . ' GMT');

// Añadir ETag
$etag = md5_file($compressed->basePath());
$response->setEtag($etag);

// Añadir Last-Modified
$lastModified = filemtime($compressed->basePath());
$response->setLastModified(Carbon::createFromTimestamp($lastModified));

if ($fileType == 'image') {
$response_cache = $compressed->response('jpeg', $availableSizes[$fileType]['sizes'][$size] === 'raw' ? 100 : $availableSizes[$fileType]['qualities'][$size]);

$response_cache->headers->set('Content-Disposition', sprintf('inline; filename="%s"', $mediaFileName));

$maxAge = 3600 * 24 * 7;
$response_cache->headers->set('Cache-Control', 'public, max-age=' . $maxAge . ', immutable');
$response_cache->headers->set('Expires', gmdate('D, d M Y H:i:s', time() + $maxAge) . ' GMT');

// Añadir ETag
$etag = md5_file($compressed->basePath());
$response_cache->setEtag($etag);

// Añadir Last-Modified
$lastModified = filemtime($compressed->basePath());
$response_cache->setLastModified(Carbon::createFromTimestamp($lastModified));
Cache::put("{$mediaId}__$size", $response_cache);
}
return $response;
}

Expand Down
10 changes: 10 additions & 0 deletions backend/app/Services/ActivityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class ActivityService extends BaseService
public function __construct() {
parent::__construct();
self::$type_service = ResourceType::activity;
self::$array = ['workspaces' => 'Workspace'];
}


public static function handleFacetCard($facets)
{
$facets = parent::handleFacetCard($facets);
$facets = self::addWorkspace( ResourceType::activity,$facets,array_keys(self::$array));

return $facets;
}

public static function handleSchema($schema)
Expand Down
9 changes: 9 additions & 0 deletions backend/app/Services/AssessmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ class AssessmentService extends BaseService
{
public function __construct() {
parent::__construct();
self::$array = ['workspaces' => 'Workspace'];
self::$type_service = ResourceType::activity;
}

public static function handleFacetCard($facets)
{
$facets = parent::handleFacetCard($facets);
$facets = self::addWorkspace(ResourceType::assessment,$facets,array_keys(self::$array));

return $facets;
}
}
41 changes: 40 additions & 1 deletion backend/app/Services/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static function handleSchema($schema)
public static function handleFacetCard($facets)
{
return $facets;

}

public static function handleFacetValues($values, $facet)
Expand Down Expand Up @@ -79,7 +80,6 @@ public static function handleFacetValues($values, $facet)
}
$values->{$name}['route'] = route($route, $opt);
$values->{$name}['route_delete'] = route($route_delete, $opt);

}
}

Expand All @@ -90,4 +90,43 @@ protected static function parseTypeBBDD($type) {
if ($type === 'enum') return 'string';
return $type;
}

protected static function addWorkspace($type,$facets,$array){
foreach ($facets as $index => $facet) {
if (in_array($facet['key'], $array)) {
$facets[$index]['canAdd'] = true;
$resourceName = self::$array[$facet['key']];
if (class_exists("App\\Models\\{$resourceName}")) {
$model = app("App\\Models\\{$resourceName}");
$model_instance = new $model();
}
$fillables = $model_instance->getFillable();
$model_class = $model::class;
$required = defined("{$model_class}::REQUIRED_FILLABLES") ? $model::REQUIRED_FILLABLES : $fillables;

foreach ($required as $key) {
$type = $model_instance->getConnection()->getSchemaBuilder()->getColumnType($model_instance->getTable(), $key);
$field_add = [
'key' => $key,
'label' => str_replace('_', ' ', ucfirst($key)),
'type' => in_array($type, self::TYPES_ALLOWS) ? self::parseTypeBBDD($type) : false
];
if ($facet['key'] === 'categories' && $key === 'type') {
$field_add['value'] = $type;
}
$facets[$index]['fields'][] = $field_add;
}
if ($facet['key'] === 'categories') {
$route = 'v1category.store';
$facets[$index]['route'] = route($route);
}
if ($facet['key'] === 'workspaces') {
$route = 'v1wsp.create';
}

if ($route) $facets[$index]['route'] = route($route);
}
}
return $facets;
}
}
9 changes: 9 additions & 0 deletions backend/app/Services/BookService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ public function __construct(SolrService $solr)
{
parent::__construct();
self::$type_service = ResourceType::book;
self::$array = ['workspaces' => 'Workspace'];
$this->client = $solr->getClient(self::CLIENT_NAME);
$this->solrSerice = $solr;
}

public static function handleFacetCard($facets)
{
$facets = parent::handleFacetCard($facets);
$facets = self::addWorkspace(ResourceType::book,$facets,array_keys(self::$array));

return $facets;
}

private function execute($query)
{
$bookQuery = new BookHandler($query);
Expand Down
1 change: 0 additions & 1 deletion backend/app/Services/Media/MediaSizeImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public function imageExists(){
public function getImage(){
$result = $this->image->dirname."/__".$this->size.".jpg";
if($this->size === "default" ) return $this->image; //$result = $this->path;
$result = str_replace('Clave_B_Fig_02_00.jpg', 'Clave_B_Fig_02_00_edit.jpg', $result);
return $this->manager->make($result);
}

Expand Down
14 changes: 13 additions & 1 deletion backend/app/Services/MultimediaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@
namespace App\Services;

use App\Enums\ResourceType;

class MultimediaService extends BaseService
{
public function __construct() {
public function __construct()
{
parent::__construct();
self::$array = ['workspaces' => 'Workspace'];
self::$type_service = ResourceType::multimedia;
}


public static function handleFacetCard($facets)
{
$facets = parent::handleFacetCard($facets);
$facets = self::addWorkspace(ResourceType::multimedia,$facets,array_keys(self::$array));

return $facets;
}
}
42 changes: 34 additions & 8 deletions backend/app/Services/ResourceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,16 +524,31 @@ public function store(
$wid cannot be null
*/
$wsp = null;
if (isset($params['toWorkspaceId'])) {
$wsp = explode(',', $params['toWorkspaceId']);
}
$paramsData = is_array($params['data']) ? Utils::arrayToObject($params['data']) : $params['data'];
$exceptionStrings = ['notWorkspace' => 'Undefined workspace','notOrganization' => 'The workspace doesn\'t belong to an organization'];
if ($wsp) {
$wsps = [];
foreach ($wsp as $value) {
$_wsp = Workspace::find($value);
if ($_wsp) $wsps[] = $_wsp;
}
}
$wsp = $toWorkspaceId ? Workspace::find($toWorkspaceId) : Workspace::find(Auth::user()->selected_workspace);
if ($wsp) $wsps[] = $wsp;

$name = array_key_exists('name', $params) ? $params["name"] : "";
$type = $fromBatchType ?? ResourceType::fromKey($params["type"])->value;
$idResourceData = ($type == ResourceType::course) ? $params['kakuma_id'] : Str::orderedUuid() ;
$nameResource = $paramsData->description->name ?? $name;

if($wsp === null) throw new Exception($exceptionStrings['notWorkspace']);
if(!$wsp->organization()->first()) throw new Exception($exceptionStrings['notOrganization']);
if (count($wsps) === 0) $wsps[] = Workspace::find(Auth::user()->selected_workspace);
foreach ($wsps as $workspace) {
if(!$workspace->organization()->first()) throw new Exception($exceptionStrings['notOrganization']);
}

$this->setDefaultLanguageIfNeeded($params);

Expand All @@ -556,7 +571,9 @@ public function store(
$newResource = DamResource::create($resource_data);
// $_newResource = $newResource;

$this->setResourceWorkspace($newResource, $wsp);
foreach ($wsps as $workspace) {
$this->setResourceWorkspace($newResource, $workspace);
}
$this->linkCategoriesFromJson($newResource,$paramsData );
$this->linkTagsFromJson($newResource,$paramsData);
$this->saveAssociatedFiles($newResource, $params);
Expand Down Expand Up @@ -720,12 +737,20 @@ public function storeBatch ($data)
{
$collection = ModelsCollection::find($data['collection']);
$organization = $collection->organization()->first();
$wsps = [];
try {
$wsps = json_decode($data['workspaces']);
} catch(\Exception $exc) {}

if($data['create_wsp'] === '1') {
$wsp = $this->workspaceService->create($organization->id, $data['workspace'])->id;
} else {
$wsp = $data['workspace'];
if (!isset($data['workspaces']) && !$data['workspaces']) {
if($data['create_wsp'] === '1') {
$wsp = $this->workspaceService->create($organization->id, $data['workspace'])->id;
} else {
$wsp = $data['workspace'];
}
$wsps[] = $wsp;
}


$genericResourceDescription = array_key_exists('generic', $data) ? json_decode($data['generic'], true) : [];

Expand Down Expand Up @@ -759,8 +784,9 @@ public function storeBatch ($data)
'collection_id' => $collection->id,
'File' => [$file],
'Preview' => $this->searchPreviewImage($data, $name),
'toWorkspaceId' => implode(",", $wsps)
];
$resource = $this->store($params, $wsp, $collection->accept === ResourceType::multimedia ? $type : $collection->accept);
$resource = $this->store($params, null, $collection->accept === ResourceType::multimedia ? $type : $collection->accept);
$createdResources[] = $resource;
}

Expand Down
17 changes: 17 additions & 0 deletions backend/public/viewerPDF/web/viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
<meta name="google" content="notranslate">
<title>PDF.js viewer</title>

<style>
@media print {
html, body {
display: none !important;
-webkit-user-select: none !important; /* Safari */
-ms-user-select: none !important; /* IE 10 and IE 11 */
user-select: none !important; /* Standard syntax */
}
}

html, body {
-webkit-user-select: none !important; /* Safari */
-ms-user-select: none !important; /* IE 10 and IE 11 */
user-select: none !important; /* Standard syntax */
}
</style>

<!-- This snippet is used in production (included from viewer.html) -->
<link rel="resource" type="application/l10n" href="locale/locale.json">
<script src="../build/pdf.js" type="module"></script>
Expand Down

0 comments on commit 3de57ba

Please sign in to comment.