# In-memory

A very simple in-memory filesystem is available which is only accessible via HTTP under `/memfs`. Use the `POST` or `PUT` method with the path to that file. The body of the request contains the contents of the file. No particular encoding or `Content-Type` is required. The file can then be downloaded from the same path.

This filesystem is practical for often changing data (e.g. HLS live stream) in order not to stress the disk or to wear out a flash drive. Also you don't need to setup a RAM drive or similar on your system.

{% tabs %}
{% tab title="PUT/POST" %}

```shell
curl http://127.0.0.1:8080/memfs/path/to/a/file.txt -X PUT -d @somefile.txt
```

{% endtab %}

{% tab title="GET" %}

```shell
curl http://127.0.0.1:8080/memfs/path/to/a/file.txt -o file.txt
```

{% endtab %}

{% tab title="DELETE" %}

```shell
curl http://127.0.0.1:8080/memfs/path/to/a/file.txt -X DELETE
```

{% endtab %}
{% endtabs %}

The returned `Content-Type` is based solely on the file extension. For a list of known mime-types and their extensions see [storage.mime\_types](https://docs.datarhei.com/core/configuration/storage#mime-types) in the configuration.

## Access protection

It is strongly recommended to enable a username/password (HTTP Basic-Auth) protection for any `PUT/POST` and `DELETE` operations on /memfs. `GET` operations are not protected.

{% content-ref url="../../configuration/storage/in-memory" %}
[in-memory](https://docs.datarhei.com/core/configuration/storage/in-memory)
{% endcontent-ref %}

By default HTTP Basic-Auth is enabled with the username "admin" and a random password.

{% openapi src="<https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media&token=dcd2b063-c826-4652-9a98-c265ec41a469>" path="/memfs/{path}" method="get" %}
[doc.json](https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media\&token=dcd2b063-c826-4652-9a98-c265ec41a469)
{% endopenapi %}

{% openapi src="<https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media&token=dcd2b063-c826-4652-9a98-c265ec41a469>" path="/memfs/{path}" method="put" %}
[doc.json](https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media\&token=dcd2b063-c826-4652-9a98-c265ec41a469)
{% endopenapi %}

{% openapi src="<https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media&token=dcd2b063-c826-4652-9a98-c265ec41a469>" path="/memfs/{path}" method="delete" %}
[doc.json](https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media\&token=dcd2b063-c826-4652-9a98-c265ec41a469)
{% endopenapi %}

### Example:

Use these endpoints to, e.g., store HLS chunks and .m3u8 files (in contrast to an actual disk or a ramdisk):

{% code overflow="wrap" %}

```
ffmpeg -f lavfi -re -i testsrc2=size=640x480:rate=25 -c:v libx264 -preset:v ultrafast -r 25 -g 50 -f hls -start_number 0 -hls_time 2 -hls_list_size 6 -hls_flags delete_segments+temp_file+append_list -method PUT -hls_segment_filename http://127.0.0.1:8080/memfs/foobar_%04d.ts -y http://127.0.0.1:8080/memfs/foobar.m3u8
```

{% endcode %}

Then you can play it generally with, e.g.,\
`ffplay http://127.0.0.1:8080/memfs/foobar.m3u8`.

## API

The contents of the `/memfs` are also accessible via the API in the same way as described above, but with the same protection as the API (see [API-Security configuration](https://docs.datarhei.com/core/configuration/api-security#auth)) for all operations. It is also possible to list all files that are currently in the filesystem.

### Create, Update

Example:

{% tabs %}
{% tab title="Curl" %}

```bash
echo 'test' > example.txt && \
curl http://127.0.0.1:8080/api/v3/fs/mem/example.txt \
   -d @example.txt \
   -X PUT
```

{% endtab %}

{% tab title="PyClient" %}

```python
from core_client import Client

client = Client(base_url="http://127.0.0.1:8080")
client.login()

client.v3_fs_put_file(
    name: "mem",
    path: "example.txt",
    data: b"test"
)
```

{% endtab %}

{% tab title="GoClient" %}

```go
import (
    "strings"
    "github.com/datarhei/core-client-go/v16"
)

client, _ := coreclient.New(coreclient.Config{
    Address: "https://127.0.0.1:8080",
})

data := strings.NewReader("test")
err := client.FilesystemAddFile("mem", "/example.txt", data)
```

The contents for the upload has to be provided as an `io.Reader`.
{% endtab %}
{% endtabs %}

After the successful upload the file is available at `/memfs/example.txt` and `/api/v3/fs/mem/example.txt`.

Description:

{% openapi src="<https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media&token=dcd2b063-c826-4652-9a98-c265ec41a469>" path="/api/v3/fs/mem/{path}" method="put" %}
[doc.json](https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media\&token=dcd2b063-c826-4652-9a98-c265ec41a469)
{% endopenapi %}

### Read

#### List all files

Listing all currently stored files is done by calling `/api/v3/fs/mem`. It also accepts the query parameters `pattern`, `sort` (`name,` `size,` or `lastmod`) and `order` (`asc` or `desc`). If none of the parameters are given, all files will be listed sorted by their last modification time in ascending order.

With the `pattern` parameter you can filter the list based on a [glob pattern](https://en.wikipedia.org/wiki/Glob_\(programming\)), with the addition of the `**` placeholder to include multiple subdirectories, e.g. listing all `.ts` file in the root directory has the pattern `/*.ts`, listing all `.ts` file in the whole filesystem has the pattern `/**.ts`.

Example:

{% tabs %}
{% tab title="Curl" %}

```bash
curl "http://127.0.0.1:8080/api/v3/fs/mem?sort=name&order=asc" \
   -X GET
```

{% endtab %}

{% tab title="PyClient" %}

```python
from core_client import Client

client = Client(base_url="http://127.0.0.1:8080")
client.login()

core_memfs_list = client.v3_fs_get_file_list(name="mem")
print(core_memfs_list)
```

{% endtab %}

{% tab title="GoClient" %}

```go
import (
    "fmt"
    "github.com/datarhei/core-client-go/v16"
)

client, _ := coreclient.New(coreclient.Config{
    Address: "https://127.0.0.1:8080",
})

files, err := client.FilesystemList("mem", "/*.", coreclient.SORT_NAME, coreclient.ORDER_ASC)

for _, file := range files {
    fmt.Printf("%+v\n", file)
}
```

{% endtab %}
{% endtabs %}

Description:

{% openapi src="<https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media&token=dcd2b063-c826-4652-9a98-c265ec41a469>" path="/api/v3/fs/mem" method="get" %}
[doc.json](https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media\&token=dcd2b063-c826-4652-9a98-c265ec41a469)
{% endopenapi %}

#### Download a file

For downloading a file you have to specify the complete path and filename. The `Content-Type` will always be `application/data`.

Example:

{% tabs %}
{% tab title="Curl" %}

```bash
curl http://127.0.0.1:8080/api/v3/fs/mem/example.txt \
   -X GET
```

{% endtab %}

{% tab title="PyClient" %}

```python
from core_client import Client

client = Client(base_url="http://127.0.0.1:8080")
client.login()

core_memfs_file = client.v3_fs_get_file(
    name="mem",
    path="example.txt"
)
print(core_memfs_file)
```

{% endtab %}

{% tab title="GoClient" %}

```go
import (
    "github.com/datarhei/core-client-go/v16"
)

client, _ := coreclient.New(coreclient.Config{
    Address: "https://127.0.0.1:8080",
})

data, err := client.FilesystemGetFile("mem", "/example.txt")
defer data.Close()
```

The returned data is an `io.ReadCloser`.
{% endtab %}
{% endtabs %}

Description:

{% openapi src="<https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media&token=dcd2b063-c826-4652-9a98-c265ec41a469>" path="/api/v3/fs/mem/{path}" method="get" %}
[doc.json](https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media\&token=dcd2b063-c826-4652-9a98-c265ec41a469)
{% endopenapi %}

### Link

Linking a file will return a redirect to the linked file. The target of the redirect has to be in the body of the request.

Example:

{% tabs %}
{% tab title="Curl" %}

```bash
curl http://127.0.0.1:8080/api/v3/fs/mem/example2.txt \
   -d 'example.txt'
   -X PATCH
```

{% endtab %}

{% tab title="PyClient" %}

```
Missed.
```

{% endtab %}

{% tab title="GoClient" %}
{% hint style="warning" %}
This is not implemented in the client.
{% endhint %}
{% endtab %}
{% endtabs %}

Description:

{% openapi src="<https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media&token=dcd2b063-c826-4652-9a98-c265ec41a469>" path="/api/v3/fs/mem/{path}" method="patch" %}
[doc.json](https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media\&token=dcd2b063-c826-4652-9a98-c265ec41a469)
{% endopenapi %}

### Delete

For deleting a file you have to specify the complete path and filename.

Example:

{% tabs %}
{% tab title="Curl" %}

```bash
curl http://127.0.0.1:8080/api/v3/fs/mem/example.txt \
   -X DELETE
```

{% endtab %}

{% tab title="PyClient" %}

```python
from core_client import Client

client = Client(base_url="http://127.0.0.1:8080")
client.login()

client.v3_fs_delete_file(
    name="mem",
    path="example.txt"
)
```

{% endtab %}

{% tab title="GoClient" %}

```go
import (
    "github.com/datarhei/core-client-go/v16"
)

client, _ := coreclient.New(coreclient.Config{
    Address: "https://127.0.0.1:8080",
})

err := client.FilesystemDeleteFile("mem", "/example.txt")
```

{% endtab %}
{% endtabs %}

Description:

{% openapi src="<https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media&token=dcd2b063-c826-4652-9a98-c265ec41a469>" path="/api/v3/fs/mem/{path}" method="delete" %}
[doc.json](https://951110271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAQRvnKSkK1SsZB0HeYhh%2Fuploads%2F3duUHhkhwH69BGLlHYga%2Fdoc.json?alt=media\&token=dcd2b063-c826-4652-9a98-c265ec41a469)
{% endopenapi %}

## Configuration

{% content-ref url="../../configuration/storage/in-memory" %}
[in-memory](https://docs.datarhei.com/core/configuration/storage/in-memory)
{% endcontent-ref %}
