Skip to content

Commit

Permalink
upload media in /tmp folder before moving them into media folder
Browse files Browse the repository at this point in the history
to give more space to conversion process.

- I keepi the empty test/fn.php file beccause otherwise I got an error. I don't know how to solve this.
  • Loading branch information
vincent-peugnet committed Oct 12, 2024
1 parent 3e172d6 commit 8237680
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 49 deletions.
62 changes: 33 additions & 29 deletions app/class/Modelmedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,20 @@ public function multiupload(string $index, string $target, bool $idclean = false

$from = $_FILES[$index]['tmp_name'][$count];
$count++;
$to = $target . $id . '.' . $extension;
$to = mktmpdir('w-media-upload') . '/' . $id . '.' . $extension;
if (move_uploaded_file($from, $to)) {
$successcount++;
if ($convertimages) {
try {
$media = new Media($to);
if ($media->type() === Media::IMAGE) {
try {
$this->convertimage($media);
} catch (RuntimeException | ImagickException $e) {
$failedconversion++;
}
if ($convertimages && $media->type() === Media::IMAGE) {
$media = $this->convertimage($media);
}
if (rename($media->getabsolutepath(), $target . $media->filename())) {
$successcount++;
}
} catch (Fileexception $e) {
// transfert failed
} catch (RuntimeException | ImagickException $e) {
$failedconversion++;
}
}
}
Expand Down Expand Up @@ -434,38 +436,40 @@ public function rename(string $oldname, string $newname)
* Convert an image to Webp format using Max width and height.
*
* @param Media $media Media to convert. It have to be an image.
* Otherwise a DomainException will be throwned
* Otherwise will thow a DomainException
* @param bool $deleteoriginal Choose if original media file should be deleted. Default is true.
*
* @return Media Converted Media object
*
* @throws RuntimeException If imagick is not installed
* @throws ImagickException If an error occured during IM process
* @throws Filesystemexception If deleting the original media failed
* @throws Filesystemexception If deleting the original media failed, or if file creation failed.
*/
private function convertimage(Media $media): Media
private function convertimage(Media $media, $deleteoriginal = true): Media
{
if ($media->type() !== Media::IMAGE) {
throw new DomainException('Given Media should be an image');
}
if (extension_loaded('imagick')) {
$image = new Imagick($media->getlocalpath());
$image->adaptiveResizeImage(
min($image->getImageWidth(), $this::CONVERSION_MAX_WIDTH),
min($image->getImageHeight(), $this::CONVERSION_MAX_HEIGHT),
true
);
$image->setImageFormat('webp');
$image->setImageCompressionQuality($this::CONVERSION_QUALITY);

$convertmediapath = $media->dir() . '/' . $media->getbasefilename() . '.webp';
if ($image->writeImage($convertmediapath)) {
Fs::deletefile($media->getlocalpath());
}
return new Media($convertmediapath);
} else {
throw new RuntimeException('Impossible to convert: missing imagick or gd PHP extension');
if (!extension_loaded('imagick')) {
throw new RuntimeException('Imagick PHP extension is not installed');
}
$image = new Imagick($media->getlocalpath());
$image->adaptiveResizeImage(
min($image->getImageWidth(), $this::CONVERSION_MAX_WIDTH),
min($image->getImageHeight(), $this::CONVERSION_MAX_HEIGHT),
true
);
$image->setImageFormat('webp');
$image->setImageCompressionQuality($this::CONVERSION_QUALITY);

$convertmediapath = $media->dir() . '/' . $media->getbasefilename() . '.webp';
if ($image->writeImage($convertmediapath) && $deleteoriginal) {
Fs::deletefile($media->getlocalpath());
}
return new Media($convertmediapath);
}


/**
* Generate a clickable folder tree based on reccurive array
*/
Expand Down
29 changes: 29 additions & 0 deletions app/fn/fn.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,32 @@ function disk_free_space_ex(string $directory): float
return $dfs;
}
}

/**
* Get system tmp dir without trailing slah
*
* @return string something like `/tmp`
*/
function get_temp_dir()
{
return rtrim(sys_get_temp_dir(), "/");
}

/**
* Create a folder with an auto-generated name, in OS temp directory
*
* @param string $prefix A prefix to suit your case (It is nice to precise that it is related to W)
* @return string Absolute created path without trailing slash
*
* @throws RuntimeException If creation failed
*/
function mktmpdir(string $prefix): string
{
$tmp = get_temp_dir();
$randstr = dechex(mt_rand() % (2 << 16));
$path = "$tmp/$prefix-$randstr";
if (!mkdir($path)) {
throw new RuntimeException("cannot create tmp dir '$path'");
}
return $path;
}
2 changes: 1 addition & 1 deletion app/view/templates/mediamenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<div>
<input type="hidden" name="convertimages" value="0">
<input type="checkbox" name="convertimages" id="convertimages" value="1" checked>
<label for="convertimages">optimize images</label>
<label for="convertimages">optimize images for the Web</label>
</div>

<input type="hidden" name="dir" value="<?= $mediaopt->dir() ?>">
Expand Down
2 changes: 1 addition & 1 deletion tests/Servicerenderv1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Servicerenderv1Test extends TestCase

public static function setUpBeforeClass(): void
{
self::$tmpdir = mktempdir("w-cms-test-servicerenderv1");
self::$tmpdir = mktmpdir("w-cms-test-servicerenderv1");
}

public function setUp(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Servicerenderv2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Servicerenderv2Test extends TestCase

public static function setUpBeforeClass(): void
{
self::$tmpdir = mktempdir("w-cms-test-servicerenderv2");
self::$tmpdir = mktmpdir("w-cms-test-servicerenderv2");
}

public function setUp(): void
Expand Down
17 changes: 0 additions & 17 deletions tests/fn.php
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
<?php

/**
* Create a folder with an auto-generated naùe, in OS temp directory
*
* @param string $prefix A prefix to suit your case (It is nice to precise that is is related to W)
* @return string Absolute created path
*/
function mktempdir(string $prefix): string
{
$tmp = sys_get_temp_dir();
$randstr = dechex(mt_rand() % (2 << 16));
$path = "$tmp/$prefix-$randstr";
if (!mkdir($path)) {
throw new LogicException("cannot create tmp dir '$path'");
}
return $path;
}

0 comments on commit 8237680

Please sign in to comment.