Skip to content

Commit 57ad266

Browse files
author
ahmadhussnain
committed
added functions that auto upload based on dev environment
1 parent 2b7d813 commit 57ad266

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

app/Http/Traits/URLScheme.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Traits;
44

55
use Illuminate\Support\Facades\App;
6+
use Illuminate\Support\Facades\Storage;
67
use Illuminate\Support\Facades\URL;
78

89
trait URLScheme {
@@ -28,4 +29,44 @@ public function checkURLScheme() {
2829
URL::forceScheme('https');
2930
}
3031
}
32+
33+
34+
/**
35+
* Old way to store images
36+
* $filename = 'categories/' . uniqid() . '.' . $file->extension(); // categories/6546dgfgfdgfg.jpg
37+
* $file->storePubliclyAs('public', $filename); // Store inside filesystem categories/6546dgfgfdgfg.jpg
38+
*/
39+
40+
/**
41+
* @param $file
42+
* @return string
43+
*/
44+
public function categoryFileStore($file): string
45+
{
46+
$fileDirectory = 'categories/';
47+
$fileName = uniqid().'.'.$file->extension();
48+
if (App::environment('local')) {
49+
Storage::disk('public')->put($fileDirectory.$fileName, file_get_contents($file));
50+
return Storage::disk('public')->url($fileDirectory.$fileName);
51+
} else {
52+
Storage::disk('s3')->put($fileDirectory.$fileName, file_get_contents($file));
53+
return Storage::disk('s3')->url($fileDirectory.$fileName);
54+
}
55+
}
56+
57+
58+
/**
59+
* @param string $oldFilePath
60+
*/
61+
public function categoryFileDelete(string $oldFilePath)
62+
{
63+
$fileDirectory = 'categories/';
64+
// Returns trailing name component of path e.g. ( 6546dgfgfdgfg.jpg)
65+
$oldPath = basename($oldFilePath); //
66+
if (App::environment('local')) {
67+
Storage::disk('public')->delete($fileDirectory.$oldPath);
68+
} else {
69+
Storage::disk('s3')->delete($fileDirectory.$oldPath);
70+
}
71+
}
3172
}

0 commit comments

Comments
 (0)