Help with sending attachments API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jacao
    Junior Member
    • Mar 2024
    • 21

    #16
    Thanks a lot! Below code that works for me. It returns file content coded to Base64. Hope it will be usefull for other.

    .../espo/custom/Espo/Custom/FormulaFunctions/FileContent.php

    PHP Code:
    <?php
    namespace Espo\Custom\FormulaFunctions;
    
    use Espo\Core\Formula\Exceptions\TooFewArguments;
    use Espo\Core\Formula\Func;
    use Espo\Core\Formula\EvaluatedArgumentList;
    use Espo\Core\FileStorage\Manager;
    use Espo\ORM\EntityManager;
    use Espo\Entities\Attachment;
    
    class FileContent implements Func
    {
    public function __construct(
    private Manager $fileStorageManager,
    private EntityManager $entityManager,
    ) {}
    
    public function process(EvaluatedArgumentList $arguments): string
    {
    if (count($arguments) < 1) {
    throw TooFewArguments::create(1);
    }
    
    $id = $arguments[0];
    $attachment = $this->entityManager->getRDBRepositoryByClass(Attachment::class)->getById($id);
    
    if (!$attachment) {
    throw new \RuntimeException("No attachment $id.");
    }
    
    $content = $this->fileStorageManager->getContents($attachment);
    
    return base64_encode($content);
    }
    }

    .../espo/custom/Espo/Custom/Resources/metadata/app/formula.json

    PHP Code:
    {
    "functionClassNameMap": {
    "fileContent": "Espo\\Custom\\FormulaFunctions\\FileContent"
    },
    "functionList": [
    "__APPEND__",
    {
    "name": "fileContent",
    "insertText": "fileContent(ID)"
    }
    ]
    }
    
    Last edited by jacao; 10-04-2024, 08:30 AM.

    Comment


    • yuri
      yuri commented
      Editing a comment
      Note: 'fileContent' class should be named FileContent, according naming conventions in PHP
  • jacao
    Junior Member
    • Mar 2024
    • 21

    #17
    Thanks. I've corrected code above.

    Comment

    Working...