# ftp\_download

Downloads a file from the FTP/SFTP server and stores it in DataJet storage. Must be used inside an `ftp_session` block.

```liquid
{% ftp_session host: "ftp.example.com", user: "myuser", password: "mypass" %}
  {% ftp_download from: "/exports/data.csv" as result %}

  {% if result.ok %}
    {% log "Downloaded: " %}
    {% log result.file.name %}
  {% endif %}
{% endftp_session %}
```

#### Syntax

```liquid
{% ftp_download from: "/path/to/file" as result_variable %}
```

#### Parameters

| Parameter | Required | Description                                                |
| --------- | -------- | ---------------------------------------------------------- |
| `from`    | Yes      | Path to the file on the FTP server                         |
| `public`  | No       | Set to `true` to make the file publicly accessible via URL |

#### Result Object

| Property      | Type        | Description                                         |
| ------------- | ----------- | --------------------------------------------------- |
| `ok`          | boolean     | `true` if download succeeded                        |
| `error`       | string/null | Error message if `ok` is `false`, `null` on success |
| `file.name`   | string      | Name of the downloaded file                         |
| `file.public` | boolean     | Whether the file is publicly accessible             |
| `file.url`    | string      | Public URL (only present if `public: true`)         |

Example result:

```json
{
  "ok": true,
  "error": null,
  "file": {
    "name": "data.csv",
    "public": false
  }
}
```

#### Examples

**Basic download:**

```liquid
{% ftp_session host: FTP_HOST, user: FTP_USER, password: FTP_PASSWORD %}
  {% ftp_download from: "/exports/products.csv" as result %}

  {% if result.ok %}
    {% assign file_content = result.file.name | content %}
    {% log file_content %}
  {% else %}
    {% log result.error %}
  {% endif %}
{% endftp_session %}
```

**Download with public URL:**

```liquid
{% ftp_session host: FTP_HOST, user: FTP_USER, password: FTP_PASSWORD %}
  {% ftp_download from: "/reports/monthly.pdf", public: true as result %}

  {% if result.ok %}
    {% log "File available at:" %}
    {% log result.file.url %}
  {% endif %}
{% endftp_session %}
```

**Download and process CSV:**

```liquid
{% ftp_session host: FTP_HOST, user: FTP_USER, password: FTP_PASSWORD, sftp: true %}
  {% ftp_download from: "/data/inventory.csv" as download %}

  {% if download.ok %}
    {% assign csv_content = download.file.name | content %}
    {% assign rows = csv_content | parse_csv %}

    {% for row in rows %}
      {% log row %}
    {% endfor %}
  {% endif %}
{% endftp_session %}
```

**Download multiple files:**

```liquid
{% ftp_session host: FTP_HOST, user: FTP_USER, password: FTP_PASSWORD %}
  {% ftp_list path: "/exports", pattern: "*.csv" as list_result %}

  {% for file in list_result.files %}
    {% ftp_download from: file.path as download %}
    {% if download.ok %}
      {% log "Downloaded: " %}
      {% log file.name %}
    {% endif %}
  {% endfor %}
{% endftp_session %}
```

#### Notes

* Must be used inside an `ftp_session` block
* Consumes 2 credits per download
* Downloaded files are stored in DataJet storage
* Use `| content` filter to read the file contents after download
* File size limits apply (10MB default, 50MB for enhanced stores)
