22
33namespace Statamic \Eloquent \Assets ;
44
5+ use Illuminate \Database \Eloquent \Model ;
56use Illuminate \Support \Facades \Cache ;
67use Statamic \Assets \Asset as FileAsset ;
78use Statamic \Assets \AssetUploader as Uploader ;
8- use Statamic \Facades \Blink ;
99use Statamic \Facades \Path ;
1010use Statamic \Support \Arr ;
1111use Statamic \Support \Str ;
1212
1313class Asset extends FileAsset
1414{
15+ protected $ existsOnDisk = false ;
1516 protected $ removedData = [];
1617
18+ public static function fromModel (Model $ model )
19+ {
20+ return (new static ())
21+ ->container ($ model ->container )
22+ ->path (Str::replace ('// ' , '/ ' , $ model ->folder .'/ ' .$ model ->basename ))
23+ ->hydrateMeta ($ model ->meta );
24+ }
25+
1726 public function meta ($ key = null )
1827 {
1928 if (func_num_args () === 1 ) {
@@ -37,27 +46,37 @@ public function meta($key = null)
3746
3847 return $ this ->meta = Cache::rememberForever ($ this ->metaCacheKey (), function () {
3948 $ handle = $ this ->container ()->handle ().':: ' .$ this ->metaPath ();
40- if ($ model = app ('statamic.eloquent.assets.model ' )::where ('handle ' , $ handle )->first ()) {
41- return $ model ->data ;
49+ if ($ model = app ('statamic.eloquent.assets.model ' )::where ([
50+ 'container ' => $ this ->containerHandle (),
51+ 'folder ' => $ this ->folder (),
52+ 'basename ' => $ this ->basename (),
53+ ])->first ()) {
54+ return $ model ->meta ;
4255 }
4356
4457 $ this ->writeMeta ($ meta = $ this ->generateMeta ());
4558
59+ if (! $ meta ['data ' ]) {
60+ $ meta ['data ' ] = [];
61+ }
62+
4663 return $ meta ;
4764 });
4865 }
4966
5067 public function exists ()
5168 {
52- $ files = Blink::once ($ this ->container ()->handle ().'::files ' , function () {
53- return $ this ->container ()->files ();
54- });
55-
56- if (! $ path = $ this ->path ()) {
57- return false ;
58- }
69+ return $ this ->existsOnDisk || $ this ->metaExists ();
70+ }
5971
60- return $ files ->contains ($ path );
72+ public function metaExists ()
73+ {
74+ return app ('statamic.eloquent.assets.model ' )::query ()
75+ ->where ([
76+ 'container ' => $ this ->containerHandle (),
77+ 'folder ' => $ this ->folder (),
78+ 'basename ' => $ this ->basename (),
79+ ])->count () > 0 ;
6180 }
6281
6382 private function metaValue ($ key )
@@ -71,13 +90,38 @@ private function metaValue($key)
7190 return Arr::get ($ this ->generateMeta (), $ key );
7291 }
7392
93+ public function generateMeta ()
94+ {
95+ if (! $ this ->disk ()->exists ($ this ->path ())) {
96+ return ['data ' => $ this ->data ->all ()];
97+ }
98+
99+ $ this ->existsOnDisk = true ;
100+
101+ return parent ::generateMeta ();
102+ }
103+
104+ public function hydrateMeta ($ meta )
105+ {
106+ $ this ->meta = $ meta ;
107+
108+ return $ this ;
109+ }
110+
74111 public function writeMeta ($ meta )
75112 {
76113 $ meta ['data ' ] = Arr::removeNullValues ($ meta ['data ' ]);
77114
78115 $ model = app ('statamic.eloquent.assets.model ' )::firstOrNew ([
79- 'handle ' => $ this ->container ()->handle ().':: ' .$ this ->metaPath (),
80- ])->fill (['data ' => $ meta ]);
116+ 'container ' => $ this ->containerHandle (),
117+ 'folder ' => $ this ->folder (),
118+ 'basename ' => $ this ->basename (),
119+ ])->fill ([
120+ 'meta ' => $ meta ,
121+ 'filename ' => $ this ->filename (),
122+ 'extension ' => $ this ->extension (),
123+ 'path ' => $ this ->path (),
124+ ]);
81125
82126 // Set initial timestamps.
83127 if (empty ($ model ->created_at ) && isset ($ meta ['last_modified ' ])) {
@@ -88,6 +132,11 @@ public function writeMeta($meta)
88132 $ model ->save ();
89133 }
90134
135+ public function metaPath ()
136+ {
137+ return $ this ->path ();
138+ }
139+
91140 /**
92141 * Move the asset to a different location.
93142 *
@@ -100,6 +149,8 @@ public function move($folder, $filename = null)
100149 $ filename = Uploader::getSafeFilename ($ filename ?: $ this ->filename ());
101150 $ oldPath = $ this ->path ();
102151 $ oldMetaPath = $ this ->metaPath ();
152+ $ oldFolder = $ this ->folder ();
153+ $ oldBasename = $ this ->basename ();
103154 $ newPath = Str::removeLeft (Path::tidy ($ folder .'/ ' .$ filename .'. ' .pathinfo ($ oldPath , PATHINFO_EXTENSION )), '/ ' );
104155
105156 $ this ->hydrate ();
@@ -108,11 +159,17 @@ public function move($folder, $filename = null)
108159 $ this ->save ();
109160
110161 if ($ oldMetaPath != $ this ->metaPath ()) {
111- $ oldMetaModel = app ('statamic.eloquent.assets.model ' )::whereHandle ($ this ->container ()->handle ().':: ' .$ oldMetaPath )->first ();
162+ $ oldMetaModel = app ('statamic.eloquent.assets.model ' )::where ([
163+ 'container ' => $ this ->containerHandle (),
164+ 'folder ' => $ oldFolder ,
165+ 'basename ' => $ oldBasename ,
166+ ])->first ();
112167
113168 if ($ oldMetaModel ) {
169+ $ meta = $ oldMetaModel ->meta ;
114170 $ oldMetaModel ->delete ();
115- $ this ->writeMeta ($ oldMetaModel ->data );
171+
172+ $ this ->writeMeta ($ meta );
116173 }
117174 }
118175
0 commit comments