> For the complete documentation index, see [llms.txt](https://qpapel.papelship.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://qpapel.papelship.com/documentation/tr-documentation/fonksiyonlar/serverside-files.md).

# Serverside Files

Downloads files from the server securely in-memory, or executes them dynamically.

{% hint style="info" %}
Files are encrypted with AES-256 on the server and transferred over an encrypted channel. They are delivered directly to memory, reducing disk footprint and maximizing security.
{% endhint %}

***

## Signature

### 1. Fetch File (In-Memory Download)

```cpp
int FetchFile(QPCTX c, 
              const char* fileId, 
              const char* licenseKey, 
              unsigned char** outData, 
              int* outLen);
```

### 2. Run File (Download and Execute)

```cpp
int RunFile(QPCTX c, 
            const char* fileId, 
            const char* licenseKey, 
            const char* arguments);
```

***

## Parameters

### FetchFile Parameters

* **`c`** (`QPCTX`): Client context pointer.
* **`fileId`** (`const char*`): 12-digit file access identifier.
* **`licenseKey`** (`const char*`): License key for protected files (empty string if public).
* **`outData`** (`unsigned char**`): Address of a pointer that will receive the allocated buffer containing the file data.
* **`outLen`** (`int*`): Address of an integer that will receive the size of the downloaded file in bytes.

### RunFile Parameters

* **`c`** (`QPCTX`): Client context pointer.
* **`fileId`** (`const char*`): 12-digit file access identifier.
* **`licenseKey`** (`const char*`): License key for protected files (empty string if public).
* **`arguments`** (`const char*`): Command-line arguments to pass to the executed file (empty string if none).

***

## Returns

**Type:** `int`

| Value | Description         |
| ----- | ------------------- |
| `1`   | Operation succeeded |
| `0`   | Operation failed    |

{% hint style="warning" %}
When using `FetchFile`, the buffer pointed to by `outData` is dynamically allocated by the SDK. To prevent memory leaks, you **must** free this memory by calling `qpapel::FreeBytes(outData)` when you are done using it.
{% endhint %}

***

## Basic Example

### Downloading and Saving a File to Disk

```cpp
#include <iostream>
#include <fstream>
#include "qPapelLib.h"

void DownloadFileExample(QPCTX ctx, const std::string& fileId, const std::string& licenseKey) {
    unsigned char* fileData = nullptr;
    int fileLen = 0;

    std::cout << "[Downloading]..." << std::endl;

    if (qpapel::FetchFile(ctx, fileId.c_str(), licenseKey.c_str(), &fileData, &fileLen) && fileData != nullptr) {
        // Save the raw buffer to a file
        std::ofstream out("downloaded_file.exe", std::ios::binary);
        out.write(reinterpret_cast<char*>(fileData), fileLen);
        out.close();

        std::cout << "[SUCCESS] File downloaded successfully (" << fileLen << " bytes)." << std::endl;
        
        // CRITICAL: Free the allocated buffer
        qpapel::FreeBytes(fileData); 
    } else {
        char* err = qpapel::GetLastStatus(ctx);
        std::cerr << "[FAILED] Download failed: " << (err ? err : "Unknown Error") << std::endl;
        if (err) qpapel::FreeString(err);
    }
}
```

### Running a File directly from the Server

```cpp
void RunFileExample(QPCTX ctx, const std::string& fileId, const std::string& licenseKey) {
    std::cout << "[Running File]..." << std::endl;

    if (qpapel::RunFile(ctx, fileId.c_str(), licenseKey.c_str(), "--verbose")) {
        std::cout << "[SUCCESS] File executed successfully." << std::endl;
    } else {
        char* err = qpapel::GetLastStatus(ctx);
        std::cerr << "[FAILED] Execution failed: " << (err ? err : "Unknown Error") << std::endl;
        if (err) qpapel::FreeString(err);
    }
}
```

***

## Use Cases

### Auto-Update Flow

```cpp
void CheckAndInstallUpdate(QPCTX ctx, const std::string& licenseKey) {
    std::string currentVersion = "1.0.0";
    char* latest = qpapel::FetchString(ctx, "version_id", licenseKey.c_str());
    
    if (latest && std::string(latest) > currentVersion) {
        std::cout << "Updating to " << latest << "..." << std::endl;
        qpapel::RunFile(ctx, "updater_id", licenseKey.c_str(), "");
        std::exit(0);
    }
    if (latest) qpapel::FreeString(latest);
}
```

***

## Security Features

* **Memory-Only Delivery**: Files downloaded via `FetchFile` are held entirely in volatile RAM. This prevents simple static inspection on disk and signature-based antivirus triggers until written.
* **Integrity Handshake**: Every block is checked with a server-signed SHA-256 checksum during transmission to prevent man-in-the-middle payload injection.
* **Encrypted Transfer**: Transported over an SSL-equivalent ECDH/AES encrypted TCP connection.
