Skip to content
Merged

Dev #3472

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions app/Nova/Actions/BulkUploadMediaFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\MediaUpload;
use Illuminate\Bus\Queueable;
use Illuminate\Http\UploadedFile;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -33,15 +34,11 @@ class BulkUploadMediaFiles extends Action
*/
public function handle(ActionFields $fields, Collection $models)
{
$uploadedFiles = $fields->files ?? request()->file('files');
if (!$uploadedFiles) {
$uploadedFiles = $this->collectUploadedFiles($fields);
if (empty($uploadedFiles)) {
return Action::danger('Please select one or more files.');
}

if (!is_array($uploadedFiles)) {
$uploadedFiles = [$uploadedFiles];
}

$allowedExtensions = [
'jpg', 'jpeg', 'png', 'gif', 'webp', 'svg',
'pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'txt',
Expand Down Expand Up @@ -100,6 +97,7 @@ public function fields(NovaRequest $request): array
File::make('Files', 'files')
->withMeta([
'extraAttributes' => [
'name' => 'files[]',
'multiple' => true,
'accept' => '.jpg,.jpeg,.png,.gif,.webp,.svg,.pdf,.doc,.docx,.ppt,.pptx,.xls,.xlsx,.txt',
],
Expand All @@ -109,6 +107,49 @@ public function fields(NovaRequest $request): array
];
}

/**
* Collect uploaded files from Nova action fields + raw request payload.
*
* @return UploadedFile[]
*/
protected function collectUploadedFiles(ActionFields $fields): array
{
$candidates = [];

if (isset($fields->files)) {
$candidates[] = $fields->files;
}

$requestFiles = request()->allFiles();
if (!empty($requestFiles)) {
$candidates[] = $requestFiles;
}

$flatten = function ($value) use (&$flatten): array {
if ($value instanceof UploadedFile) {
return [$value];
}

if (!is_array($value)) {
return [];
}

$result = [];
foreach ($value as $item) {
$result = array_merge($result, $flatten($item));
}

return $result;
};

$files = [];
foreach ($candidates as $candidate) {
$files = array_merge($files, $flatten($candidate));
}

return $files;
}

/**
* Indicate that this action can be run without any models.
*/
Expand Down
28 changes: 19 additions & 9 deletions app/Nova/MediaUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,16 @@ public function fields(Request $request): array
->help('Uploads directly to S3 disk "resources" under folder "nova/uploads".'),

Text::make('URL', function () {
if (empty($this->resolved_url)) {
return '-';
}

$url = $this->resolved_url;

return '<a href="' . e($url) . '" target="_blank" rel="noopener noreferrer">' . e($url) . '</a>';
return $this->renderUrlWithCopy($this->resolved_url);
})
->asHtml()
->onlyOnDetail(),

Text::make('URL', function () {
return $this->resolved_url ?: '-';
})->onlyOnIndex(),
return $this->renderUrlWithCopy($this->resolved_url);
})
->asHtml()
->onlyOnIndex(),
];
}

Expand All @@ -83,4 +79,18 @@ public function actions(Request $request): array
new BulkUploadMediaFiles,
];
}

protected function renderUrlWithCopy(?string $url): string
{
if (empty($url)) {
return '-';
}

$escapedUrl = e($url);
$jsonUrl = json_encode($url, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
$copyScript = "navigator.clipboard.writeText({$jsonUrl}).then(function(){alert('URL copied to clipboard');}).catch(function(){window.prompt('Copy URL:', {$jsonUrl});}); return false;";

return '<a href="' . $escapedUrl . '" target="_blank" rel="noopener noreferrer">' . $escapedUrl . '</a>'
. ' <button type="button" class="btn btn-default btn-xs" onclick="' . e($copyScript) . '">Copy URL</button>';
}
}
Loading