Skip to content

Commit

Permalink
Use file checksum as file name prefix (#907)
Browse files Browse the repository at this point in the history
* Alternative implementation using file checksums
* truncate checksum to 32 char for file names: thus no need for migration

Co-authored-by: ildyria <[email protected]>
  • Loading branch information
kamil4 and ildyria authored Feb 16, 2021
1 parent eb9166f commit edc4c21
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/Actions/Photo/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ public function add(
// Set paths
$this->tmp_name = $file['tmp_name'];
$this->is_uploaded = is_uploaded_file($file['tmp_name']);
$this->photo_Url = md5(microtime()) . $this->extension;
$this->path_prefix = ($this->kind != 'raw') ? 'big/' : 'raw/';
$this->path = Storage::path($this->path_prefix . $this->photo_Url);

// Calculate checksum
$this->photo->checksum = $this->checksum($this->tmp_name);
$duplicate = $this->get_duplicate($this->photo->checksum);
$exists = ($duplicate !== null);

$this->photo_Url = substr($this->photo->checksum, 0, 32) . $this->extension;
$this->path = Storage::path($this->path_prefix . $this->photo_Url);
/*
* ! From here we need to use a Strategy depending if we have
* ! a duplicate
Expand Down
36 changes: 29 additions & 7 deletions app/Actions/Photo/Rotate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Actions\Photo;

use App\Actions\Photo\Extensions\Checksum;
use App\Actions\Photo\Extensions\Constants;
use App\Actions\Photo\Extensions\ImageEditing;
use App\Assets\Helpers;
Expand All @@ -13,6 +14,7 @@

class Rotate
{
use Checksum;
use Constants;
use ImageEditing;

Expand Down Expand Up @@ -59,21 +61,40 @@ public function do(Photo $photo, int $direction)
return false;
}

// We will use new names to avoid problems with image caching.
$new_prefix = md5(microtime());
$url = $photo->url;
$new_url = $new_prefix . Helpers::getExtension($url);
// Generate a temporary name for the rotated file.
$big_folder = Storage::path('big/');
$url = $photo->url;
$path = $big_folder . $url;
$new_path = $big_folder . $new_url;
$extension = Helpers::getExtension($url);
if (
!($new_tmp = tempnam($big_folder, 'lychee')) ||
!@rename($new_tmp, $new_tmp . $extension)
) {
Logs::notice(__METHOD__, __LINE__, 'Could not create a temporary file.');

return false;
}
$new_tmp .= $extension;

// Rotate the original image.
if ($this->imageHandler->rotate($path, ($direction == 1) ? 90 : -90, $new_path) === false) {
if ($this->imageHandler->rotate($path, ($direction == 1) ? 90 : -90, $new_tmp) === false) {
Logs::error(__METHOD__, __LINE__, 'Failed to rotate ' . $path);

return false;
}

// We will use new names to avoid problems with image caching.
$new_prefix = substr($this->checksum($new_tmp), 0, 32);
$new_url = $new_prefix . $extension;
$new_path = $big_folder . $new_url;

// Rename the temporary file, now that we know its final name.
if (!@rename($new_tmp, $new_path)) {
Logs::error(__METHOD__, __LINE__, 'Failed to rename ' . $new_tmp);

return false;
}

$photo->url = $new_url;
$old_width = $photo->width;
$photo->width = $photo->height;
Expand Down Expand Up @@ -140,7 +161,8 @@ public function do(Photo $photo, int $direction)
'small2x' => $photo->small2x,
'medium' => $photo->medium,
'medium2x' => $photo->medium2x,
]);
]
);

return true;
}
Expand Down

0 comments on commit edc4c21

Please sign in to comment.