Skip to content

Commit 8fed315

Browse files
authored
Fix asset uploading bug (#252)
* Fix asset uploading bug * Switcheroo
1 parent 36ac24d commit 8fed315

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/Assets/Asset.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
use Statamic\Assets\AssetUploader as Uploader;
99
use Statamic\Contracts\Assets\Asset as AssetContract;
1010
use Statamic\Data\HasDirtyState;
11+
use Statamic\Events\AssetUploaded;
1112
use Statamic\Facades\Blink;
1213
use Statamic\Facades\Path;
1314
use Statamic\Support\Arr;
1415
use Statamic\Support\Str;
16+
use Symfony\Component\HttpFoundation\File\UploadedFile;
1517

1618
class Asset extends FileAsset
1719
{
@@ -146,15 +148,19 @@ public static function makeModelFromContract(AssetContract $source, $meta = [])
146148
return null;
147149
}
148150

151+
$original = $source->getOriginal();
152+
149153
$model = app('statamic.eloquent.assets.model')::firstOrNew([
150154
'container' => $source->containerHandle(),
151-
'folder' => $source->folder(),
152-
'basename' => $source->basename(),
155+
'folder' => Arr::get($original, 'folder', $source->folder()),
156+
'basename' => Arr::get($original, 'basename', $source->basename()),
153157
])->fill([
154158
'meta' => $meta,
155159
'filename' => $source->filename(),
156160
'extension' => $extension,
157161
'path' => $source->path(),
162+
'folder' => $source->folder(),
163+
'basename' => $source->basename(),
158164
]);
159165

160166
// Set initial timestamps.
@@ -211,4 +217,32 @@ public function move($folder, $filename = null)
211217

212218
return $this;
213219
}
220+
221+
/**
222+
* Upload a file.
223+
*
224+
* @return $this
225+
*/
226+
public function upload(UploadedFile $file)
227+
{
228+
$path = Uploader::asset($this)->upload($file);
229+
230+
$this
231+
->path($path)
232+
->save();
233+
234+
AssetUploaded::dispatch($this);
235+
236+
return $this;
237+
}
238+
239+
public function getCurrentDirtyStateAttributes(): array
240+
{
241+
return array_merge([
242+
'path' => $this->path(),
243+
'folder' => $this->folder(),
244+
'basename' => $this->basename(),
245+
'data' => $this->data()->toArray(),
246+
]);
247+
}
214248
}

0 commit comments

Comments
 (0)