removed S3 and handles bugs that came with it
This commit is contained in:
parent
59bc78fe47
commit
3a610ba4d7
|
|
@ -72,7 +72,7 @@ export default class extends Controller {
|
||||||
formatterParams: {
|
formatterParams: {
|
||||||
height: "50px",
|
height: "50px",
|
||||||
width: "50px",
|
width: "50px",
|
||||||
urlPrefix: "",
|
urlPrefix: "/",
|
||||||
urlSuffix: "",
|
urlSuffix: "",
|
||||||
},
|
},
|
||||||
width: 100,
|
width: 100,
|
||||||
|
|
|
||||||
|
|
@ -232,6 +232,7 @@ class OrganizationController extends AbstractController
|
||||||
try {
|
try {
|
||||||
$organization->setIsActive(false);
|
$organization->setIsActive(false);
|
||||||
$organization->setIsDeleted(true);
|
$organization->setIsDeleted(true);
|
||||||
|
$this->organizationsService->deleteLogo($organization);
|
||||||
// Deactivate all associated UsersOrganizations
|
// Deactivate all associated UsersOrganizations
|
||||||
$this->userOrganizationService->deactivateAllUserOrganizationLinks($actingUser, null, $organization);
|
$this->userOrganizationService->deactivateAllUserOrganizationLinks($actingUser, null, $organization);
|
||||||
|
|
||||||
|
|
@ -344,12 +345,12 @@ class OrganizationController extends AbstractController
|
||||||
|
|
||||||
// Map to array
|
// Map to array
|
||||||
$data = array_map(function (Organizations $org) {
|
$data = array_map(function (Organizations $org) {
|
||||||
$picture = $this->awsService->getPublicUrl($_ENV['S3_PORTAL_BUCKET']) . $org->getLogoUrl();
|
|
||||||
return [
|
return [
|
||||||
'id' => $org->getId(),
|
'id' => $org->getId(),
|
||||||
'name' => $org->getName(),
|
'name' => $org->getName(),
|
||||||
'email' => $org->getEmail(),
|
'email' => $org->getEmail(),
|
||||||
'logoUrl' => $picture ?: null,
|
'logoUrl' => $org->getLogoUrl() ?: null,
|
||||||
'active' => $org->isActive(),
|
'active' => $org->isActive(),
|
||||||
'showUrl' => $this->generateUrl('organization_show', ['id' => $org->getId()]),
|
'showUrl' => $this->generateUrl('organization_show', ['id' => $org->getId()]),
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -30,25 +30,86 @@ class OrganizationsService
|
||||||
|
|
||||||
public function handleLogo(Organizations $organization, $logoFile): void
|
public function handleLogo(Organizations $organization, $logoFile): void
|
||||||
{
|
{
|
||||||
|
// 1. Define the destination directory (adjust path as needed, e.g., 'public/uploads/profile_pictures')
|
||||||
|
$destinationDir = 'uploads/organization_logos';
|
||||||
|
|
||||||
|
// 2. Create the directory if it doesn't exist
|
||||||
|
if (!file_exists($destinationDir)) {
|
||||||
|
// 0755 is the standard permission (Owner: read/write/exec, Others: read/exec)
|
||||||
|
if (!mkdir($destinationDir, 0755, true) && !is_dir($destinationDir)) {
|
||||||
|
throw new \RuntimeException(sprintf('Directory "%s" was not created', $destinationDir));
|
||||||
|
}
|
||||||
|
}
|
||||||
$extension = $logoFile->guessExtension();
|
$extension = $logoFile->guessExtension();
|
||||||
|
|
||||||
$customFilename = $organization->getName() . '_' . date('dmyHis') . "." . $extension;
|
// Sanitize the filename to remove special characters/spaces to prevent filesystem errors
|
||||||
|
$safeName = preg_replace('/[^a-zA-Z0-9]/', '', $organization->getName());
|
||||||
|
$customFilename = $safeName . '_' . date('dmyHis') . '.' . $extension;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->awsService->PutDocObj($_ENV['S3_PORTAL_BUCKET'], $logoFile, $customFilename, $extension, 'logo/');
|
// 4. Move the file to the destination directory
|
||||||
$this->loggerService->logAWSAction('Upload organization logo', [
|
// The move() method is standard in Symfony/Laravel UploadedFile objects
|
||||||
'organization_id' => $organization->getId(),
|
$logoFile->move($destinationDir, $customFilename);
|
||||||
'filename' => $customFilename,
|
|
||||||
'bucket' => $_ENV['S3_PORTAL_BUCKET'],
|
// 5. Update the user entity with the relative path
|
||||||
|
// Ensure you store the path relative to your public folder usually
|
||||||
|
$organization->setLogoUrl($destinationDir . '/' . $customFilename);
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// 6. Log the critical error as requested
|
||||||
|
$this->loggerService->logError('File upload failed',[
|
||||||
|
'target_organization_id' => $organization->getId(),
|
||||||
|
'message' => $e->getMessage(),
|
||||||
|
'file_name' => $customFilename,
|
||||||
]);
|
]);
|
||||||
$organization->setLogoUrl('logo/' . $customFilename);
|
|
||||||
} catch (FileException $e) {
|
// Optional: Re-throw the exception if you want the controller/user to know the upload failed
|
||||||
$this->loggerService->logError('Failed to upload organization logo to S3', [
|
throw new FileException('File upload failed.');
|
||||||
'organization_id' => $organization->getId(),
|
}
|
||||||
'error' => $e->getMessage(),
|
}
|
||||||
'bucket' => $_ENV['S3_PORTAL_BUCKET'],
|
|
||||||
|
public function deleteLogo(Organizations $organization): void
|
||||||
|
{
|
||||||
|
// 1. Get the current picture path from the user entity
|
||||||
|
$currentPicturePath = $organization->getLogoUrl();
|
||||||
|
|
||||||
|
// If the user doesn't have a picture, simply return (nothing to delete)
|
||||||
|
if (!$currentPicturePath) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 2. Check if the file exists on the server before trying to delete
|
||||||
|
// Note: Ensure $currentPicturePath is relative to the script execution or absolute.
|
||||||
|
if (file_exists($currentPicturePath)) {
|
||||||
|
|
||||||
|
// 3. Delete the file
|
||||||
|
if (!unlink($currentPicturePath)) {
|
||||||
|
throw new \Exception(sprintf('Could not unlink "%s"', $currentPicturePath));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Optional: Log a warning if the DB had a path but the file was missing
|
||||||
|
$this->loggerService->logError('File not found on disk during deletion request', [
|
||||||
|
'target_organization_id' => $organization->getId(),
|
||||||
|
'missing_path' => $currentPicturePath
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Update the user entity to remove the reference
|
||||||
|
$organization->setLogoUrl("");
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// 5. Log the critical error
|
||||||
|
// We log it, but strictly speaking, we might still want to nullify the DB
|
||||||
|
// if the file is corrupted/un-deletable to prevent broken images on the frontend.
|
||||||
|
$this->loggerService->logError('File deletion failed', [
|
||||||
|
'target_organization_id' => $organization->getId(),
|
||||||
|
'message' => $e->getMessage(),
|
||||||
|
'file_path' => $currentPicturePath,
|
||||||
]);
|
]);
|
||||||
throw new FileException('Failed to upload logo to S3: ' . $e->getMessage());
|
|
||||||
|
// Re-throw if you want to stop execution/show error to user
|
||||||
|
throw new \RuntimeException('Unable to remove profile picture.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
<div class="col d-flex justify-content-between align-items-center">
|
<div class="col d-flex justify-content-between align-items-center">
|
||||||
<div class="d-flex ">
|
<div class="d-flex ">
|
||||||
{% if organization.logoUrl %}
|
{% if organization.logoUrl %}
|
||||||
<img src="{{ aws_url ~ organization.logoUrl }}" alt="Organization logo"
|
<img src="{{ asset(organization.logoUrl) }}" alt="Organization logo"
|
||||||
class="rounded-circle" style="width:40px; height:40px;">
|
class="rounded-circle" style="width:40px; height:40px;">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<h1 class="mb-4 ms-3">{{ organization.name|title }} - Dashboard</h1>
|
<h1 class="mb-4 ms-3">{{ organization.name|title }} - Dashboard</h1>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue