rename before upload to image hosting server

This commit is contained in:
xiaomlove
2024-12-30 00:35:02 +08:00
parent 5a9f1f1017
commit 3cf7b36ad6
2 changed files with 17 additions and 3 deletions
+16 -2
View File
@@ -23,9 +23,23 @@ abstract class Storage {
abstract function getBaseUrl(): string;
abstract function getDriverName(): string;
public function uploadGetLocation(string $filepath): string
public function uploadGetLocation(string $filepath, string $originalName): string
{
$url = $this->upload($filepath);
$extension = pathinfo($filepath, PATHINFO_EXTENSION);
if (empty($extension)) {
$newFilepath = sprintf("%s/%s", dirname($filepath), trim($originalName));
$moveResult = move_uploaded_file($filepath, $newFilepath);
do_log(sprintf("filepath: %s, newFilepath: %s, moveResult: %s", $filepath, $newFilepath, $moveResult));
if (!$moveResult) {
throw new \Exception("Failed to move uploaded file.");
}
$url = $this->upload($newFilepath);
@unlink($filepath);
@unlink($newFilepath);
} else {
$url = $this->upload($filepath);
@unlink($filepath);
}
return $this->trimBaseUrl($url);
}