1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/ClientPortal/DocumentController.php

93 lines
2.6 KiB
PHP
Raw Normal View History

2019-08-07 02:44:38 +02:00
<?php
2019-08-07 02:44:38 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-08-07 02:44:38 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
2019-08-07 02:44:38 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Controllers\ClientPortal;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientPortal\Documents\ShowDocumentRequest;
use App\Http\Requests\Document\DownloadMultipleDocumentsRequest;
2020-12-12 09:46:28 +01:00
use App\Models\ClientContact;
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;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
2019-08-07 02:44:38 +02:00
class DocumentController extends Controller
{
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
*/
public function index()
2019-08-07 02:44:38 +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
*/
public function show(ShowDocumentRequest $request, Document $document)
2019-08-07 02:44:38 +02:00
{
return render('documents.show', [
'document' => $document,
]);
2019-08-07 02:44:38 +02:00
}
public function download(ShowDocumentRequest $request, Document $document)
2019-08-07 02:44:38 +02:00
{
return Storage::disk($document->disk)->download($document->url, $document->name);
2019-08-07 02:44:38 +02:00
}
2020-12-12 09:46:28 +01:00
public function publicDownload(string $contact_key, Document $document)
{
//return failure if the contact is not associated with the document
$contact = ClientContact::where('contact_key', $contact_key)->firstOrFail();
if($contact->company_id == $document->company_id)
return Storage::disk($document->disk)->download($document->url, $document->name);
return response()->json(['message' => 'Access denied']);
}
public function downloadMultiple(DownloadMultipleDocumentsRequest $request)
2019-08-07 02:44:38 +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
$documents->map(function ($document) {
if (auth()->user('contact')->client->id != $document->documentable->id) {
abort(401);
}
});
2019-08-07 02:44:38 +02:00
$options = new Archive();
2019-08-08 13:07:26 +02:00
$options->setSendHttpHeaders(true);
2019-08-08 13:07:26 +02:00
$zip = new ZipStream('files.zip', $options);
foreach ($documents as $document) {
$zip->addFileFromPath(basename($document->filePath()), TempFile::path($document->filePath()));
}
2019-08-08 13:07:26 +02:00
$zip->finish();
2019-08-07 02:44:38 +02:00
}
}