Using a different storage location for some attachments by using a hook

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bandtank
    Active Community Member
    • Mar 2017
    • 382

    Using a different storage location for some attachments by using a hook

    In my environment, most files are stored in AWS S3. However, I want exported files from report lists to be stored locally. Using the following code, the user can choose a different storage location for certain types of attachments.

    PHP Code:
    File: custom/Espo/Custom/Hooks/Attachment/AttachmentHook.php
    
    <?php
    
    namespace Espo\Custom\Hooks\Attachment;
    
    use Espo\Core\Exceptions\Error;
    use Espo\Core\Utils\Config;
    use Espo\ORM\Entity;
    
    use DateTime;
    
    class AttachmentHook
    {
      public function __construct(
        private Config $config
      ) { }
    
      public function beforeSave(Entity $entity, array $options): void {
        if($entity->get("relatedType") == "Report") {
          $entity->set("storage", $this->config->get("StorageForReportAttachments", "EspoUploadDir"));
        }
      }
    }

    The attachment entity has a few fields you can use to make decisions about where to store files. For my use case, I want all files related to reports to be stored locally instead of in AWS S3. To add flexibility, a new configuration parameter called StorageForReportAttachments is exposed, which can be set in data/config.php if desired.

    Tested on V8.1.1
    Last edited by bandtank; 01-25-2024, 08:58 PM.
Working...