Cambiar idioma

Despliegue VPS / Apache

Despliega el portal en tu propio VPS con Apache y PHP.

Descargar paquete

Apache 2.4 + PHP 8.2 — Production VPS

A complete deployment configuration for running the Rediafile Developer Portal on a standard Linux VPS with Apache and mod_rewrite.

<?php
/**
 * RediafileClient — minimal PHP cURL SDK for Rediafile Cloud API v2.
 *
 * Features:
 *   - Bearer token authentication with caching
 *   - Multipart file upload (streaming, no full-memory load)
 *   - Remote URL upload
 *   - Folder listing / creation
 */
class RediafileClient
{
    private string $baseUrl = 'https://rediafile.com/cloud/api/v2';
    private ?string \$token  = null;
    private string \$tokenFile;

    public function __construct(
        private string $username,
        private string $password,
        ?string \$tokenCacheFile = null
    ) {
        $this->tokenFile = \$tokenCacheFile ?? sys_get_temp_dir() . '/rediafile_token.json';
    }

    /** Authenticate and cache the token. */
    public function authorize(): string
    {
        $cached = $this->loadCachedToken();
        if ($cached !== null) {
            $this->token = $cached;
            return $cached;
        }

        $resp = $this->sendRequest('/authorize', 'POST', [
            'username' => $this->username,
            'password' => $this->password,
        ], false);

        $data = json_decode($resp, true);
        $this->token = $data['data']['access_token'] ?? '';
        $this->saveCachedToken($this->token, $data['data']['expires_in'] ?? 86400);
        return $this->token;
    }

    /** Upload a local file via multipart/form-data. */
    public function uploadFile(string $filePath, ?string $folderId = null): array
    {
        $this->ensureToken();
        $params = ['file' => new CURLFile($filePath)];
        if ($folderId) {
            $params['folder_id'] = $folderId;
        }
        $resp = $this->sendRequest('/file/upload', 'POST', $params);
        return json_decode($resp, true) ?? [];
    }

    /** Get a signed download URL for a short URL. */
    public function getDownloadUrl(string $shortUrl): array
    {
        $this->ensureToken();
        $resp = $this->sendRequest('/file/download', 'GET', ['short_url' => $shortUrl]);
        return json_decode($resp, true) ?? [];
    }

    /** Queue a remote URL for server-to-server upload. */
    public function uploadFromRemoteUrl(string $url): array
    {
        $this->ensureToken();
        $resp = $this->sendRequest('/file/url_upload_add', 'POST', ['url' => $url]);
        return json_decode($resp, true) ?? [];
    }

    /** List contents of a folder. */
    public function listFolder(string $folderId = 'fld_root'): array
    {
        $this->ensureToken();
        $resp = $this->sendRequest('/folder/listing', 'GET', ['folder_id' => $folderId]);
        return json_decode($resp, true) ?? [];
    }

    /** Create a new folder. */
    public function createFolder(string $name, ?string $parentId = null, ?string $password = null): array
    {
        $this->ensureToken();
        $params = ['folder_name' => $name];
        if ($parentId) $params['parent_id'] = $parentId;
        if ($password) $params['password']  = $password;
        $resp = $this->sendRequest('/folder/create', 'POST', $params);
        return json_decode($resp, true) ?? [];
    }

    /* --- internals --- */

    private function ensureToken(): void
    {
        if ($this->token === null) {
            $this->authorize();
        }
    }

    private function sendRequest(string $path, string $method, array $params = [], bool $withAuth = true): string
    {
        $url    = $this->baseUrl . $path;
        $isGet  = strtoupper($method) === 'GET';
        $fields = $isGet ? http_build_query($params) : $params;

        if ($isGet && $fields) {
            $url .= '?' . $fields;
        }

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));

        $headers = ['Accept: application/json'];
        if ($withAuth && $this->token) {
            $headers[] = 'Authorization: Bearer ' . $this->token;
        }
        if (!$isGet && $fields) {
            // If $fields contains a CURLFile, cURL auto-sets multipart
            $hasFile = false;
            foreach ($fields as $v) {
                if ($v instanceof CURLFile) { $hasFile = true; break; }
            }
            if (!$hasFile) {
                $headers[] = 'Content-Type: application/x-www-form-urlencoded';
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
            } else {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            }
        }
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $resp = curl_exec($ch);
        curl_close($ch);
        return is_string($resp) ? $resp : '';
    }

    private function loadCachedToken(): ?string
    {
        if (!is_file($this->tokenFile)) return null;
        $data = json_decode((string) file_get_contents($this->tokenFile), true);
        if (!is_array($data)) return null;
        if (($data['expires_at'] ?? 0) < time()) return null;
        return $data['token'] ?? null;
    }

    private function saveCachedToken(string \$token, int $expiresIn): void
    {
        file_put_contents($this->tokenFile, json_encode([
            'token'      => \$token,
            'expires_at' => time() + $expiresIn - 60,
        ]));
    }
}

// --- Usage ---
// $client = new RediafileClient('admin', 'secret123');
// $result = $client->uploadFile('/path/to/large-file.zip');
// print_r($result);