fix!: just use upload dir as file upload

This commit is contained in:
mahdahar 2026-04-20 09:27:14 +07:00
parent d91412d495
commit b21327a5d3
3 changed files with 19 additions and 51 deletions

4
.gitignore vendored
View File

@ -126,4 +126,6 @@ _modules/*
/results/ /results/
/phpunit*.xml /phpunit*.xml
/public/.htaccess /public/.htaccess
# CocoIndex Code (ccc)
/.cocoindex_code/

View File

@ -50,16 +50,24 @@ class Activities extends Controller {
return ''; return '';
} }
$attachment = trim(str_replace('\\', '/', $attachment)); $attachment = ltrim(trim(str_replace('\\', '/', $attachment)), '/');
if ($attachment === '') { if ($attachment === '') {
return ''; return '';
} }
if (strpos($attachment, 'file/') === 0) { 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 protected function getAttachmentSubfolder(): string
@ -70,7 +78,7 @@ class Activities extends Controller {
protected function buildAttachmentRelativePath(string $filename): string protected function buildAttachmentRelativePath(string $filename): string
{ {
$filename = basename(str_replace('\\', '/', trim($filename))); $filename = basename(str_replace('\\', '/', trim($filename)));
return $this->getAttachmentSubfolder() . '/' . $filename; return 'upload/' . $this->getAttachmentSubfolder() . '/' . $filename;
} }
protected function normalizeAttachmentList(?string $attachments): string protected function normalizeAttachmentList(?string $attachments): string
@ -1078,7 +1086,7 @@ class Activities extends Controller {
} }
else { else {
$subfolder = $this->getAttachmentSubfolder(); $subfolder = $this->getAttachmentSubfolder();
$uploadDir = FCPATH . "file/$subfolder/"; $uploadDir = FCPATH . "upload/$subfolder/";
if (!is_dir($uploadDir)) { if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true); mkdir($uploadDir, 0755, true);
} }

View File

@ -13,53 +13,11 @@ function resolve_attachment_relative_path($filename) {
} }
$normalized = str_replace('\\', '/', ltrim($filename, '/')); $normalized = str_replace('\\', '/', ltrim($filename, '/'));
$legacyRoot = 'upload/legacy/file'; if (strpos($normalized, 'upload/') === 0) {
return $normalized;
$directCandidates = [];
if (strpos($normalized, 'file/') === 0 || strpos($normalized, 'upload/') === 0) {
$directCandidates[] = $normalized;
} else {
$directCandidates[] = $legacyRoot . '/' . $normalized;
$directCandidates[] = 'upload/' . $normalized;
$directCandidates[] = 'file/' . $normalized;
} }
foreach ($directCandidates as $candidate) { return 'upload/' . $normalized;
$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;
} }
$i = 1; $i = 1;