2019-08-07 02:44:38 +02:00
|
|
|
<?php
|
2020-09-28 12:04:34 +02:00
|
|
|
|
2019-08-07 02:44:38 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-08-07 02:44:38 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-08-07 02:44:38 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-08-07 02:44:38 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\ClientPortal;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2020-09-28 12:04:34 +02:00
|
|
|
use App\Http\Requests\ClientPortal\Documents\ShowDocumentRequest;
|
|
|
|
use App\Http\Requests\Document\DownloadMultipleDocumentsRequest;
|
|
|
|
use App\Models\Document;
|
|
|
|
use App\Utils\TempFile;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Contracts\View\Factory;
|
2019-08-07 08:56:19 +02:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\View\View;
|
2020-09-28 12:04:34 +02:00
|
|
|
use ZipStream\Option\Archive;
|
|
|
|
use ZipStream\ZipStream;
|
2019-08-07 02:44:38 +02:00
|
|
|
|
|
|
|
class DocumentController extends Controller
|
|
|
|
{
|
2020-09-28 12:04:34 +02:00
|
|
|
use MakesHash;
|
2019-08-07 02:44:38 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Factory|View
|
2019-08-07 02:44:38 +02:00
|
|
|
*/
|
2020-09-28 12:04:34 +02:00
|
|
|
public function index()
|
2019-08-07 02:44:38 +02:00
|
|
|
{
|
2020-09-28 12:04:34 +02:00
|
|
|
return render('documents.index');
|
2019-08-07 02:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param ShowDocumentRequest $request
|
|
|
|
* @param Document $document
|
|
|
|
* @return Factory|View
|
2019-08-07 02:44:38 +02:00
|
|
|
*/
|
2020-09-28 12:04:34 +02:00
|
|
|
public function show(ShowDocumentRequest $request, Document $document)
|
2019-08-07 02:44:38 +02:00
|
|
|
{
|
2020-09-28 12:04:34 +02:00
|
|
|
return render('documents.show', [
|
|
|
|
'document' => $document,
|
|
|
|
]);
|
2019-08-07 02:44:38 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 12:04:34 +02:00
|
|
|
public function download(ShowDocumentRequest $request, Document $document)
|
2019-08-07 02:44:38 +02:00
|
|
|
{
|
2020-09-28 12:04:34 +02:00
|
|
|
return Storage::disk($document->disk)->download($document->url, $document->name);
|
2019-08-07 02:44:38 +02:00
|
|
|
}
|
|
|
|
|
2020-12-13 10:46:29 +01:00
|
|
|
public function publicDownload(string $document_hash)
|
2020-12-12 09:46:28 +01:00
|
|
|
{
|
2020-12-13 10:46:29 +01:00
|
|
|
$document = Document::where('hash', $document_hash)->firstOrFail();
|
2020-12-12 09:46:28 +01:00
|
|
|
|
2020-12-16 12:52:40 +01:00
|
|
|
return Storage::disk($document->disk)->download($document->url, $document->name);
|
2020-12-12 09:46:28 +01:00
|
|
|
}
|
|
|
|
|
2020-09-28 12:04:34 +02:00
|
|
|
public function downloadMultiple(DownloadMultipleDocumentsRequest $request)
|
2019-08-07 02:44:38 +02:00
|
|
|
{
|
2020-09-28 12:04:34 +02:00
|
|
|
$documents = Document::whereIn('id', $this->transformKeys($request->file_hash))
|
|
|
|
->where('company_id', auth('contact')->user()->company->id)
|
|
|
|
->get();
|
2019-08-07 02:44:38 +02:00
|
|
|
|
2020-09-28 12:04:34 +02:00
|
|
|
$documents->map(function ($document) {
|
|
|
|
if (auth()->user('contact')->client->id != $document->documentable->id) {
|
2021-05-24 11:39:21 +02:00
|
|
|
abort(401, 'Permission denied');
|
2020-09-28 12:04:34 +02:00
|
|
|
}
|
|
|
|
});
|
2019-08-07 02:44:38 +02:00
|
|
|
|
2020-09-28 12:04:34 +02:00
|
|
|
$options = new Archive();
|
2019-08-08 13:07:26 +02:00
|
|
|
|
2020-09-28 12:04:34 +02:00
|
|
|
$options->setSendHttpHeaders(true);
|
2019-08-08 13:07:26 +02:00
|
|
|
|
2021-02-15 15:52:13 +01:00
|
|
|
$zip = new ZipStream(now() . '-documents.zip', $options);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-09-28 12:04:34 +02:00
|
|
|
foreach ($documents as $document) {
|
2021-09-08 04:23:26 +02:00
|
|
|
$zip->addFileFromPath(basename($document->diskPath()), TempFile::path($document->filePath()));
|
2020-09-28 12:04:34 +02:00
|
|
|
}
|
2019-08-08 13:07:26 +02:00
|
|
|
|
2020-09-28 12:04:34 +02:00
|
|
|
$zip->finish();
|
2019-08-07 02:44:38 +02:00
|
|
|
}
|
|
|
|
}
|