ForestAdmin / forest-express

🧱 Dependency of Express Lianas for Forest Admin
GNU General Public License v3.0
72 stars 21 forks source link

How to download remote file from URL in smart action? #1006

Open yosle opened 1 year ago

yosle commented 1 year ago

Expected behavior

In a smart action with a form that has a enum , the user selects an option and execute the action, after data is fetched I want to download a file from a remote url (a cloudinary hosted file) and return the downloaded file as a response

Actual behavior

Not returning the file

Failure Logs

I could not find any documentation about how to do this

Context

//rest of the code in users collection
   {
      name: "Download User Documents",
      type: "single",
      fields: [
        {
          field: 'Download User Documents',
          description: 'Select Document Type',
          type: 'Enum',
          isRequired: true,
          enums: [
            "Carta del Banco",
            "Certificado de Proyecto de Trabajo",
            "Certifico de No Adeudo Fiscal",     
          ],

        }
      ]
    },
...//rest of the code
The action code
```

router.post('/actions/download-user-documents', permissionMiddlewareCreator.smartAction(), (req, res) => {

// Get the values of the input fields entered by the admin user.
const attrs = req.body.data.attributes.values;
let selected_document = attrs['Download User Documents'];
console.log("selected document ", selected_document);

//Logic where I calculate the right file and get the url

res.sendFile(fileName, options, (error) => {
  if (error) { next(error); }

//HOW TO SEND THE FILE BACK IF I ONLY HAVE AN EXTERNAL URL?? }); // res.send({ success: "Accion exitosa selected_document " + selected_document }) });

```