# `OpenrelikWorkerCommon.FileUtils`
[🔗](https://github.com/hacktobeer/openrelik-worker-common-elixir/blob/v0.1.0/lib/openrelik_worker_common/file_utils.ex#L147)

File helper functions for OpenRelik workers
(Elixir port of `openrelik_worker_common.file_utils`).

# `build_file_tree`

```elixir
@spec build_file_tree(Path.t(), list()) ::
  OpenrelikWorkerCommon.FileUtils.TemporaryDirectory.t() | nil
```

Creates the original file tree structure from a list of `%OutputFile{}` structs.

Returns a `%TemporaryDirectory{}` representing the root path of the file tree, or `nil`
if `files` is empty or contains non-`%OutputFile{}` elements.

# `count_file_lines`

```elixir
@spec count_file_lines(Path.t()) :: non_neg_integer()
```

Counts the number of lines in a file using `wc -l`.

# `create_output_file`

```elixir
@spec create_output_file(String.t(), keyword() | map()) ::
  OpenrelikWorkerCommon.FileUtils.OutputFile.t()
```

Creates and returns an `%OutputFile{}` struct.

## Parameters
* `output_base_path` - The path to the output directory.
* `opts` - Keyword list or map of optional parameters:
  * `:display_name` - The name of the output file (optional, defaults to generated UUID).
  * `:extension` - File extension (optional).
  * `:data_type` - The data type of the output file (optional).
  * `:original_path` - The original path of the file (optional).
  * `:source_file_id` - The `OutputFile` or ID this file belongs to (optional).
  * `:register_in_db` - Whether the mediator should create a DB row for this file (default `true`).
  * `:uuid` - Optional override for the generated UUIDv4 hex string.
  * `:uuid_generator` - Optional `0`-arity function returning a UUID hex string (for testing).

# `delete_file_tree`

```elixir
@spec delete_file_tree(OpenrelikWorkerCommon.FileUtils.TemporaryDirectory.t()) :: :ok
```

Deletes a temporary file tree folder structure created by `build_file_tree/2`.

Raises `ArgumentError` if `root_path` is not a `%TemporaryDirectory{}` struct.

# `get_relative_path`

```elixir
@spec get_relative_path(Path.t()) :: String.t()
```

Converts a full path to a relative path without the root anchor.

# `is_disk_image`

```elixir
@spec is_disk_image(map()) :: boolean()
```

Checks if `inputfile` is a disk image based on its `"display_name"` extension.

Raises `RuntimeError` if `"display_name"` is not present in `inputfile`.

# `uuid4_hex`

```elixir
@spec uuid4_hex() :: String.t()
```

Generates a 32-character lowercase hexadecimal UUIDv4 string (`uuid4().hex`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
