From b21327a5d3d8a47f1a16a92d33580582ff48f39f Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Mon, 20 Apr 2026 09:27:14 +0700 Subject: [PATCH] fix!: just use upload dir as file upload --- .gitignore | 4 ++- app/Controllers/Activities.php | 18 +++++++++---- app/Views/activities_detail.php | 48 +++------------------------------ 3 files changed, 19 insertions(+), 51 deletions(-) diff --git a/.gitignore b/.gitignore index d174a55..17a4ac1 100644 --- a/.gitignore +++ b/.gitignore @@ -126,4 +126,6 @@ _modules/* /results/ /phpunit*.xml -/public/.htaccess \ No newline at end of file +/public/.htaccess +# CocoIndex Code (ccc) +/.cocoindex_code/ diff --git a/app/Controllers/Activities.php b/app/Controllers/Activities.php index 2202a81..4efb724 100644 --- a/app/Controllers/Activities.php +++ b/app/Controllers/Activities.php @@ -50,16 +50,24 @@ class Activities extends Controller { return ''; } - $attachment = trim(str_replace('\\', '/', $attachment)); + $attachment = ltrim(trim(str_replace('\\', '/', $attachment)), '/'); if ($attachment === '') { return ''; } if (strpos($attachment, 'file/') === 0) { - $attachment = substr($attachment, 5); + $attachment = 'upload/' . substr($attachment, 5); } - return ltrim($attachment, '/'); + if (strpos($attachment, 'upload/legacy/file/') === 0 || strpos($attachment, 'upload/') === 0) { + return $attachment; + } + + if (preg_match('#^\d{4}/\d{2}/#', $attachment) === 1) { + return 'upload/' . $attachment; + } + + return $attachment; } protected function getAttachmentSubfolder(): string @@ -70,7 +78,7 @@ class Activities extends Controller { protected function buildAttachmentRelativePath(string $filename): string { $filename = basename(str_replace('\\', '/', trim($filename))); - return $this->getAttachmentSubfolder() . '/' . $filename; + return 'upload/' . $this->getAttachmentSubfolder() . '/' . $filename; } protected function normalizeAttachmentList(?string $attachments): string @@ -1078,7 +1086,7 @@ class Activities extends Controller { } else { $subfolder = $this->getAttachmentSubfolder(); - $uploadDir = FCPATH . "file/$subfolder/"; + $uploadDir = FCPATH . "upload/$subfolder/"; if (!is_dir($uploadDir)) { mkdir($uploadDir, 0755, true); } diff --git a/app/Views/activities_detail.php b/app/Views/activities_detail.php index eed6064..b8c025d 100644 --- a/app/Views/activities_detail.php +++ b/app/Views/activities_detail.php @@ -13,53 +13,11 @@ function resolve_attachment_relative_path($filename) { } $normalized = str_replace('\\', '/', ltrim($filename, '/')); - $legacyRoot = 'upload/legacy/file'; - - $directCandidates = []; - if (strpos($normalized, 'file/') === 0 || strpos($normalized, 'upload/') === 0) { - $directCandidates[] = $normalized; - } else { - $directCandidates[] = $legacyRoot . '/' . $normalized; - $directCandidates[] = 'upload/' . $normalized; - $directCandidates[] = 'file/' . $normalized; + if (strpos($normalized, 'upload/') === 0) { + return $normalized; } - foreach ($directCandidates as $candidate) { - $absolutePath = FCPATH . str_replace('/', DIRECTORY_SEPARATOR, $candidate); - if (is_file($absolutePath)) { - return $candidate; - } - } - - $basename = basename($normalized); - if ($basename === '') { - return $legacyRoot . '/' . $normalized; - } - - $flatCandidates = [$legacyRoot . '/' . $basename, 'file/' . $basename, 'upload/' . $basename]; - foreach ($flatCandidates as $candidate) { - $absolutePath = FCPATH . str_replace('/', DIRECTORY_SEPARATOR, $candidate); - if (is_file($absolutePath)) { - return $candidate; - } - } - - $roots = [$legacyRoot, 'upload', 'file']; - foreach ($roots as $root) { - $rootPath = FCPATH . $root . DIRECTORY_SEPARATOR; - $pattern = $rootPath . '*' . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . $basename; - $matches = @glob($pattern); - if ($matches !== false) { - foreach ($matches as $match) { - if (is_file($match)) { - $relative = str_replace('\\', '/', substr($match, strlen($rootPath))); - return $root . '/' . $relative; - } - } - } - } - - return $legacyRoot . '/' . $basename; + return 'upload/' . $normalized; } $i = 1;