Skip to content

Commit

Permalink
Refactor file download command
Browse files Browse the repository at this point in the history
  • Loading branch information
dvlpp committed Oct 21, 2015
1 parent 88516af commit 37e8cb8
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 48 deletions.
3 changes: 1 addition & 2 deletions resources/assets/js/sharp.commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ function handleCommandReturn_RELOAD() {
function handleCommandReturn_DOWNLOAD(data) {
var $dllink = $("#sharp_command_download_link");

$dllink.prop("href", data.file_path)
.prop("download", data.file_name);
$dllink.prop("href", $dllink.data("base") + "/" + data.file_path);
$dllink[0].click();
}
2 changes: 1 addition & 1 deletion resources/assets/sharp.form.min.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions resources/assets/sharp.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -12714,8 +12714,7 @@ function handleCommandReturn_RELOAD() {
function handleCommandReturn_DOWNLOAD(data) {
var $dllink = $("#sharp_command_download_link");

$dllink.prop("href", data.file_path)
.prop("download", data.file_name);
$dllink.prop("href", $dllink.data("base") + "/" + data.file_path);
$dllink[0].click();
};$(window).load(function() {

Expand Down
4 changes: 2 additions & 2 deletions resources/assets/sharp.ui.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion resources/views/cms/entitiesList.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@section('content')

{{-- Link with HTML5's donwload property used by Download commands --}}
<a href="" download="" class="hidden" id="sharp_command_download_link"></a>
<a data-base="{{ route("download") }}" download class="hidden" id="sharp_command_download_link"></a>

<table class="table table-responsive table-striped" id="entity-list">
<thead>
Expand Down
7 changes: 3 additions & 4 deletions src/Dvlpp/Sharp/Commands/CommandReturnTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ public function reload()
/**
* Download a file.
*
* @param $fileName
* @param $filePath
* @param $relativeFilePath
* @return SharpCommandReturnDownload
*/
public function download($fileName, $filePath)
public function download($relativeFilePath)
{
return new SharpCommandReturnDownload($fileName, $filePath);
return new SharpCommandReturnDownload($relativeFilePath);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

class SharpCommandReturnDownload implements SharpCommandReturn
{
private $fileName;
private $filePath;
/**
* @var string
*/
private $relativeFilePath;

/**
* SharpCommandReturnDownload constructor.
* @param $fileName
* @param $filePath
* @param $relativeFilePath
*/
public function __construct($fileName, $filePath)
public function __construct($relativeFilePath)
{
$this->fileName = $fileName;
$this->filePath = $filePath;
$this->relativeFilePath = $relativeFilePath;
}

/**
Expand All @@ -27,8 +27,7 @@ public function get()
{
return [
"type" => "DOWNLOAD",
"file_name" => $this->fileName,
"file_path" => $this->filePath
"file_path" => $this->relativeFilePath
];
}
}
42 changes: 19 additions & 23 deletions src/Dvlpp/Sharp/Http/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

namespace Dvlpp\Sharp\Http;

use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Http\Request;
use Illuminate\Contracts\Filesystem\FileNotFoundException;

class UploadController extends Controller
{

/**
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function upload(Request $request)
{
try {
Expand All @@ -20,32 +25,23 @@ public function upload(Request $request)
}
}

public function uploadWithThumbnail(Request $request)
{
return $this->upload($request);
// try {
// $tab = $this->uploadFile($request);
//
// // Manage thumbnail creation
// $tab["thumbnail"] = sharp_thumbnail(
// $tab["path"],
// $request->get("thumbnail_height"),
// $request->get("thumbnail_width")
// );
//
// return response()->json(["file" => $tab]);
//
// } catch (\Exception $e) {
// return response()->json(["err" => $e->getMessage()]);
// }

}

/**
* @param $fileShortPath
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function download($fileShortPath)
{
$relativeFilePath = config("sharp.upload_storage_base_path") . '/' . $fileShortPath;
if(strstr($fileShortPath, ":")) {
list($disk, $path) = explode(":", $fileShortPath);
} else {
$disk = config("sharp.upload_storage_disk");
$path = $fileShortPath;
}

return response()->download(get_file_path($relativeFilePath), basename($relativeFilePath));
return response()->download(
get_file_path($path, $disk),
basename($path)
);
}

private function uploadFile(Request $request)
Expand Down
4 changes: 0 additions & 4 deletions src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@
"as" => "upload",
"uses" => '\Dvlpp\Sharp\Http\UploadController@upload'
]);
Route::post('/admin/uploadWithThumbnail', [
"as" => "uploadWithThumbnail",
"uses" => '\Dvlpp\Sharp\Http\UploadController@uploadWithThumbnail'
]);
Route::get('/admin/download/{file?}', [
"as" => "download",
"uses" => '\Dvlpp\Sharp\Http\UploadController@download'
Expand Down

0 comments on commit 37e8cb8

Please sign in to comment.