fix: wrong attachment link

This commit is contained in:
mahdahar 2026-04-17 09:44:27 +07:00
parent e68e39b3a8
commit 9023828c45
3 changed files with 85 additions and 10 deletions

View File

@ -43,6 +43,53 @@ class Activities extends Controller {
'PR' => 'Replace - Repair', 'PB' => 'Replace - broken', 'PU' => 'Replace - usage', 'PF' => 'Replace - FSCA', 'B' => 'Borrow', 'R' => 'Return'
);
}
protected function normalizeAttachmentEntry(?string $attachment): string
{
if ($attachment === null) {
return '';
}
$attachment = trim(str_replace('\\', '/', $attachment));
if ($attachment === '') {
return '';
}
if (strpos($attachment, 'file/') === 0) {
$attachment = substr($attachment, 5);
}
return ltrim($attachment, '/');
}
protected function getAttachmentSubfolder(): string
{
return date('Y/m');
}
protected function buildAttachmentRelativePath(string $filename): string
{
$filename = basename(str_replace('\\', '/', trim($filename)));
return $this->getAttachmentSubfolder() . '/' . $filename;
}
protected function normalizeAttachmentList(?string $attachments): string
{
if ($attachments === null) {
return '';
}
$items = array_filter(array_map('trim', explode(',', $attachments)), static function ($item) {
return $item !== '';
});
$normalized = [];
foreach ($items as $item) {
$normalized[] = $this->normalizeAttachmentEntry($item);
}
return implode(',', array_unique(array_filter($normalized)));
}
public function index() {
$data = array();
@ -414,7 +461,7 @@ class Activities extends Controller {
'media' => $this->request->getVar('media'),
'action' => $this->request->getVar('action'),
'subject' => $this->request->getVar('subject'),
'attachment' => $this->request->getVar('attachment'),
'attachment' => $this->normalizeAttachmentList($this->request->getVar('attachment')),
'actdetailid' => $this->request->getVar('actdetailid'),
'acttextid' => $this->request->getVar('acttextid'),
'textvalue' => $this->request->getVar('textvalue'),
@ -809,7 +856,7 @@ class Activities extends Controller {
'media' => $this->request->getVar('media'),
'action' => $this->request->getVar('action'),
'subject' => $this->request->getVar('subject'),
'attachment' => $this->request->getVar('attachment'),
'attachment' => $this->normalizeAttachmentList($this->request->getVar('attachment')),
'actdetailid' => $this->request->getVar('actdetailid'),
'acttextid' => $this->request->getVar('acttextid'),
'textvalue' => $this->request->getVar('textvalue')
@ -1023,15 +1070,30 @@ class Activities extends Controller {
public function upload(){
if ($this->request->getMethod() === 'POST') {
if ( 0 < $_FILES['file']['error'] ) {
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
return $this->response->setJSON([
'status' => 'error',
'message' => 'Upload failed',
'error' => $_FILES['file']['error'],
]);
}
else {
$subfolder = date('Y/m');
$subfolder = $this->getAttachmentSubfolder();
$uploadDir = FCPATH . "file/$subfolder/";
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
move_uploaded_file($_FILES['file']['tmp_name'], $uploadDir . $_FILES['file']['name']);
$filename = basename(str_replace('\\', '/', $_FILES['file']['name']));
if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadDir . $filename)) {
return $this->response->setJSON([
'status' => 'error',
'message' => 'Unable to save upload',
]);
}
return $this->response->setJSON([
'status' => 'success',
'relativePath' => $this->buildAttachmentRelativePath($filename),
'filename' => $filename,
]);
}
}
}
@ -1270,7 +1332,7 @@ class Activities extends Controller {
'startdate' => str_replace("T"," ",($this->request->getPost('open'))),
'enddate' => str_replace("T"," ",($this->request->getPost('close'))),
'activitystatus' => $this->request->getPost('status'),
'attachment' => $this->request->getPost('attachment'),
'attachment' => $this->normalizeAttachmentList($this->request->getPost('attachment')),
];
$model = new ActivitiesModel();
@ -1361,7 +1423,7 @@ class Activities extends Controller {
$bcc = $this->request->getVar('bcc');
$subject = $this->request->getVar('subject');
$message = $this->request->getVar('message');
$attachment = $this->request->getVar('attachment');
$attachment = $this->normalizeAttachmentList($this->request->getVar('attachment'));
// // $attachments = explode(',',$attachment);
// /*

View File

@ -13,11 +13,13 @@ 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;
}
@ -31,10 +33,18 @@ function resolve_attachment_relative_path($filename) {
$basename = basename($normalized);
if ($basename === '') {
return 'upload/' . $normalized;
return $legacyRoot . '/' . $normalized;
}
$roots = ['file', 'upload'];
$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;
@ -49,7 +59,7 @@ function resolve_attachment_relative_path($filename) {
}
}
return 'upload/' . $basename;
return $legacyRoot . '/' . $basename;
}
$i = 1;

View File

@ -65,6 +65,9 @@ uppy.on("complete", (result) => {
for (let i = 0; i < array.length; i++) {
let text = result.successful[i].name;
if (result.successful[i].response && result.successful[i].response.body && result.successful[i].response.body.relativePath) {
text = result.successful[i].response.body.relativePath;
}
arrtext.push(text);
}