You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

188 lines
7.6 KiB

<?php
namespace App\Services;
class RenameService
{
public function rename($baseDir = "/Volumes/N/v/美腿骇客 Leghacker&Leglegs-TLoB/", $prefix = "")
{
if (!is_dir($baseDir)) {
return;
}
$this->parseDir($baseDir, $prefix);
}
public function yaRename($baseDir = "/Volumes/intel660p/image/xg/zz/")
{
if (!is_dir($baseDir)) {
return;
}
$this->yaParseDir($baseDir);
}
private function parseDir($dir, $prefix = "")
{
if (!is_dir($dir)) {
return;
}
$files = scandir($dir);
foreach ($files as $file) {
// dump($file);
if ($file == "." || $file == ".." || $file == ".DS_Store" || $file == ".tmp.drivedownload") {
continue;
}
if (is_dir($dir . "/" .$file)) {
$this->parseDir($dir . "/" . $file, $prefix);
}
if (is_file($dir . "/" .$file) && (pathinfo($file, PATHINFO_EXTENSION) == "JPG" || pathinfo($file, PATHINFO_EXTENSION) == "gif" || pathinfo($file, PATHINFO_EXTENSION) == "jpg" || pathinfo($file, PATHINFO_EXTENSION) == "jpeg" || pathinfo($file, PATHINFO_EXTENSION) == "png" || pathinfo($file, PATHINFO_EXTENSION) == "mp4" || pathinfo($file, PATHINFO_EXTENSION) == "avi")) {
echo $dir;
echo "\n";
echo $file;
$dirList = explode("/", $dir);
$filePrefix = $prefix . array_pop($dirList);
if (!str_contains($file, $filePrefix)) {
rename($dir . "/" . $file, $dir. "/". $filePrefix. "-" . $file);
}
echo "\n";
// exit;
}
}
}
private function yaParseDir($dir, $starter = "zz") {
if (!is_dir($dir)) {
return;
}
$files = scandir($dir);
foreach ($files as $file) {
// dump($file);
if ($file == "." || $file == ".." || $file == ".DS_Store" || $file == ".tmp.drivedownload") {
continue;
}
if (is_dir($dir . "/" . $file)) {
$this->yaParseDir($dir . "/" . $file);
}
if (is_file($dir . "/" . $file) && (pathinfo($file, PATHINFO_EXTENSION) == "jpg" || pathinfo($file, PATHINFO_EXTENSION) == "jpeg" || pathinfo($file, PATHINFO_EXTENSION) == "mp4")) {
echo $dir;
echo "\n";
echo $file;
$dirList = explode("/", $dir);
$filePrefix = array_pop($dirList);
$filePrefixList = explode("-", $filePrefix);
if (!str_contains($file, $starter . "-" . $filePrefixList[0])) {
rename($dir . "/" . $file, $dir . "/" . $starter . "-" . $filePrefixList[0] . "-" . $file);
}
echo "\n";
// exit;
}
}
}
public function mvFiles($dir = "")
{
$dirList = [];
$fileList = [];
if (is_dir($dir)){
$files = scandir($dir);
foreach ($files as $file) {
if ($file == "." || $file == "..") {
continue;
}
if (is_dir($dir . $file)) {
$dirList[$file] = $dir . $file;
}
if (is_file($dir . $file) && pathinfo($file, PATHINFO_EXTENSION) == "mp4") {
$fileList[$file] = $dir . $file;
}
}
// foreach ($fileList as $key => $value) {
// $fileKey = explode(" ", $key)[0];
// if (array_key_exists($fileKey, $dirList)) {
// echo $fileKey . "\n";
//// copy($value, $dirList[$fileKey] . "/" . $key);
// shell_exec("mv " . escapeshellarg($value) . " " . escapeshellarg($dirList[$fileKey] . "/" . $fileKey . ".mp4"));
//// exit;
// }
//// exit;
// }
foreach ($fileList as $key => $value) {
$fileMatches = [];
preg_match("#VN.\d+#", $key, $fileMatches);
foreach ($dirList as $dirKey => $dirValue) {
$dirMatches = [];
preg_match("#VN.\d+#", $dirKey, $dirMatches);
if (count($fileMatches) == 1 && count($dirMatches) == 1 && $fileMatches[0] == $dirMatches[0]) {
echo "file is " . $key . " dir is " . $dirKey . "\n";
shell_exec("mv " . escapeshellarg($value) . " " . escapeshellarg($dirList[$dirKey] . "/" . $dirKey . ".mp4"));
}
}
}
print_r($dirList);
print_r($fileList);
}
}
public function splitCustomSizeOfFolder($dir = "", $prefix = "", $size = 500)
{
$files = $this->recordAllFiles($dir, $prefix);
asort($files, SORT_NUMERIC);
$files = array_keys($files);
$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]);
$newFileName = $currentDirName . DIRECTORY_SEPARATOR . $fileInfo['filename'];
$t = 0;
$trueNewFileName = $newFileName . "." . $fileInfo["extension"];
while (is_file($trueNewFileName)) {
echo "file " . $trueNewFileName . " already exists\n";
$trueNewFileName = $newFileName . "_" . $t++ . "." . $fileInfo["extension"];
echo "now rename {$files[$j]} to " . $trueNewFileName . "\n";
}
rename($files[$j], $trueNewFileName);
// 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)) {
$filetime = filectime($dir . DIRECTORY_SEPARATOR . $file);
$trueFiles[$dir . DIRECTORY_SEPARATOR . $file] = $filetime;
}
}
}
// dump($files);
return $trueFiles;
}
}