HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.naniguide.com/app/Http/Controllers/AssetController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use App\Model\Asset;
use App\Services\AssetService;
use Illuminate\Support\Facades\File;
use App\Library\StringHelper;
use App\Library\Storage\StorageEngineResolver;
use App\Library\Storage\AssetPath;

class AssetController extends Controller
{
    // Associated with "public_media" or any other Asset-based route (with {asset_uid})
    public function serveAsset(Request $request)
    {
        // Retrieve from DB UID, returning Asset
        $asset = Asset::findByUid($request->uid);
        $storage = app(StorageEngineResolver::class)->resolve();

        // There is a special public/private check with Asset
        if (!$asset->isPublic()) {
            return response("Access to #{$asset->uid} not allowed", 403);
        }

        if (!$storage->fileExists($asset)) {
            return response("Resource #{$request->uid} not found", 404);
        }

        if ($storage->isRemote()) {
            // Has a remote URL
            return redirect()->away($storage->getRemoteUrl($asset));
        }

        $stream = $storage->readStream($asset);
        return response()->stream(function () use ($stream) {
            fpassthru($stream);
        }, 200, [
            'Content-Type' => $storage->mimeType($asset),
            'Content-Disposition' => "inline; filename=\"{$asset->original_name}\"",
            'Cache-Control' => 'max-age=31536000, public',
        ]);
    }

    // Associated with route("public_asset")
    public function serveAssetPath($dirname, $basename)
    {
        $dirname = StringHelper::base64UrlDecode($dirname);
        $fullPath = join_paths($dirname, $basename);
        $asset = AssetPath::from($fullPath);

        $storage = app(StorageEngineResolver::class)->resolve();

        if (!$storage->fileExists($asset)) {
            return response("Resource #{$fullPath} not found!", 404);
        }

        if ($storage->isRemote()) {
            return redirect()->away($storage->getRemoteUrl($asset));
        }

        $stream = $storage->readStream($asset);
        return response()->stream(function () use ($stream) {
            fpassthru($stream);
        }, 200, [
            'Content-Type' => $storage->mimeType($asset),
            'Content-Disposition' => "inline; filename=\"{$basename}\"",
            'Cache-Control' => 'max-age=31536000, public',
        ]);
    }

    public function download($name)
    {
        $path = storage_path('app/download/' . $name);
        if (File::exists($path)) {
            return response()->download($path);
        } else {
            return response('Download file not found!', 404);
        }
    }

    public function themeAssets(Request $request)
    {
        $theme = \App\Library\StringHelper::base64UrlDecode($request->theme);

        // Shipped themes live in resources/themes/, user-imported themes in storage (local disk)
        $resourcePath = resource_path(join_paths('themes', $theme, $request->path));

        if (!file_exists($resourcePath)) {
            abort(404);
        }

        $ext = strtolower(pathinfo($resourcePath, PATHINFO_EXTENSION));
        $mimeTypes = [
            'js' => 'application/javascript', 'mjs' => 'application/javascript',
            'css' => 'text/css', 'json' => 'application/json',
        ];
        $mimeType = $mimeTypes[$ext] ?? mime_content_type($resourcePath);
        $filename = basename($resourcePath);

        return response()->stream(function () use ($resourcePath) {
            $stream = fopen($resourcePath, 'rb');
            fpassthru($stream);
            fclose($stream);
        }, 200, [
            'Content-Type' => $mimeType,
            'Content-Disposition' => "inline; filename=\"{$filename}\"",
            'Cache-Control' => 'max-age=31536000, public',
        ]);
    }

    // For Customer
    public function uploadMedia(Request $request, AssetService $assetService)
    {
        $file = $request->file('file');
        $asset = $assetService->uploadMedia($request->user()->customer, $file);
        return response()->json([
            'message' => "Upload complete #{$asset->uid}",
            'url' => $asset->getUrl(),
        ], 200);
    }

    // For ADMIN
    public function uploadApplicationMedia(Request $request, AssetService $assetService)
    {
        $file = $request->file('file');
        $asset = $assetService->uploadApplicationMedia($file);
        return response()->json([
            'message' => "Upload complete #{$asset->uid}",
            'url' => $asset->getUrl(),
        ], 200);
    }
}