HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/vebon.komma.pro/app/KommaApp/Downloader/DownloaderController.php
<?php

/**
 * Short description for the file.
 *
 * @author      Tim Van Samang <timvansamang@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace KommaApp\Downloader;

use KommaApp\Audit\AuditService;
use KommaApp\Audit\Models\Audit;
use KommaApp\Audit\Models\Document;
use KommaApp\Audit\Models\DocumentGroup;
use KommaApp\Core\CoreController;

class DownloaderController extends CoreController
{
    private $endUser;


    /**
     * AuditController constructor.
     * @param AuditService $auditService
     */
    public function __construct(AuditService $auditService)
    {
        $this->middleware('auth:endUser');

        $this->auditService = $auditService;
        $this->endUser = \Auth::endUser()->get();
    }


    /**
     * This method will download
     * the given type as file or zip.
     *
     * @param $type
     * @param $id
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function download($type, $id)
    {
        //Switch on the download type
        switch ($type) {
            case 'document':
                return $this->downloadDocument($id);
            case 'documentGroup':
                return $this->downloadDocumentGroup($id);
            case 'audit':
                return $this->downloadAudit($id);
        }
        return \App::abort(403, 'Type: ' . $type . ' doesn\'t exist');
    }

    /**
     * Download a single document
     * Force a save as (also on pdf)
     *
     * @param $documentId
     * @return mixed
     */
    public function downloadDocument($documentId)
    {
        //Check if we can find the document
        if (!$document = Document::find($documentId)) return \App::abort(403, 'Document doesn\'t exist');
        //Check if the endUser is allowed to the document based on the audit
        if (!$this->auditService->isUserAllowedToDocument($this->endUser, $document)) return \App::abort(403, 'Not allowed to download this file');
        //Set the file with the storage_path()
        $file = storage_path($document->full_path);
        //Get the mime type from the file
        $mime = mime_content_type($file);
        //Return a download response for the given file, use the file_name from the Document
        return \Response::download($file, $document->file_name, ['content-type' => $mime]);
    }

    /**
     * Download all the files form a DocumentGroup
     * Packed together in a .zip file
     *
     * @param $documentGroupId
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function downloadDocumentGroup($documentGroupId)
    {
        //Check if the documentGroup exists
        if (!$documentGroup = DocumentGroup::find($documentGroupId)) return \App::abort(403, 'DocumentGroup doesn\'t exist');
        //Check if the endUser is allowed to the documentGroup based on the audit
        if (!$this->auditService->isUserAllowedToDocumentGroup($this->endUser, $documentGroup)) return \App::abort(403, 'Not allowed to download this group of files');

        //Create a zipfile, based on auditd id and documentGroup id
        $zipFile = 'zips/audit-' . $documentGroup->audit_id . '-' . $documentGroup->id . '.zip';
        //Make the empty zip file
        $zip = \Zipper::make(storage_path($zipFile));

        //Loop trough the documents in the group
        foreach ($documentGroup->documents as $document) {
            //Add the files to the zip
            $zip->add(storage_path($document->full_path));
        }
        //Close and save the zip
        $zip->close();

        //The zip name that the user see is audit member company, audit year, documentGroup name
        $zipName = \Str::slug($documentGroup->audit->member->company . ' ' . $documentGroup->audit->year . ' ' . $documentGroup->name) . '.zip';

        //Return a download response for the created zip file
        return response()->download(storage_path($zipFile), $zipName);
    }

    /**
     * Download all the files form an audit
     * Packed together in a .zip file.
     *
     * @param $auditId
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function downloadAudit($auditId)
    {
        //Check if the audit exists
        if (!$audit = Audit::find($auditId)) return \App::abort(403, 'Audit doesn\'t exist');
        //Check if the endUser is allowed to the audit
        if (!$this->auditService->isUserAllowed($this->endUser, $audit)) return \App::abort(403, 'Not allowed to download this audit');

        //Create a zipfale, based on the audit id
        $zipFile = 'zips/audit-' . $audit->id . '.zip';
        //Make the empty zip file
        $zip = \Zipper::make(storage_path($zipFile));

        //Loop trough the documentGroups from an audit
        foreach ($audit->documentGroups as $documentGroup) {
            //Loop trough the documents form an group
            foreach ($documentGroup->documents as $document) {
                //Add the files to the zip in the folder based on the documentGroup name
                $zip->folder($documentGroup->name)->add(storage_path($document->full_path));
            }
        }
        //Close and save the zip
        $zip->close();
        //The zip name that the user wull see is de audit member company, audit years
        $zipName = \Str::slug($documentGroup->audit->member->company . ' ' . $documentGroup->audit->year) . '.zip';

        //Return a download response for the created zip file
        return response()->download(storage_path($zipFile), $zipName);
    }


}