1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larafolio\database\seeds; |
4
|
|
|
|
5
|
|
|
use App\User; |
6
|
|
|
use Larafolio\Models\Project; |
7
|
|
|
use Illuminate\Database\Seeder; |
8
|
|
|
use Illuminate\Filesystem\Filesystem; |
9
|
|
|
|
10
|
|
|
class ImagesTableSeeder extends Seeder |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Lookup table to map images to projects. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $projectLookup = [ |
18
|
|
|
'a' => 1, |
19
|
|
|
'b' => 2, |
20
|
|
|
'c' => 3, |
21
|
|
|
'd' => 4, |
22
|
|
|
'e' => 5, |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Run the database seeds. |
27
|
|
|
*/ |
28
|
|
|
public function run() |
29
|
|
|
{ |
30
|
|
|
$filesystem = new Filesystem(); |
31
|
|
|
|
32
|
|
|
$from = __DIR__.'/../../../tests/_data/images'; |
33
|
|
|
|
34
|
|
|
$images = $filesystem->allFiles($from); |
35
|
|
|
|
36
|
|
|
foreach ($images as $key => $image) { |
37
|
|
|
$name = $image->getFilename(); |
38
|
|
|
|
39
|
|
|
$path = 'public/images/'.$name; |
40
|
|
|
|
41
|
|
|
$this->moveImage($image, $path, $filesystem); |
42
|
|
|
|
43
|
|
|
$this->addToProject($name, $path); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$old = umask(0); |
47
|
|
|
|
48
|
|
|
chmod(storage_path('app/public/images'), 0775); |
49
|
|
|
|
50
|
|
|
umask($old); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Move image file to storage. |
55
|
|
|
* |
56
|
|
|
* @param \Symfony\Component\Finder\SplFileInfo $image File info. |
57
|
|
|
* @param string $path Path to move to. |
58
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
59
|
|
|
*/ |
60
|
|
|
protected function moveImage($image, $path, $filesystem) |
61
|
|
|
{ |
62
|
|
|
$imageFile = $filesystem->get($image->getPathname()); |
63
|
|
|
|
64
|
|
|
\Storage::put($path, $imageFile); |
65
|
|
|
|
66
|
|
|
storage_path('app/'.$path); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add image to a project. |
71
|
|
|
* |
72
|
|
|
* @param string $name Original filename. |
73
|
|
|
* @param string $path Path to image. |
74
|
|
|
*/ |
75
|
|
|
protected function addToProject($name, $path) |
76
|
|
|
{ |
77
|
|
|
$key = $this->projectLookup[substr($name, 0, 1)]; |
78
|
|
|
|
79
|
|
|
$project = Project::find($key); |
80
|
|
|
|
81
|
|
|
$user = User::find(1); |
82
|
|
|
|
83
|
|
|
$user->addImageToProject($project, ['path' => $path]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|