Skip to content

Commit

Permalink
Merge pull request #118 from XIMDEX/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
FranEG80 authored Jan 3, 2025
2 parents 3d51cd7 + 867d4a4 commit 31cace4
Show file tree
Hide file tree
Showing 12 changed files with 693 additions and 412 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,23 @@ First, clone the repository from GitHub using the following command:
git clone https://github.com/XIMDEX/xdam.git
```

## 3. Environment Configuration

## 2. Environment Configuration
Create and configure the .env file. To do this, first go to the backend folder. You can use the .env.example file as a base:
```bash
cd backend
cp .env.example .env
nano .env
```

## 3. Install Dependencies

To install the necessary dependencies, run the following command:

```bash
composer install
```

## 4. Database Migration

After setting up your environment variables in the .env file, you need to run database migrations. This step creates the necessary tables in your database according to the defined schema.
Expand Down Expand Up @@ -69,12 +78,6 @@ sudo apt install jpegoptim optipng pngquant gifsicle ffmpeg
sudo npm install -g svgo
```

Then, navigate to the backend directory and install Composer dependencies:
```bash
cd backend
composer install
php artisan migrate
```

## 7. Final Configuration Steps

Expand All @@ -84,4 +87,5 @@ Finally, execute the following commands:
sudo php artisan solrCores:maintenance --action=ALL
sudo php artisan optimize:clear
sudo php artisan passport:install
php artisan db:seed
```
3 changes: 2 additions & 1 deletion backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ SOLR_SCHEMAS_FOLDER="solr_schemas"
SOLR_CORES_VERSION="" # This value will be an integer (2, 3, 4, etc.)
# surrounded by "". If it's empty, it will take ""
# by default.
SOLR_THUMBNAIL="false" # Allow to save thumbnails on solr.

##### VIDEO TOOLS #####
### CAN BE OBTAINED EXECUTING which ffprobe and wich ffmpeg. THESE ARE REQUIRED
Expand Down Expand Up @@ -107,4 +108,4 @@ DAM_FRONT_URL=http://localhost:3000
############## SCORM #######################
SCORM_URL=https://scorm.mhe.ximdex.net/
SCORM_VERSION=''
SCORM_TOKEN='token_scorm'
SCORM_TOKEN='token_scorm'
378 changes: 243 additions & 135 deletions backend/app/Http/Controllers/ResourceController.php

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions backend/app/Http/Resources/MediaResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ class MediaResource extends JsonResource
*/
public function toArray($request)
{
$thumbnailsTypes = ThumbnailTypes::getValues();
/*$thumbnailsTypes = ThumbnailTypes::getValues();
$thumbnails = [];
if ($this->collection_name == MediaType::Preview()->key)
{
foreach($thumbnailsTypes as $thumbnailType)
{
$hasPreview = $this->hasGeneratedConversion($thumbnailType);
$thumbnails[$thumbnailType] = $hasPreview;
$thumbnails[$thumbnailType] = $hasPreview;
}
}
}*/


$parent_id = $this->hasCustomProperty('parent_id') ? $this->getCustomProperty('parent_id') : "";
Expand All @@ -38,7 +38,7 @@ public function toArray($request)
'parent_id' => $parent_id,
'file_name' => $this->file_name,
'mime_type' => $this->mime_type,
'thumbnails' => $thumbnails,
// 'thumbnails' => $thumbnails,
];
}
}
20 changes: 19 additions & 1 deletion backend/app/Http/Resources/Solr/BaseSolrResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use App\Http\Resources\Solr\LOMSolrResource;
use App\Models\Lom;
use App\Models\Lomes;
use App\Services\Media\MediaSizeImage;
use App\Utils\DamUrlUtil;
use App\Utils\Utils;
use Illuminate\Http\Resources\Json\JsonResource;
use Intervention\Image\ImageManager;
use Solarium\Client;

class BaseSolrResource extends JsonResource
Expand Down Expand Up @@ -191,7 +194,7 @@ protected function getLOMValues(string $type = 'lom')
}
}



return $values;
}
Expand All @@ -212,4 +215,19 @@ protected function formatSemanticTags($tags)
}
return $toSolr;
}

protected function imgToBase64() {

$media = $this->getMedia(MediaType::File()->key);
$file = $media->first();
$mediaPath = $file->getPath();
$manager = new ImageManager(['driver' => 'imagick']);
$image = $manager->make($mediaPath);
$image->fit(256, 144, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$base64 = (string) $image->encode('data-url');
return $base64;
}
}
12 changes: 9 additions & 3 deletions backend/app/Http/Resources/Solr/MultimediaSolrResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

class MultimediaSolrResource extends BaseSolrResource
{

public function __construct($resource, $lomSolrClient = null, $lomesSolrClient = null, $toSolr = false)
{
parent::__construct($resource, $lomSolrClient, $lomesSolrClient);

}

protected function getPreviews()
Expand Down Expand Up @@ -88,11 +90,15 @@ protected function getCoreResourceType()
public function toArray($request)
{
$files = $this->getFiles();

$data = json_decode($this->getData());
if (isset($files[0]) && env('SOLR_THUMBNAIL')) {
$data->img = $this->imgToBase64($files[0]);
}
$data = json_encode($data);
return [
'id' => $this->getID(),
'name' => $this->getName(),
'data' => $this->getData(),
'data' => $data,
'active' => $this->getActive(),
'type' => $this->getType(),
'types' => $this->getTypes($files),
Expand All @@ -108,7 +114,7 @@ public function toArray($request)
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'lom' => $this->getLOMValues(),
'lomes' => $this->getLOMValues('lomes')
'lomes' => $this->getLOMValues('lomes'),
];
}
}
4 changes: 2 additions & 2 deletions backend/app/Models/DamResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DamResource extends Model implements HasMedia, TaggableInterface
"id" => "string"
];

public function registerMediaConversions(\Spatie\MediaLibrary\MediaCollections\Models\Media $media = null): void
/* public function registerMediaConversions(\Spatie\MediaLibrary\MediaCollections\Models\Media $media = null): void
{
$this->addMediaConversion(ThumbnailTypes::thumb_64x64)
->width(64)
Expand All @@ -51,7 +51,7 @@ public function registerMediaConversions(\Spatie\MediaLibrary\MediaCollections\M
->width(200)
->height(400)
->performOnCollections(MediaType::Preview()->key);
}
}*/

public function categories(): BelongsToMany
{
Expand Down
189 changes: 102 additions & 87 deletions backend/app/Services/Media/MediaSizeImage.php
Original file line number Diff line number Diff line change
@@ -1,109 +1,124 @@
<?php

namespace App\Services\Media;

use Intervention\Image\Image;
use Intervention\Image\ImageManager;

// JAP ELIMINAR REDUNDANCIA DE ARRAY DE TAMAÑOS con archivo ResourceController. Eliminar entrada para default ya que se trata como large o avif
class MediaSizeImage
{
private array $allowed_sizes = ['thumbnail', 'small', 'medium', 'raw', 'default'];
private array $sizes = [
'thumbnail' => array('width' => 256, 'height' => 144),
'small' => array('width' => 426, 'height' => 240),
'medium' => array('width' => 854, 'height' => 480),
'raw' => 'raw',
'default' => array('width' => 1280, 'height' => 720)
];
private array $qualities = [
'thumbnail' => 25,
'small' => 25,
'medium' => 50,
'raw' => 'raw',
'default' => 90
];
private array $sizes;
private string $size;
private ImageManager $manager;
private Image $image;
private string $path;
private float $height;
private float $width;
public function __construct(string $size, string $path,ImageManager $manager,Image $image)
{
$this->size = $size;
$this->manager = $manager;
$this->image = $image;
$this->path = $path;
$this->height = $this->image->height();
$this->width = $this->image->width();
}
/**
* Save the image.
*
* @return void
*/
public function save(){
$pathSave = $this->image->dirname."/__".$this->size.".jpg";
$aspectRatio = $this->getAspectRatio($this->width / $this->height);
if ($this->size === 'default'){
public function __construct(string $size, string $path, ImageManager $manager, Image $image,array $sizes)
{
$this->sizes = $sizes;
$this->size = $size;
$this->manager = $manager;
$this->image = $image;
$this->path = $path;
$this->height = $this->image->height();
$this->width = $this->image->width();
}
/**
* Save the image.
*
* @return void
*/
public function save(String $extension)
{
$pathSave = $this->image->dirname . "/__" . $this->size . ".$extension";

if ($this->size === 'default') {
$pathSave = $this->path;
$this->image->save($pathSave);
}else{
$this->image->resize($aspectRatio['width'],$aspectRatio['height'])->save($pathSave);
} else {
$aspectRatio = $this->getAspectRatio();
$this->image->resize($aspectRatio['width'], $aspectRatio['height'])->save($pathSave);
}
}
}
/**
* Check the image's size
*
* @return boolean
*/
public function checkSize(){
$result = true;
$widthNew = $this->sizes[$this->size]['width'];
$heightNew = $this->sizes[$this->size]['height'] ;
if ($widthNew >= $this->width && $heightNew >= $this->height ) $result = false;
return $result;
}

/**
* Check if a specific image exits.
*
* @return boolean
*/
public function imageExists(){
$result = false;
$path = $this->path;
if ($this->size !== 'default') {
$path = $this->image->dirname."/__".$this->size.".jpg";
/**
* Check the image's size
*
* @return boolean
*/
public function checkSize()
{
$result = true;
$widthNew = $this->sizes[$this->size]['width'];
$heightNew = $this->sizes[$this->size]['height'];
if ($widthNew >= $this->width && $heightNew >= $this->height) $result = false;
return $result;
}
$result = file_exists($path);
return $result;
}

/**
* Return an image
*
* @return \Intervention\Image\Image
*/
public function getImage(){
$result = $this->image->dirname."/__".$this->size.".jpg";
if($this->size === "default" ) return $this->image; //$result = $this->path;
return $this->manager->make($result);
}

private function getAspectRatio($aspectRatio){
if ($aspectRatio >= 1.0 && $this->height<=$this->sizes[$this->size]['height']) { // Horizontal
$newWidth = $this->sizes[$this->size]['width'];
$newHeight = $newWidth / $aspectRatio;
} else { // Vertical
$newHeight = $this->sizes[$this->size]['height'];
$newWidth = $newHeight * $aspectRatio;

/**
* Check if a specific image exits.
*
* @return boolean
*/
public function imageExists(String $extension)
{
$result = false;
$path = $this->path;
if ($this->size !== 'default') {
$path = $this->image->dirname . "/__" . $this->size . ".$extension";
}
$result = file_exists($path);
return $result;
}
$result = ["height" => $newHeight,"width" => $newWidth];
return $result;
}

public function setSizeDefault(){
$this->size = 'default';
}
/**
* Return an image
*
* @return \Intervention\Image\Image
*/
public function getImage(String $extension)
{
$result = $this->image->dirname . "/__" . $this->size . ".$extension";
if ($this->size === "default" || $this->size === "raw") return $this->image; //$result = $this->path;
return $this->manager->make($result);
}

private function getAspectRatio()
{
$originalWidth = $this->width;
$originalHeight = $this->height;


$targetWidth = $this->sizes[$this->size ]['width'];
$targetHeight = $this->sizes[$this->size ]['height'];


$isVertical = $originalHeight > $originalWidth;

if ($originalWidth <= $targetWidth && $originalHeight <= $targetHeight) {
return ["height" => $originalHeight, "width" => $originalWidth];
}

if ($isVertical) {
$newHeight = $targetWidth;
$newWidth = round($newHeight*$originalWidth / $originalHeight);
} else {
$ratio = $originalWidth / $targetWidth;
$newWidth = $targetWidth;
$newHeight = round($originalHeight / $ratio);
}

return ["height" => $newHeight, "width" => $newWidth];
}

public function setSizeDefault()
{
$this->size = 'default';
}

public function pngHasAlpha()
{
return strpos($this->image->encode('png')->getEncoded(), 'tRNS') !== false;
}
}
Loading

0 comments on commit 31cace4

Please sign in to comment.