Skip to content

Commit

Permalink
Add s3 to the download option
Browse files Browse the repository at this point in the history
  • Loading branch information
eliurkis committed Jan 8, 2018
1 parent b2ed7f4 commit fc34cb9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/CrudController.php
Expand Up @@ -251,7 +251,15 @@ public function download($id, $fieldName)
$this->entityInstance = $this->entity->findOrFail($id);
}

return $this->entityInstance->getFirstMedia($fieldName);
$media = $this->entityInstance->getMedia($fieldName)->last();

if (config('medialibrary.default_filesystem') === 's3') {
$tempImage = tempnam(sys_get_temp_dir(), $media->file_name);
copy($media->getTemporaryUrl(\Carbon::now()->addMinutes(5)), $tempImage);
return response()->file($tempImage, ['Content-Type' => $media->mime_type]);
}

return $media;
}

/* Private Actions */
Expand Down Expand Up @@ -516,7 +524,7 @@ protected function prepareFieldShow($name, $properties = [])
: (isset($config['default_value']) ? $config['default_value'] : null);

if ($this->entityInstance) {
if ($properties['type'] === 'file' && $this->entityInstance->getFirstMedia($name)) {
if ($properties['type'] === 'file' && $this->entityInstance->getMedia($name)->last()) {
$value = '<a href="'.route($this->route.'.download', [$this->entityInstance->id, $name]).
'" target="_blank">'.(
isset($this->fields[$name]['link_name'])
Expand Down
2 changes: 1 addition & 1 deletion src/resources/views/list.blade.php
Expand Up @@ -66,7 +66,7 @@
{{ !empty($row->$name) && $row->$name->diff(Carbon::now())->format('%y') != date('Y') ? $row->$name->format('m/d/Y') : 'N/A' }}
@elseif ($fields[$name]['type'] == 'datetime' && is_object($row->$name))
{{ !empty($row->$name) && $row->$name->diff(Carbon::now())->format('%y') != date('Y') ? $row->$name->format('m/d/Y h:ia') : 'N/A' }}
@elseif ($fields[$name]['type'] == 'file' && $row->getFirstMedia($name))
@elseif ($fields[$name]['type'] == 'file' && $row->getMedia($name)->last())
<a href="{{ route($route.'.download', [$row->id, $name]) }}" target="_blank">
{!! isset($fields[$name]['link_name'])? $fields[$name]['link_name'] : 'download' !!}
</a>
Expand Down

0 comments on commit fc34cb9

Please sign in to comment.