fixed remote media streaming
This commit is contained in:
@ -5,8 +5,8 @@ namespace App\Http\Controllers\Common;
|
||||
use App\Abstracts\Http\Controller;
|
||||
use App\Models\Common\Media;
|
||||
use App\Traits\Uploads as Helper;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Http\Request;
|
||||
use File;
|
||||
|
||||
class Uploads extends Controller
|
||||
{
|
||||
@ -31,7 +31,7 @@ class Uploads extends Controller
|
||||
return response(null, 204);
|
||||
}
|
||||
|
||||
return response()->file($path);
|
||||
return $this->streamMedia($media, $path);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,7 +112,7 @@ class Uploads extends Controller
|
||||
return false;
|
||||
}
|
||||
|
||||
return response()->download($path);
|
||||
return $this->streamMedia($media, $path);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -151,7 +151,7 @@ class Uploads extends Controller
|
||||
|
||||
$media->delete(); //will not delete files
|
||||
|
||||
File::delete($path);
|
||||
Storage::delete($path);
|
||||
|
||||
if (!empty($request->input('page'))) {
|
||||
switch ($request->input('page')) {
|
||||
|
@ -111,4 +111,51 @@ trait Uploads
|
||||
|
||||
return Storage::path($path);
|
||||
}
|
||||
|
||||
public function streamMedia($media, $path = '', $action = '')
|
||||
{
|
||||
if ($this->isLocalStorage()) {
|
||||
if (empty($path)) {
|
||||
$path = $this->getMediaPathOnStorage($media);
|
||||
}
|
||||
|
||||
if (empty($action)) {
|
||||
$action = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
|
||||
}
|
||||
|
||||
return $this->streamLocalMedia($path, $action);
|
||||
}
|
||||
|
||||
return $this->streamRemoteMedia($media);
|
||||
}
|
||||
|
||||
public function streamLocalMedia($path, $action)
|
||||
{
|
||||
$function = ($action == 'get') ? 'file' : $action;
|
||||
|
||||
return response()->$function($path);
|
||||
}
|
||||
|
||||
public function streamRemoteMedia($media)
|
||||
{
|
||||
return response()->streamDownload(
|
||||
function() use ($media) {
|
||||
$stream = $media->stream();
|
||||
|
||||
while($bytes = $stream->read(1024)) {
|
||||
echo $bytes;
|
||||
}
|
||||
},
|
||||
$media->basename,
|
||||
[
|
||||
'Content-Type' => $media->mime_type,
|
||||
'Content-Length' => $media->size,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public function isLocalStorage()
|
||||
{
|
||||
return config('filesystems.disks.' . config('filesystems.default') . '.driver') == 'local';
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user