Skip to content

Commit

Permalink
downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Feb 2, 2024
1 parent 07b4b15 commit 7113d8e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 12 deletions.
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php

use Cone\Root\Http\Controllers\DashboardController;
use Cone\Root\Http\Controllers\DownloadController;
use Cone\Root\Http\Controllers\ResourceController;
use Illuminate\Support\Facades\Route;

// Dashboard
Route::get('/', DashboardController::class)->name('dashboard');

// Download
Route::get('/download/{medium:uuid}', DownloadController::class)->name('download');

// Resource
Route::get('/{resource}', [ResourceController::class, 'index'])->name('resource.index');
Route::get('/{resource}/create', [ResourceController::class, 'create'])->name('resource.create');
Expand Down
19 changes: 17 additions & 2 deletions src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\View;

class File extends MorphToMany
Expand Down Expand Up @@ -102,14 +103,28 @@ public function resolveDisplay(Model $related): ?string
if (is_null($this->displayResolver)) {
$this->display(function (Medium $related): string {
return $related->isImage
? sprintf('<img src="%s" width="30" height="30">', $related->getUrl($this->displayConversion))
: sprintf('<a href="%s">%s</a>', $related->getUrl(), $related->file_name);
? sprintf('<img src="%s" width="40" height="40" alt="%s">', $related->getUrl($this->displayConversion), $related->name)
: $related->file_name;
});
}

return parent::resolveDisplay($related);
}

/**
* {@inheritdoc}
*/
public function formatRelated(Request $request, Model $model, Model $related): ?string
{
$value = $this->resolveDisplay($related);

if ($related->isImage || ! $this->resolveAbility('view', $request, $model, $related)) {

Check failure on line 121 in src/Fields/File.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Access to an undefined property Illuminate\Database\Eloquent\Model::$isImage.
return $value;
}

return sprintf('<a href="%s" download>%s</a>', URL::signedRoute('root.download', $related), $value);
}

/**
* {@inheritdoc}
*/
Expand Down
28 changes: 18 additions & 10 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,21 +323,29 @@ public function resolveFormat(Request $request, Model $model): ?string
$this->formatResolver = function (Request $request, Model $model): mixed {
$default = $this->getValue($model);

return Collection::wrap($default)->map(function (Model $related) use ($request): mixed {
$resource = Root::instance()->resources->forModel($related);
return Collection::wrap($default)->map(function (Model $related) use ($model, $request): mixed {
return $this->formatRelated($request, $model, $related);
})->filter()->join(', ');
};
}

return parent::resolveFormat($request, $model);
}

$value = $this->resolveDisplay($related);
/**
* Format the related model.
*/
public function formatRelated(Request $request, Model $model, Model $related): ?string
{
$resource = Root::instance()->resources->forModel($related);

if (! is_null($resource) && $related->exists && $request->user()->can('view', $related)) {
$value = sprintf('<a href="%s" data-turbo-frame="_top">%s</a>', $resource->modelUrl($related), $value);
}
$value = $this->resolveDisplay($related);

return $value;
})->join(', ');
};
if (! is_null($resource) && $related->exists && $resource->resolveAbility('view', $request, $related)) {
$value = sprintf('<a href="%s" data-turbo-frame="_top">%s</a>', $resource->modelUrl($related), $value);
}

return parent::resolveFormat($request, $model);
return $value;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Http/Controllers/DownloadController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Cone\Root\Http\Controllers;

use Cone\Root\Models\Medium;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class DownloadController extends Controller
{
/**
* Create a new controller instance.
*/
public function __construct()
{
$this->middleware('signed');
}

/**
* Handle the incoming request.
*/
public function __invoke(Medium $medium): BinaryFileResponse
{
return $medium->download();
}
}
7 changes: 7 additions & 0 deletions src/Interfaces/Models/Medium.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Cone\Root\Interfaces\Models;

use Symfony\Component\HttpFoundation\BinaryFileResponse;

interface Medium
{
/**
Expand All @@ -23,4 +25,9 @@ public function getAbsolutePath(?string $conversion = null): string;
* Get the url to the conversion.
*/
public function getUrl(?string $conversion = null): string;

/**
* Download the medium.
*/
public function download(?string $conversion = null): BinaryFileResponse;
}
10 changes: 10 additions & 0 deletions src/Models/Medium.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Number;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class Medium extends Model implements Contract
{
Expand Down Expand Up @@ -279,6 +281,14 @@ public function hasConversion(string $conversion): bool
return in_array($conversion, $this->properties['conversions'] ?? []);
}

/**
* Download the medium.
*/
public function download(?string $conversion = null): BinaryFileResponse
{
return Response::download($this->getAbsolutePath($conversion));
}

/**
* Scope the query only to the given search term.
*/
Expand Down

0 comments on commit 7113d8e

Please sign in to comment.