changed media folder structure
This commit is contained in:
parent
1fb81aa46e
commit
02391b766a
@ -3,13 +3,15 @@
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use App\Abstracts\Http\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Common\Media;
|
||||
use App\Traits\Uploads as Helper;
|
||||
use Illuminate\Http\Request;
|
||||
use File;
|
||||
use Storage;
|
||||
|
||||
class Uploads extends Controller
|
||||
{
|
||||
use Helper;
|
||||
|
||||
/**
|
||||
* Get the specified resource.
|
||||
*
|
||||
@ -25,7 +27,7 @@ class Uploads extends Controller
|
||||
}
|
||||
|
||||
// Get file path
|
||||
if (!$path = $this->getPath($media)) {
|
||||
if (!$path = $this->getMediaPathOnStorage($media)) {
|
||||
return response(null, 204);
|
||||
}
|
||||
|
||||
@ -68,7 +70,7 @@ class Uploads extends Controller
|
||||
}
|
||||
|
||||
// Get file path
|
||||
if (!$path = $this->getPath($media)) {
|
||||
if (!$path = $this->getMediaPathOnStorage($media)) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'error' => true,
|
||||
@ -106,7 +108,7 @@ class Uploads extends Controller
|
||||
}
|
||||
|
||||
// Get file path
|
||||
if (!$path = $this->getPath($media)) {
|
||||
if (!$path = $this->getMediaPathOnStorage($media)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -139,7 +141,7 @@ class Uploads extends Controller
|
||||
}
|
||||
|
||||
// Get file path
|
||||
if (!$path = $this->getPath($media)) {
|
||||
if (!$path = $this->getMediaPathOnStorage($media)) {
|
||||
$message = trans('messages.warning.deleted', ['name' => $media->basename, 'text' => $media->basename]);
|
||||
|
||||
flash($message)->warning()->important();
|
||||
@ -163,38 +165,4 @@ class Uploads extends Controller
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the full path of resource.
|
||||
*
|
||||
* @param $media
|
||||
* @return boolean|string
|
||||
*/
|
||||
protected function getPath($media)
|
||||
{
|
||||
if (!is_object($media)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$path = $media->basename;
|
||||
|
||||
if (!empty($media->directory)) {
|
||||
$folders = explode('/', $media->directory);
|
||||
|
||||
// Check if company can access media
|
||||
if ($folders[0] != company_id()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$path = $media->directory . '/' . $media->basename;
|
||||
}
|
||||
|
||||
if (!Storage::exists($path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$full_path = Storage::path($path);
|
||||
|
||||
return $full_path;
|
||||
}
|
||||
}
|
||||
|
86
app/Listeners/Update/V21/Version2111.php
Normal file
86
app/Listeners/Update/V21/Version2111.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners\Update\V21;
|
||||
|
||||
use App\Abstracts\Listeners\Update as Listener;
|
||||
use App\Events\Install\UpdateFinished as Event;
|
||||
use App\Models\Common\Company;
|
||||
use App\Models\Common\Media;
|
||||
use App\Utilities\Date;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class Version2111 extends Listener
|
||||
{
|
||||
const ALIAS = 'core';
|
||||
|
||||
const VERSION = '2.1.11';
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Event $event)
|
||||
{
|
||||
if ($this->skipThisUpdate($event)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->updateCompanies();
|
||||
}
|
||||
|
||||
public function updateCompanies()
|
||||
{
|
||||
$companies = Company::cursor();
|
||||
|
||||
foreach ($companies as $company) {
|
||||
$this->moveMedia($company);
|
||||
}
|
||||
}
|
||||
|
||||
public function moveMedia($company)
|
||||
{
|
||||
$medias = Media::inDirectory('uploads', $company->id . '/', true)->cursor();
|
||||
|
||||
foreach ($medias as $media) {
|
||||
// Bizarre record
|
||||
if (empty($media->directory) || empty($media->basename)) {
|
||||
$media->delete();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Delete media from db if file not exists
|
||||
if (!Storage::exists($media->directory . '/' . $media->basename)) {
|
||||
$media->delete();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Delete completely if soft deleted
|
||||
if (!empty($media->deleted_at)) {
|
||||
$media->delete();
|
||||
|
||||
Storage::delete($media->directory . '/' . $media->basename);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$date = Date::parse($media->created_at)->format('Y/m/d');
|
||||
|
||||
$new_folder = $date . '/'. $media->directory;
|
||||
|
||||
// Check if already exists and delete
|
||||
if (Storage::exists($new_folder . '/' . $media->basename)) {
|
||||
Storage::delete($new_folder . '/' . $media->basename);
|
||||
}
|
||||
|
||||
$media->move($new_folder);
|
||||
}
|
||||
|
||||
// Delete old company folder
|
||||
File::deleteDirectory(Storage::path($company->id));
|
||||
}
|
||||
}
|
@ -30,6 +30,7 @@ class Event extends Provider
|
||||
'App\Listeners\Update\V21\Version213',
|
||||
'App\Listeners\Update\V21\Version218',
|
||||
'App\Listeners\Update\V21\Version219',
|
||||
'App\Listeners\Update\V21\Version2111',
|
||||
],
|
||||
'Illuminate\Auth\Events\Login' => [
|
||||
'App\Listeners\Auth\Login',
|
||||
|
@ -3,32 +3,13 @@
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\Common\Media as MediaModel;
|
||||
use App\Utilities\Date;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use MediaUploader;
|
||||
|
||||
trait Uploads
|
||||
{
|
||||
public function getUploadedFilePath($file, $folder = 'settings', $company_id = null)
|
||||
{
|
||||
$path = '';
|
||||
|
||||
if (!$file || !$file->isValid()) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
if (!$company_id) {
|
||||
$company_id = company_id();
|
||||
}
|
||||
|
||||
$file_name = $file->getClientOriginalName();
|
||||
|
||||
// Upload file
|
||||
$file->storeAs($company_id . '/' . $folder, $file_name);
|
||||
|
||||
// Prepare db path
|
||||
$path = $folder . '/' . $file_name;
|
||||
|
||||
return $path;
|
||||
}
|
||||
public $company_id_index = 3;
|
||||
|
||||
public function getMedia($file, $folder = 'settings', $company_id = null)
|
||||
{
|
||||
@ -38,11 +19,7 @@ trait Uploads
|
||||
return $path;
|
||||
}
|
||||
|
||||
if (!$company_id) {
|
||||
$company_id = company_id();
|
||||
}
|
||||
|
||||
$path = $company_id . '/' . $folder;
|
||||
$path = $this->getMediaFolder($folder, $company_id);
|
||||
|
||||
return MediaUploader::fromSource($file)->toDirectory($path)->upload();
|
||||
}
|
||||
@ -55,11 +32,7 @@ trait Uploads
|
||||
$disk = config('mediable.default_disk');
|
||||
}
|
||||
|
||||
if (!$company_id) {
|
||||
$company_id = company_id();
|
||||
}
|
||||
|
||||
$path = $company_id . '/' . $folder . '/' . basename($file);
|
||||
$path = $this->getMediaFolder($folder, $company_id) . '/' . basename($file);
|
||||
|
||||
return MediaUploader::importPath($disk, $path);
|
||||
}
|
||||
@ -94,4 +67,48 @@ trait Uploads
|
||||
MediaModel::where('id', $media->id)->delete();
|
||||
}
|
||||
}
|
||||
|
||||
public function getMediaFolder($folder, $company_id = null)
|
||||
{
|
||||
if (!$company_id) {
|
||||
$company_id = company_id();
|
||||
}
|
||||
|
||||
$date = Date::now()->format('Y/m/d');
|
||||
|
||||
// 2021/04/09/34235/invoices
|
||||
return $date . '/' . $company_id . '/' . $folder;
|
||||
}
|
||||
|
||||
public function getMediaPathOnStorage($media)
|
||||
{
|
||||
if (!is_object($media)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$path = $media->basename;
|
||||
|
||||
if (!empty($media->directory)) {
|
||||
// 2021/04/09/34235/invoices
|
||||
$folders = explode('/', $media->directory);
|
||||
|
||||
// No company_id in folder path
|
||||
if (empty($folders[$this->company_id_index])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if company can access media
|
||||
if ($folders[$this->company_id_index] != company_id()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$path = $media->directory . '/' . $media->basename;
|
||||
}
|
||||
|
||||
if (!Storage::exists($path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Storage::path($path);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user