From 8c4591cf903636a1b890fdef475cb10913787667 Mon Sep 17 00:00:00 2001 From: shixuesen Date: Mon, 6 Dec 2021 23:40:51 +0800 Subject: [PATCH] add rename service --- app/Services/RenameService.php | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/app/Services/RenameService.php b/app/Services/RenameService.php index 228eebf..f6a88a4 100644 --- a/app/Services/RenameService.php +++ b/app/Services/RenameService.php @@ -128,4 +128,48 @@ class RenameService } } + public function splitCustomSizeOfFolder($dir = "", $prefix = "", $size = 500) + { + $files = $this->recordAllFiles($dir, $prefix); + $allFileNum = count($files); + $folderNum = ceil(count($files) / $size); + for ($i=0; $i < $folderNum; $i++) { + # code... + $currentDirName = $dir . DIRECTORY_SEPARATOR . $prefix . "_00" .$i; + if (!is_dir($currentDirName)) { + mkdir($currentDirName); + } + for ($j=0 + $i * $size; $j < ($i + 1) * $size && $j < $allFileNum; $j++) { + $fileInfo = pathinfo($files[$j]); + if (is_file($currentDirName . DIRECTORY_SEPARATOR . $fileInfo['basename'])) { + echo "file " . $currentDirName . DIRECTORY_SEPARATOR . $fileInfo['basename'] . " already exists\n"; + echo "now rename {$files[$j]} to " . $currentDirName . DIRECTORY_SEPARATOR . $fileInfo['filename'] . "_1." . $fileInfo['extension'] . "\n"; + rename($files[$j], $currentDirName . DIRECTORY_SEPARATOR . $fileInfo['filename'] . "_1." . $fileInfo['extension']); + } else{ + rename($files[$j], $currentDirName . DIRECTORY_SEPARATOR . $fileInfo['basename']); + } + } + } + } + + public function recordAllFiles($dir = "", $prefix) { + $trueFiles = []; + if (is_dir($dir)) { + $files = scandir($dir); + foreach ($files as $file) { + if ($file == "." || $file == ".." || $file == ".DS_Store" || $file == ".tmp.drivedownload" || str_contains($file, $prefix . "_00")) { + continue; + } + if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) { + $trueFiles = array_merge($trueFiles, $this->recordAllFiles($dir . DIRECTORY_SEPARATOR . $file, $prefix)); + } + if (is_file($dir . DIRECTORY_SEPARATOR . $file)) { + $trueFiles[] = $dir . DIRECTORY_SEPARATOR . $file; + } + } + } + // dump($files); + return $trueFiles; + } + }