Invoice path
This commit is contained in:
		@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Common;
 | 
				
			|||||||
use App\Http\Controllers\Controller;
 | 
					use App\Http\Controllers\Controller;
 | 
				
			||||||
use App\Models\Common\Media;
 | 
					use App\Models\Common\Media;
 | 
				
			||||||
use Storage;
 | 
					use Storage;
 | 
				
			||||||
 | 
					use File;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Uploads extends Controller
 | 
					class Uploads extends Controller
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -15,10 +16,12 @@ class Uploads extends Controller
 | 
				
			|||||||
     * @param  $file
 | 
					     * @param  $file
 | 
				
			||||||
     * @return boolean|Response
 | 
					     * @return boolean|Response
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function get($folder, $file)
 | 
					    public function get($id)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        $media = Media::find($id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Get file path
 | 
					        // Get file path
 | 
				
			||||||
        if (!$path = $this->getPath($folder, $file)) {
 | 
					        if (!$path = $this->getPath($media)) {
 | 
				
			||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -32,10 +35,12 @@ class Uploads extends Controller
 | 
				
			|||||||
     * @param  $file
 | 
					     * @param  $file
 | 
				
			||||||
     * @return boolean|Response
 | 
					     * @return boolean|Response
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function download($folder, $file)
 | 
					    public function download($id)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        $media = Media::find($id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Get file path
 | 
					        // Get file path
 | 
				
			||||||
        if (!$path = $this->getPath($folder, $file)) {
 | 
					        if (!$path = $this->getPath($media)) {
 | 
				
			||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -49,21 +54,23 @@ class Uploads extends Controller
 | 
				
			|||||||
     * @param  $file
 | 
					     * @param  $file
 | 
				
			||||||
     * @return callable
 | 
					     * @return callable
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function destroy($folder, $id)
 | 
					    public function destroy($id)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $media = Media::find($id);
 | 
					        $media = Media::find($id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Get file path
 | 
					        // Get file path
 | 
				
			||||||
        /*if (!$path = $this->getPath($folder, $id)) {
 | 
					        if (!$path = $this->getPath($media)) {
 | 
				
			||||||
            $message = trans('messages.warning.deleted', ['name' => $file, 'text' => $file]);
 | 
					            $message = trans('messages.warning.deleted', ['name' => $media->basename, 'text' => $media->basename]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            flash($message)->warning();
 | 
					            flash($message)->warning();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return back();
 | 
					            return back();
 | 
				
			||||||
        }*/
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $media->delete(); //will not delete files
 | 
					        $media->delete(); //will not delete files
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        File::delete($path);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return back();
 | 
					        return back();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -74,14 +81,13 @@ class Uploads extends Controller
 | 
				
			|||||||
     * @param  $file
 | 
					     * @param  $file
 | 
				
			||||||
     * @return boolean|string
 | 
					     * @return boolean|string
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    protected function getPath($folder, $file)
 | 
					    protected function getPath($media)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        // Add company id
 | 
					        $path = $media->basename;
 | 
				
			||||||
        if ($folder != 'users') {
 | 
					 | 
				
			||||||
            $folder = session('company_id') . '/' . $folder;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $path = $folder . '/' . $file;
 | 
					        if (!empty($media->directory)) {
 | 
				
			||||||
 | 
					            $path = $media->directory . '/' . $media->basename;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!Storage::exists($path)) {
 | 
					        if (!Storage::exists($path)) {
 | 
				
			||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -416,7 +416,7 @@ class Invoices extends Controller
 | 
				
			|||||||
        if ($request->file('attachment')) {
 | 
					        if ($request->file('attachment')) {
 | 
				
			||||||
            $media = $this->getMedia($request->file('attachment'), 'invoices');
 | 
					            $media = $this->getMedia($request->file('attachment'), 'invoices');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $invoice->syncMedia($media, 'attachment');
 | 
					            $invoice->attachMedia($media, 'attachment');
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Delete previous invoice totals
 | 
					        // Delete previous invoice totals
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -56,7 +56,7 @@ class Items extends Controller
 | 
				
			|||||||
        $item = Item::create($request->input());
 | 
					        $item = Item::create($request->input());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Upload picture
 | 
					        // Upload picture
 | 
				
			||||||
        if ($media) {
 | 
					        if ($request->file('picture')) {
 | 
				
			||||||
            $media = $this->getMedia($request->file('picture'), 'items');
 | 
					            $media = $this->getMedia($request->file('picture'), 'items');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $item->attachMedia($media, 'picture');
 | 
					            $item->attachMedia($media, 'picture');
 | 
				
			||||||
@@ -141,7 +141,7 @@ class Items extends Controller
 | 
				
			|||||||
        $item->update($request->input());
 | 
					        $item->update($request->input());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Upload picture
 | 
					        // Upload picture
 | 
				
			||||||
        if ($media) {
 | 
					        if ($request->file('picture')) {
 | 
				
			||||||
            $media = $this->getMedia($request->file('picture'), 'items');
 | 
					            $media = $this->getMedia($request->file('picture'), 'items');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $item->syncMedia($media, 'picture');
 | 
					            $item->syncMedia($media, 'picture');
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,10 +36,8 @@ trait Uploads
 | 
				
			|||||||
            $company_id = session('company_id');
 | 
					            $company_id = session('company_id');
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $path = config('filesystems.disks.uploads.root') . '/' . $company_id . '/' . $folder;
 | 
					        $path = $company_id . '/' . $folder;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        config(['filesystems.disks.uploads.root' => $path]);
 | 
					        return MediaUploader::fromSource($file)->toDirectory($path)->upload();
 | 
				
			||||||
 | 
					 | 
				
			||||||
        return MediaUploader::fromSource($file)->upload();
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -189,7 +189,7 @@
 | 
				
			|||||||
                            {!! Form::open([
 | 
					                            {!! Form::open([
 | 
				
			||||||
                                'id' => 'attachment-' . $invoice->attachment->id,
 | 
					                                'id' => 'attachment-' . $invoice->attachment->id,
 | 
				
			||||||
                                'method' => 'DELETE',
 | 
					                                'method' => 'DELETE',
 | 
				
			||||||
                                'url' => [url('uploads/invoices/' . $invoice->attachment->id)],
 | 
					                                'url' => [url('uploads/' . $invoice->attachment->id)],
 | 
				
			||||||
                                'style' => 'display:inline'
 | 
					                                'style' => 'display:inline'
 | 
				
			||||||
                            ]) !!}
 | 
					                            ]) !!}
 | 
				
			||||||
                            {{ Form::hidden('id', $invoice->id) }}
 | 
					                            {{ Form::hidden('id', $invoice->id) }}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,8 +9,8 @@
 | 
				
			|||||||
Route::group(['middleware' => 'language'], function () {
 | 
					Route::group(['middleware' => 'language'], function () {
 | 
				
			||||||
    Route::group(['middleware' => 'auth'], function () {
 | 
					    Route::group(['middleware' => 'auth'], function () {
 | 
				
			||||||
        Route::group(['prefix' => 'uploads'], function () {
 | 
					        Route::group(['prefix' => 'uploads'], function () {
 | 
				
			||||||
            Route::get('{folder}/{file}', 'Common\Uploads@get');
 | 
					            Route::get('{id}', 'Common\Uploads@get');
 | 
				
			||||||
            Route::get('{folder}/{file}/download', 'Common\Uploads@download');
 | 
					            Route::get('{id}/download', 'Common\Uploads@download');
 | 
				
			||||||
            Route::delete('{id}', 'Common\Uploads@destroy');
 | 
					            Route::delete('{id}', 'Common\Uploads@destroy');
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user