invoiceninja/app/Http/Controllers/ClientPortal/DocumentController.php

103 lines
2.8 KiB
PHP
Raw Normal View History

2019-08-07 10:44:38 +10:00
<?php
2019-08-07 10:44:38 +10:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-08-07 10:44:38 +10:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-08-07 10:44:38 +10:00
*
2021-06-16 16:58:16 +10:00
* @license https://www.elastic.co/licensing/elastic-license
2019-08-07 10:44:38 +10:00
*/
namespace App\Http\Controllers\ClientPortal;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientPortal\Documents\ShowDocumentRequest;
use App\Http\Requests\Document\DownloadMultipleDocumentsRequest;
2021-12-01 07:22:17 +11:00
use App\Libraries\MultiDB;
use App\Models\Document;
use App\Utils\TempFile;
use App\Utils\Traits\MakesHash;
2020-10-28 21:10:49 +11:00
use Illuminate\Contracts\View\Factory;
2019-08-07 16:56:19 +10:00
use Illuminate\Support\Facades\Storage;
2020-10-28 21:10:49 +11:00
use Illuminate\View\View;
2019-08-07 10:44:38 +10:00
class DocumentController extends Controller
{
use MakesHash;
2019-08-07 10:44:38 +10:00
/**
2020-10-28 21:10:49 +11:00
* @return Factory|View
2019-08-07 10:44:38 +10:00
*/
public function index()
2019-08-07 10:44:38 +10:00
{
return render('documents.index');
2019-08-07 10:44:38 +10:00
}
/**
2020-10-28 21:10:49 +11:00
* @param ShowDocumentRequest $request
* @param Document $document
* @return Factory|View
2019-08-07 10:44:38 +10:00
*/
public function show(ShowDocumentRequest $request, Document $document)
2019-08-07 10:44:38 +10:00
{
return render('documents.show', [
'document' => $document,
]);
2019-08-07 10:44:38 +10:00
}
public function download(ShowDocumentRequest $request, Document $document)
2019-08-07 10:44:38 +10:00
{
return Storage::disk($document->disk)->download($document->url, $document->name);
2019-08-07 10:44:38 +10:00
}
public function publicDownload(string $document_hash)
2020-12-12 19:46:28 +11:00
{
2021-12-01 07:22:17 +11:00
MultiDB::documentFindAndSetDb($document_hash);
$document = Document::where('hash', $document_hash)->firstOrFail();
2020-12-12 19:46:28 +11:00
2021-09-27 08:03:11 +10:00
$headers = [];
if(request()->input('inline') == 'true')
$headers = array_merge($headers, ['Content-Disposition' => 'inline']);
return Storage::disk($document->disk)->download($document->url, $document->name, $headers);
2020-12-12 19:46:28 +11:00
}
public function downloadMultiple(DownloadMultipleDocumentsRequest $request)
2019-08-07 10:44:38 +10:00
{
$documents = Document::whereIn('id', $this->transformKeys($request->file_hash))
2022-02-18 21:45:01 +11:00
->where('company_id', auth()->guard('contact')->user()->company_id)
->get();
2019-08-07 10:44:38 +10:00
2022-02-18 21:45:01 +11:00
$zipFile = new \PhpZip\ZipFile();
2019-08-07 10:44:38 +10:00
2022-02-18 21:45:01 +11:00
try{
foreach ($documents as $document) {
$zipFile->addFile(TempFile::path($document->filePath()), $document->name);
}
2019-08-08 21:07:26 +10:00
2022-02-18 21:45:01 +11:00
$filename = now() . '-documents.zip';
$filepath = sys_get_temp_dir() . '/' . $filename;
2019-08-08 21:07:26 +10:00
2022-02-18 21:45:01 +11:00
$zipFile->saveAsFile($filepath) // save the archive to a file
->close(); // close archive
return response()->download($filepath, $filename)->deleteFileAfterSend(true);
2022-02-18 21:45:01 +11:00
}
catch(\PhpZip\Exception\ZipException $e){
// handle exception
}
finally{
$zipFile->close();
}
2019-08-08 21:07:26 +10:00
2019-08-07 10:44:38 +10:00
}
2022-02-18 21:45:01 +11:00
2019-08-07 10:44:38 +10:00
}