Skip to content

Commit 316b4ef

Browse files
committed
tests for uploading, and clean up class.
1 parent 741e8c2 commit 316b4ef

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

app/Assets/Asset.php

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Statamic\Assets;
44

5-
use Carbon\Carbon;
65
use Stringy\Stringy;
76
use Statamic\API\Str;
87
use Statamic\API\URL;
@@ -12,6 +11,7 @@
1211
use Statamic\API\Image;
1312
use Statamic\Data\Data;
1413
use Statamic\API\Blueprint;
14+
use Illuminate\Support\Carbon;
1515
use Statamic\Data\ContainsData;
1616
use League\Flysystem\Filesystem;
1717
use League\Flysystem\Adapter\Local;
@@ -421,18 +421,15 @@ public function upload(UploadedFile $file)
421421

422422
// If the file exists, we'll append a timestamp to prevent overwriting.
423423
if ($this->disk()->exists($path)) {
424-
$basename = $filename . '-' . time() . '.' . $ext;
424+
$basename = $filename . '-' . Carbon::now()->timestamp . '.' . $ext;
425425
$path = Str::removeLeft(Path::assemble($directory, $basename), '/');
426426
}
427427

428-
$this->performUpload($file, $path);
428+
$this->disk()->put($path, $file);
429429

430430
$this->path($path);
431431

432432
event(new AssetUploaded($this));
433-
434-
// Legacy/Deprecated. TODO: Remove in 2.3
435-
event('asset.uploaded', $path);
436433
}
437434

438435
private function getSafeFilename($string)
@@ -451,36 +448,6 @@ private function getSafeFilename($string)
451448
return (string) $str;
452449
}
453450

454-
/**
455-
* Actually perform the file upload.
456-
*
457-
* Saves the file to a temporary location on the local filesystem, then moves it to the
458-
* right place. This is a workaround for needing to know the file extension or mime
459-
* type when uploading to Amazon S3. Temporary files don't have file extensions
460-
* so sending directly to S3 causes it to appear with the wrong mime type.
461-
*
462-
* @param UploadedFile $file
463-
* @param string $path
464-
* @return void
465-
*/
466-
private function performUpload(UploadedFile $file, $path)
467-
{
468-
// Build up a path where the file will be temporarily stored
469-
$temp = 'uploads/'.md5($file->getRealPath().microtime(true)).'.'.$file->getClientOriginalExtension();
470-
471-
// Upload to a temporary location
472-
$temp_disk = new Filesystem(new Local(temp_path()));
473-
$stream = fopen($file->getRealPath(), 'r+');
474-
$temp_disk->putStream($temp, $stream);
475-
fclose($stream);
476-
477-
// Move from the temporary location to the real container location
478-
$this->disk()->put($path, $temp_disk->readStream($temp));
479-
480-
// Delete the temporary file
481-
$temp_disk->delete($temp);
482-
}
483-
484451
/**
485452
* Replace the file
486453
*

tests/Assets/AssetTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
use Tests\TestCase;
88
use Statamic\Assets\Asset;
99
use Statamic\Fields\Blueprint;
10+
use Illuminate\Http\UploadedFile;
1011
use Statamic\Assets\AssetContainer;
1112
use Illuminate\Filesystem\Filesystem;
12-
use Illuminate\Support\Facades\Storage;
13+
use Illuminate\Support\Facades\Event;
1314
use Facades\Statamic\Assets\Dimensions;
15+
use Illuminate\Support\Facades\Storage;
16+
use Statamic\Events\Data\AssetUploaded;
1417
use Tests\PreventSavingStacheItemsToDisk;
1518

1619
class AssetTest extends TestCase
@@ -463,7 +466,35 @@ function extra_keys_get_added_to_array_when_file_exists()
463466
/** @test */
464467
function it_can_upload_a_file()
465468
{
466-
$this->markTestIncomplete();
469+
Event::fake();
470+
$asset = (new Asset)->container($this->container)->path('path/to/asset.jpg');
471+
Storage::disk('test')->assertMissing('path/to/asset.jpg');
472+
473+
$asset->upload(UploadedFile::fake()->create('asset.jpg'));
474+
475+
Storage::disk('test')->assertExists('path/to/asset.jpg');
476+
$this->assertEquals('path/to/asset.jpg', $asset->path());
477+
Event::assertDispatched(AssetUploaded::class, function ($event) use ($asset) {
478+
return $event->asset = $asset;
479+
});
480+
}
481+
482+
/** @test */
483+
function it_appends_timestamp_to_uploaded_files_filename_if_it_already_exists()
484+
{
485+
Event::fake();
486+
Carbon::setTestNow(Carbon::createFromTimestamp(1549914700));
487+
$asset = (new Asset)->container($this->container)->path('path/to/asset.jpg');
488+
Storage::disk('test')->put('path/to/asset.jpg', '');
489+
Storage::disk('test')->assertExists('path/to/asset.jpg');
490+
491+
$asset->upload(UploadedFile::fake()->create('asset.jpg'));
492+
493+
Storage::disk('test')->assertExists('path/to/asset-1549914700.jpg');
494+
$this->assertEquals('path/to/asset-1549914700.jpg', $asset->path());
495+
Event::assertDispatched(AssetUploaded::class, function ($event) use ($asset) {
496+
return $event->asset = $asset;
497+
});
467498
}
468499

469500
/** @test */

0 commit comments

Comments
 (0)