Skip to content

Commit

Permalink
Merge pull request #120 from XIMDEX/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
FranEG80 authored Jan 8, 2025
2 parents 31cace4 + ec6f451 commit 42e7fff
Show file tree
Hide file tree
Showing 9 changed files with 1,948 additions and 1,585 deletions.
20 changes: 11 additions & 9 deletions backend/app/Http/Controllers/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function exploreCourses(ResouceCategoriesRequest $request)
*/
public function update(DamResource $damResource, UpdateResourceRequest $request)
{
$resource = $this->resourceService->update($damResource, $request->all());
$resource = $this->resourceService->update($damResource, $request->all(),$this->getAvailableResourceSizes());
return (new ResourceResource($resource))
->response()
->setStatusCode(Response::HTTP_OK);
Expand All @@ -196,7 +196,7 @@ public function update(DamResource $damResource, UpdateResourceRequest $request)
public function updateFromXeval(string $xevalId, UpdateResourceRequest $request)
{
$damResource = $resource = DamResource::whereJsonContains('data->description', ['xeval_id' => $xevalId])->first();
$resource = $this->resourceService->updateFromXeval($damResource, $request->all());
$resource = $this->resourceService->updateFromXeval($damResource, $request->all(),$this->getAvailableResourceSizes());
return (new ResourceResource($resource))
->response()
->setStatusCode(Response::HTTP_OK);
Expand All @@ -208,7 +208,8 @@ public function updateFromXeval(string $xevalId, UpdateResourceRequest $request)
*/
public function store(StoreResourceRequest $request)
{
$resource = $this->resourceService->store($request->all());
$resource = $this->resourceService->store( params: $request->all(),
availableSizes: $this->getAvailableResourceSizes());
return response(new ResourceResource($resource))
->setStatusCode(Response::HTTP_OK);
}
Expand Down Expand Up @@ -253,7 +254,8 @@ public function copyGetStatus($copy)

public function storeBatch(Request $request)
{
$resources = $this->resourceService->storeBatch($request->all());
$resources = $this->resourceService->storeBatch( data: $request->all(),
availableSizes: $this->getAvailableResourceSizes());
return (new ResourceCollection($resources))
->response()
->setStatusCode(Response::HTTP_OK);
Expand Down Expand Up @@ -526,8 +528,8 @@ private function getAvailableResourceSizes()
'sizes' => [
'thumbnail' => array('width' => 256, 'height' => 144),
'small' => array('width' => 426, 'height' => 240),
'medium' => array('width' => 1920, 'height' => 1080), //HD
'large' => array('width' => 3840, 'height' => 2160), //4k
'medium' => array('width' => 1280, 'height' => 720),
'large' => array('width' => 1920, 'height' => 1080), //4k 3840x2160 HD 1920x1080
'raw' => 'raw',
'default' => array('width' => 1280, 'height' => 720)
],
Expand Down Expand Up @@ -1000,7 +1002,7 @@ private function createCachedResponse(string $file, StreamedResponse $streamedRe
private function handleImageAndVideoResponse($fileType, $size, $compressed, $mediaFileName, $mediaId, $availableSizes)
{
if ($fileType == 'image' || ($fileType == 'video' && in_array($size, ['medium', 'small', 'thumbnail']))) {
$response = response()->file($compressed->basePath());
$response = response()->file($compressed->origin()->filePath());
$this->setCommonHeaders($response, $mediaFileName, $compressed);

// if ($fileType == 'image') {
Expand All @@ -1021,10 +1023,10 @@ private function setCommonHeaders($response, $mediaFileName, $compressed)
$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');

$etag = md5_file($compressed->basePath());
$etag = md5_file($compressed->origin()->filePath());
$response->setEtag($etag);

$lastModified = filemtime($compressed->basePath());
$lastModified = filemtime($compressed->origin()->filePath());
$response->setLastModified(Carbon::createFromTimestamp($lastModified));
}

Expand Down
67 changes: 63 additions & 4 deletions backend/app/Http/Controllers/SemanticController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function store(StoreResourceRequest $request)
*/
public function update(DamResource $damResource, UpdateResourceRequest $request)
{
$resource = $this->resourceService->update($damResource, $request->all());
$resource = $this->resourceService->update($damResource, $request->all(), $this->getAvailableResourceSizes());
return (new ResourceResource($resource))
->response()
->setStatusCode(Response::HTTP_OK);
Expand All @@ -102,7 +102,7 @@ public function update(DamResource $damResource, UpdateResourceRequest $request)
*/
public function patch(DamResource $damResource, PatchResourceRequest $request)
{
$resource = $this->resourceService->patch($damResource, $request->all());
$resource = $this->resourceService->patch($damResource, $request->all(), $this->getAvailableResourceSizes());
return (new ResourceResource($resource))
->response()
->setStatusCode(Response::HTTP_OK);
Expand Down Expand Up @@ -131,7 +131,7 @@ public function updateWithEnhancement(DamResource $damResource, Request $request
$semanticRequest['text'] = $damResourceData->description->body;
$enhanced = $this->semanticService->updateWithEnhance($damResourceData->description, $semanticRequest);
$semanticRequest['data'] = json_encode($enhanced['resources']['data']);
$resource = $this->resourceService->patch($damResource, $semanticRequest);
$resource = $this->resourceService->patch($damResource, $semanticRequest, $this->getAvailableResourceSizes());

return (new ResourceResource($resource))
->response()
Expand Down Expand Up @@ -207,7 +207,7 @@ public function enhance(Request $request) {
);
}



/**
* @param Request $request
Expand All @@ -231,4 +231,63 @@ public function enhanceAutomatic(Request $request) {
Response::HTTP_OK
);
}

private function getAvailableResourceSizes()
{
$sizes = [
'image' => [
'allowed_sizes' => ['thumbnail', 'small', 'medium', 'large', 'raw', 'default'],
'sizes' => [
'thumbnail' => array('width' => 256, 'height' => 144),
'small' => array('width' => 426, 'height' => 240),
'medium' => array('width' => 1280, 'height' => 720),
'large' => array('width' => 1920, 'height' => 1080), //4k 3840x2160 HD 1920x1080
'raw' => 'raw',
'default' => array('width' => 1280, 'height' => 720)
],
'qualities' => [
'thumbnail' => 25,
'small' => 25,
'medium' => 50,
'large' => 100,
'raw' => 'raw',
'default' => 90,
],
'error_message' => ''
],
'video' => [
'allowed_sizes' => ['very_low', 'low', 'standard', 'hd', 'raw', 'thumbnail', 'small', 'medium', 'default'],
'sizes_scale' => ['very_low', 'low', 'standard', 'hd'], // Order Lowest to Greatest
'screenshot_sizes' => ['thumbnail', 'small', 'medium'],
'sizes' => [
// 'lowest' => array('width' => 256, 'height' => 144, 'name' => '144p'),
'very_low' => array('width' => 426, 'height' => 240, 'name' => '240p'),
'low' => array('width' => 640, 'height' => 360, 'name' => '360p'),
'standard' => array('width' => 854, 'height' => 480, 'name' => '480p'),
'hd' => array('width' => 1280, 'height' => 720, 'name' => '720p'),
// 'full_hd' => array('width' => 1920, 'height' => 1080, 'name' => '1080p'),
'raw' => 'raw',
//'thumbnail' => 'thumbnail',
'thumbnail' => array('width' => 256, 'height' => 144, 'name' => '144p'),
'small' => array('width' => 426, 'height' => 240, 'name' => '240p'),
'medium' => array('width' => 854, 'height' => 480, 'name' => '480p'),
'default' => 'raw'
],
'qualities' => [
'thumbnail' => 25,
'small' => 25,
'medium' => 50,
'raw' => 'raw',
'default' => 90
],
'error_message' => ''
]
];

foreach ($sizes as $k => $v) {
$sizes[$k]['error_message'] = $this->setErrorMessage($sizes[$k]['allowed_sizes']);
}

return $sizes;
}
}
10 changes: 5 additions & 5 deletions backend/app/Services/Media/MediaSizeImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(string $size, string $path, ImageManager $manager, I
*/
public function save(String $extension)
{
$pathSave = $this->image->dirname . "/__" . $this->size . ".$extension";
$pathSave = dirname($this->image->origin()->filePath()). "/__" . $this->size . ".$extension";

if ($this->size === 'default') {
$pathSave = $this->path;
Expand Down Expand Up @@ -66,7 +66,7 @@ public function imageExists(String $extension)
$result = false;
$path = $this->path;
if ($this->size !== 'default') {
$path = $this->image->dirname . "/__" . $this->size . ".$extension";
$path = dirname($this->image->origin()->filePath()) . "/__" . $this->size . ".$extension";
}
$result = file_exists($path);
return $result;
Expand All @@ -79,9 +79,9 @@ public function imageExists(String $extension)
*/
public function getImage(String $extension)
{
$result = $this->image->dirname . "/__" . $this->size . ".$extension";
$result = dirname($this->image->origin()->filePath()) . "/__" . $this->size . ".$extension";
if ($this->size === "default" || $this->size === "raw") return $this->image; //$result = $this->path;
return $this->manager->make($result);
return $this->manager->read($result);
}

private function getAspectRatio()
Expand Down Expand Up @@ -119,6 +119,6 @@ public function setSizeDefault()

public function pngHasAlpha()
{
return strpos($this->image->encode('png')->getEncoded(), 'tRNS') !== false;
// return strpos($this->image->encode('png')->getEncoded(), 'tRNS') !== false;
}
}
56 changes: 42 additions & 14 deletions backend/app/Services/MediaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
use FFMpeg\FFMpeg;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
use Iman\Streamer\VideoStreamer;
use Intervention\Image\Facades\Image;
use Intervention\Image\ImageManager;
use App\Utils\DamUrlUtil;
use Imagine;
use Iman\Streamer\Video;
use stdClass;
use Intervention\Image\Drivers\Imagick\Driver;



class MediaService
Expand Down Expand Up @@ -137,9 +135,6 @@ private function getValidSizesRange($availableSizes, $originalResolution)
private function downloadVideo($mediaID, $mediaFileName, $mediaPath, $availableSizes, $sizeKey = null, $size = null, $thumbnail = null)
{
return $this->getVideo($mediaID, $mediaFileName, $mediaPath, $availableSizes, $sizeKey, $size, $thumbnail, true);
$video = new Video();
$video->setPath($mediaPath);
return $video;
}

private function previewVideo($mediaID, $mediaFileName, $mediaPath, $availableSizes, $sizeKey = null, $size = null, $thumbnail = null)
Expand Down Expand Up @@ -200,18 +195,41 @@ private function getVideo($mediaID, $mediaFileName, $mediaPath, $availableSizes,

private function getPreviewOrDownload($path, $isDownload)
{
if (!file_exists($path)) {
abort(404, 'File not found');
}

$fileSize = filesize($path);
$fileName = basename($path);

// Determine content type (you might want to expand this based on your file types)
$contentType = 'video/mp4';

if ($isDownload) {
$video = new Video();
$video->setPath($path);
return $video;
// For download
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
} else {
// For preview/streaming
header('Content-Type: ' . $contentType);
header('Content-Disposition: inline; filename="' . $fileName . '"');
}
return VideoStreamer::streamFile($path);

header('Content-Length: ' . $fileSize);
header('Accept-Ranges: bytes');

// Output the file
$file = fopen($path, 'rb');
fpassthru($file);
fclose($file);

exit;
}

private function previewImage($mediaPath, $type = 'raw', $sizes)
{
$manager = new ImageManager(['driver' => 'imagick']);
$image = $manager->make($mediaPath);
$manager = new ImageManager(new Driver());
$image = $manager->read($mediaPath);
$imageProcess = new MediaSizeImage($type, $mediaPath, $manager, $image, $sizes);
$extension = pathinfo($mediaPath, PATHINFO_EXTENSION);
if ($type !== 'raw') {
Expand Down Expand Up @@ -246,7 +264,7 @@ public function saveVideoSnapshot($thumbPath, $videoSourcePath, $sec = 1)
* @param null $files
* @return array|mixed
*/
public function addFromRequest(Model $model, $collection, $customProperties, $files = null, $requestKey = null)
public function addFromRequest(Model $model, $collection, $customProperties, $files = null, $requestKey = null, $availableSizes)
{
$collection = $collection ?? $this->defaultFileCollection;
if (!empty($requestKey) && empty($files)) {
Expand All @@ -270,6 +288,16 @@ public function addFromRequest(Model $model, $collection, $customProperties, $f
$thumbnail = $file_directory . '/' . $media->filename . '__thumb_.png';
$this->saveVideoSnapshot($thumbnail, $mediaPath);
}
if ($fileType === 'image') {
$extension = pathinfo($mediaPath, PATHINFO_EXTENSION);
$manager = new ImageManager(new Driver());
$image = $manager->read($mediaPath);
$image2 = $manager->read($mediaPath);
$thumb = new MediaSizeImage('thumbnail', $mediaPath, $manager, $image, $availableSizes);
$small = new MediaSizeImage('small', $mediaPath, $manager, $image2, $availableSizes);
if ($thumb->checkSize()) $thumb->save($extension);
if ($small->checkSize()) $small->save($extension);
}
return !empty($mediaList) ? end($mediaList) : [];
}

Expand Down
Loading

0 comments on commit 42e7fff

Please sign in to comment.