mirror of
https://github.com/fergalmoran/OpnForm.git
synced 2026-01-28 19:35:24 +00:00
Initial commit
This commit is contained in:
69
app/Service/Storage/StorageFileNameParser.php
Normal file
69
app/Service/Storage/StorageFileNameParser.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Storage;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Used
|
||||
* File can have 2 formats:
|
||||
* - file_name-{uuid}.{ext}
|
||||
* - {uuid}
|
||||
*/
|
||||
class StorageFileNameParser
|
||||
{
|
||||
public ?string $uuid = null;
|
||||
public ?string $fileName = null;
|
||||
public ?string $extension = null;
|
||||
|
||||
public function __construct(string $fileName)
|
||||
{
|
||||
$this->parseFileName($fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* If we have parsed a file name and an extension, we keep the same and append uuid to avoid collisions
|
||||
* Otherwise we just return the uuid
|
||||
* @return string
|
||||
*/
|
||||
public function getMovedFileName(): ?string
|
||||
{
|
||||
if ($this->fileName && $this->extension) {
|
||||
return substr($this->fileName,0,50).'_'.$this->uuid.'.'.$this->extension;
|
||||
}
|
||||
return $this->uuid;
|
||||
}
|
||||
|
||||
private function parseFileName(string $fileName)
|
||||
{
|
||||
if (Str::isUuid($fileName)) {
|
||||
$this->uuid = $fileName;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!str_contains($fileName, '_')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$candidateString = substr($fileName, strrpos($fileName, '_') + 1);
|
||||
if (!str_contains($candidateString, '.')
|
||||
|| !Str::isUuid(substr($candidateString, 0, strpos($candidateString, '.')))) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->uuid = substr($candidateString, 0, strpos($candidateString, '.'));
|
||||
$this->fileName = substr($fileName, 0, strrpos($fileName, '_'));
|
||||
// get everything after the last dot
|
||||
$this->extension = substr($candidateString, strrpos($candidateString, '.') + 1);
|
||||
} catch (\Exception $e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public static function parse(string $fileName): self
|
||||
{
|
||||
return new self($fileName);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user