1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Support multiple downloads

This commit is contained in:
Benjamin Beganović 2020-08-20 03:10:04 +02:00
parent c9d94804de
commit f4698c0108
12 changed files with 116 additions and 4 deletions

View File

@ -13,10 +13,14 @@
namespace App\Http\Controllers\ClientPortal;
use App\Http\Controllers\Controller;
use App\Http\Requests\Document\DownloadMultipleDocumentsRequest;
use App\Http\Requests\Document\ShowDocumentRequest;
use App\Models\Document;
use App\Utils\TempFile;
use App\Utils\Traits\MakesHash;
use Illuminate\Support\Facades\Storage;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
class DownloadController extends Controller
{
@ -43,10 +47,30 @@ class DownloadController extends Controller
/**
* @param \App\Http\Requests\Document\ShowDocumentRequest $request
* @param \App\Models\Document $download
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
* @param bool $bulk
* @return mixed
*/
public function download(ShowDocumentRequest $request, Document $download)
{
return Storage::disk($download->disk)->download($download->url, $download->name);
}
public function downloadMultiple(DownloadMultipleDocumentsRequest $request)
{
$documents = Document::whereIn('id', $this->transformKeys($request->file_hash))
->where('company_id', auth('contact')->user()->company->id)
->get();
$options = new Archive();
$options->setSendHttpHeaders(true);
$zip = new ZipStream('files.zip', $options);
foreach ($documents as $document) {
$zip->addFileFromPath(basename($document->filePath()), TempFile::path($document->filePath()));
}
$zip->finish();
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Http\Requests\Document;
use Illuminate\Foundation\Http\FormRequest;
class DownloadMultipleDocumentsRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
// return auth()->user()->can('view', $this->document);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'file_hash' => ['required'],
];
}
}

View File

@ -22,8 +22,7 @@ class ShowDocumentRequest extends Request
*/
public function authorize() : bool
{
return true;
// return auth()->user()->can('view', $this->document);
return auth()->user()->can('view', $this->document);
}
/**

View File

@ -123,4 +123,9 @@ class Document extends BaseModel
{
Storage::disk($this->disk)->delete($this->url);
}
public function filePath()
{
return Storage::disk($this->disk)->url($this->url);
}
}

View File

@ -0,0 +1 @@
!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="/",n(n.s=16)}({16:function(e,t,n){e.exports=n("W54t")},W54t:function(e,t){window.appendToElement=function(e,t){var n=document.getElementById(e),r=n.querySelector('input[value="'.concat(t,'"]'));if(r)return r.remove();var o=document.createElement("INPUT");o.setAttribute("name","file_hash[]"),o.setAttribute("value",t),o.hidden=!0,n.append(o)}}});

View File

@ -14,6 +14,7 @@
"/js/clients/payments/sofort.js": "/js/clients/payments/sofort.js?id=ff4ad07a93bd9fb327c1",
"/js/clients/quotes/action-selectors.js": "/js/clients/quotes/action-selectors.js?id=b6b33ab51b58b51e1212",
"/js/clients/quotes/approve.js": "/js/clients/quotes/approve.js?id=1c5d76fb5f98bd49f6c8",
"/js/clients/shared/multiple-downloads.js": "/js/clients/shared/multiple-downloads.js?id=bf87649ca30c9a3fba59",
"/js/clients/shared/pdf.js": "/js/clients/shared/pdf.js?id=86f3b8d82f809268b3de",
"/js/setup/setup.js": "/js/setup/setup.js?id=c4cd098778bf824a3470",
"/css/card-js.min.css": "/css/card-js.min.css?id=62afeb675235451543ad"

View File

@ -0,0 +1,29 @@
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
const appendToElement = (parent, value) => {
let _parent = document.getElementById(parent);
let _possibleElement = _parent.querySelector(`input[value="${value}"]`);
if (_possibleElement) {
return _possibleElement.remove();
}
let _temp = document.createElement('INPUT');
_temp.setAttribute('name', 'file_hash[]');
_temp.setAttribute('value', value);
_temp.hidden = true;
_parent.append(_temp);
};
window.appendToElement = appendToElement;

View File

@ -3262,4 +3262,6 @@ return [
'common_codes' => 'Common codes and their meanings',
'payment_error_code_20087' => '20087: Bad Track Data (invalid CVV and/or expiry date)',
'download_selected' => 'Download selected',
];

View File

@ -8,6 +8,12 @@
<option>15</option>
<option>20</option>
</select>
<button x-on:click="document.getElementById('multiple-downloads').submit()" class="button button-primary py-2 ml-2">
<span class="hidden md:block">
{{ ctrans('texts.download_selected') }}
</span>
<svg class="md:hidden" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="8 17 12 21 16 17"></polyline><line x1="12" y1="12" x2="12" y2="21"></line><path d="M20.88 18.09A5 5 0 0 0 18 9h-1.26A8 8 0 1 0 3 16.29"></path></svg>
</button>
</div>
<div class="flex items-center">
<div class="mr-3">
@ -25,6 +31,7 @@
<table class="min-w-full shadow rounded border border-gray-200 mt-4 credits-table">
<thead>
<tr>
<th class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider" />
<th class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
<span role="button" wire:click="sortBy('name')" class="cursor-pointer">
{{ ctrans('texts.name') }}
@ -54,6 +61,9 @@
<tbody>
@forelse($downloads as $download)
<tr class="bg-white group hover:bg-gray-100">
<td class="px-6 py-4 whitespace-no-wrap text-sm leading-5 text-gray-500">
<input type="checkbox" class="form-checkbox cursor-pointer" onchange="appendToElement('multiple-downloads', '{{ $download->hashed_id }}')" />
</td>
<td class="px-6 py-4 whitespace-no-wrap text-sm leading-5 text-gray-500">
{{ Illuminate\Support\Str::limit($download->name, 20) }}
</td>

View File

@ -5,8 +5,13 @@
@if($client->getSetting('client_portal_enable_uploads'))
@component('portal.ninja2020.upload.index') @endcomponent
@endif
<script src="{{ asset('js/clients/shared/multiple-downloads.js') }}"></script>
@endsection
@section('body')
<form action="{{ route('client.downloads.multiple') }}" method="post" id="multiple-downloads">
@csrf
</form>
@livewire('downloads-table')
@endsection

View File

@ -60,6 +60,7 @@ Route::group(['middleware' => ['auth:contact','locale'], 'prefix' => 'client', '
Route::get('client/switch_company/{contact}', 'ClientPortal\SwitchCompanyController')->name('switch_company');
Route::post('downloads/multiple', 'ClientPortal\DownloadController@downloadMultiple')->name('downloads.multiple');
Route::get('downloads/{download}/download', 'ClientPortal\DownloadController@download')->name('downloads.download');
Route::resource('downloads', 'ClientPortal\DownloadController')->only(['index', 'show']);

4
webpack.mix.js vendored
View File

@ -61,6 +61,10 @@ mix.js("resources/js/app.js", "public/js")
.js(
"resources/js/clients/shared/pdf.js",
"public/js/clients/shared/pdf.js"
)
.js(
"resources/js/clients/shared/multiple-downloads.js",
"public/js/clients/shared/multiple-downloads.js"
);
mix.copyDirectory('node_modules/card-js/card-js.min.css', 'public/css/card-js.min.css');