# get()

Canonical URL: https://developers.iagenify.com/pt/sdk/ref/files/get/
Language: pt

`api.files.get(sourceId) → Promise<IaGenifyFile>`

Legge un file con il suo contenuto. Il testo torna come una string, qualsiasi altra cosa como bytes — e l'oggetto è pronto a aggiornare.

Status: GET /api/files/:id · free · content included

## Parâmetros {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `sourceId` | string | obrigatório | De [salve()](/pt/sdk/ref/files/save) or [list()](/pt/sdk/ref/files/list). Manca → il SDK lance. |

## Retorno {#returns}

```javascript
const file = await api.files.get(sourceId);

file.name;       // "report.md"
file.mimeType;   // "text/markdown"
file.content;    // a string, here — a Buffer for binary
file.sourceId;   // set, so update() works straight away
file.location;   // the kind of storage it is in
file.metadata;   // { size, mimeType, originalName }
```

## Corda o octes, decisa per il tipo {#text}

Un file di tipo text-ish torna come una string; tutto il resto come un `Buffer`.

| Tipomime | contenuto |
| --- | --- |
| text/* · json · xml · yaml · csv · markdown · plain | una corda |
| Image/* · audio/* · video/* · pdf · ningun altro | a a buf |

```javascript
const file = await api.files.get(sourceId);

if (typeof file.content === "string") {
  console.log(file.content.slice(0, 200));
} else {
  await fs.writeFile(`./${file.name}`, file.content);
}
```

> **La decisione è il tipo, non i bytes**
>
> Un file di testo salvat senza un mimeType torna come un buffer. Imposta il tipo a create() e questo non sale mai.

## Lee l'intero fascicolo {#whole}

Non c'è range e streaming: il contenuto arriva in una risposta, come testo o come base64 que il SDK decodifica per voi. Per qualcosa di grande, che è memoria da ambele lati — dimeniu-lo prima de recuperar.

```javascript
const files = await api.files.list();
const target = files.find((f) => f.id === sourceId);

if (target.size > 20 * 1024 * 1024) {
  throw new Error(`${target.name} is ${Math.round(target.size / 1e6)} MB — too big to read in one go`);
}

const file = await api.files.get(sourceId);
```

## Erros {#errors}

| Código | Status | Quando |
| --- | --- | --- |
| — (erro lançado localmente) | — | - Non ha stèu passè. |
| file_not_found | 404 | Nessuna lissíduo di questo cont con que id. |
| file_missing_on_disk | 404 | Il record existe e i suoi bytes non. |

> **file_missing_on_disk vale la pena notar, non retest**
>
> Il record e i bytes s'hann spartit. Retesting responde la medesima cosa; il che aiuta è cancellare il record e salvando il file di nuovo.

Un file appartenente a un altro account risponde la stessa `404`: ogni consulta è mirat al llamante.

- [file.update()](/pt/sdk/ref/files/update): L'obiectu retorna pronto per il.
- [list()](/pt/sdk/ref/files/list): Trovare un identità, e un tamaño, prima de ler.

```python
file = api.files.get(source_id)

file.name        # "report.md"
file.mime_type   # "text/markdown"
file.content     # a str, here — bytes for binary
file.source_id   # set, so update() works straight away
file.location    # the kind of storage it is in
file.metadata    # {"size", "mimeType", "originalName"}
```

```python
from pathlib import Path

file = api.files.get(source_id)

if isinstance(file.content, str):
    print(file.content[:200])
else:
    Path(file.name).write_bytes(file.content)
```

```python
files = api.files.list()
target = next(f for f in files if f["id"] == source_id)

if target["size"] > 20 * 1024 * 1024:
    raise ValueError(f"{target['name']} is {round(target['size'] / 1e6)} MB — too big to read in one go")

file = api.files.get(source_id)
```
