Skip to content

Commit 714b582

Browse files
author
Sebastian Fix
committed
Quality improvements #major
1 parent 2cfa8d4 commit 714b582

8 files changed

Lines changed: 52 additions & 37 deletions

src/CloudinaryPathNormalizer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public function prefixed(string $path): string
1919

2020
$folder = trim(trim($this->folder), '/');
2121

22+
if ($path === '') {
23+
return $folder;
24+
}
25+
2226
return "{$folder}/{$path}";
2327
}
2428

src/CloudinaryResponseMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function normalizeUploadOrExplicit(
4040
return [
4141
'contents' => $body,
4242
'etag' => Arr::get($response, 'etag'),
43-
'mimetype' => $this->mimeDetector->detectMimeType($logicalPath, $body) ?? 'text/plain',
43+
'mimetype' => $this->mimeDetector->detectMimeType($logicalPath, is_string($body) ? $body : null) ?? 'text/plain',
4444
'path' => $logicalPath,
4545
'size' => Arr::get($response, 'bytes'),
4646
'timestamp' => strtotime((string) Arr::get($response, 'created_at')),

src/CloudinaryUrlBuilder.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public function __construct(
1212
private readonly CloudinaryPathNormalizer $paths,
1313
private readonly CloudinaryDiskOptions $diskOptions,
1414
private readonly CloudinaryResourceOperations $resourceOps,
15-
private readonly CloudinaryResponseLogger $logger,
1615
) {}
1716

1817
public function deliveryUrl(string|array $path): string|false
@@ -42,8 +41,6 @@ public function urlViaExplicit(string $path): string|false
4241
return false;
4342
}
4443

45-
$this->logger->log($response);
46-
4744
[
4845
'url' => $url,
4946
'secure_url' => $secureUrl,

src/FlysystemCloudinaryAdapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public function __construct(
7777
$this->paths,
7878
$this->diskOptions,
7979
$this->resourceOps,
80-
$this->logger
8180
);
8281
$this->listAssembler = new CloudinaryListResponseAssembler(
8382
$this->cloudinary,
@@ -572,9 +571,11 @@ private function contentsToTempStream(string $contents, string $errorPath)
572571
throw UnableToReadFile::fromLocation($errorPath, 'Could not create temporary stream.');
573572
}
574573
if (fwrite($tempFile, $contents) === false) {
574+
fclose($tempFile);
575575
throw UnableToReadFile::fromLocation($errorPath);
576576
}
577577
if (rewind($tempFile) === false) {
578+
fclose($tempFile);
578579
throw UnableToReadFile::fromLocation($errorPath);
579580
}
580581

tests/Feature/Adapter/UploadWriteTest.php

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,27 @@
5757
});
5858
$adapter = new FlysystemCloudinaryAdapter($mock);
5959

60-
$adapter->writeStream($publicId, $contents, new Config);
61-
62-
$meta = $adapter->lastUploadMetadata();
63-
$this->assertSame($contents, $meta['contents']);
64-
$this->assertSame('::etag::', $meta['etag']);
65-
$this->assertSame('text/plain', $meta['mimetype']);
66-
$this->assertSame($publicId, $meta['path']);
67-
$this->assertSame(789, $meta['size']);
68-
$this->assertSame(1633860610, $meta['timestamp']);
69-
$this->assertSame('file', $meta['type']);
70-
$this->assertSame(123456, $meta['version']);
71-
$this->assertSame('::version-id::', $meta['versionid']);
72-
$this->assertSame('public', $meta['visibility']);
73-
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 1);
60+
$resource = fopen('php://temp', 'r+');
61+
fwrite($resource, $contents);
62+
rewind($resource);
63+
try {
64+
$adapter->writeStream($publicId, $resource, new Config);
65+
66+
$meta = $adapter->lastUploadMetadata();
67+
$this->assertSame($resource, $meta['contents']);
68+
$this->assertSame('::etag::', $meta['etag']);
69+
$this->assertSame('text/plain', $meta['mimetype']);
70+
$this->assertSame($publicId, $meta['path']);
71+
$this->assertSame(789, $meta['size']);
72+
$this->assertSame(1633860610, $meta['timestamp']);
73+
$this->assertSame('file', $meta['type']);
74+
$this->assertSame(123456, $meta['version']);
75+
$this->assertSame('::version-id::', $meta['versionid']);
76+
$this->assertSame('public', $meta['visibility']);
77+
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 1);
78+
} finally {
79+
fclose($resource);
80+
}
7481
});
7582

7683
it('can update', function () {
@@ -120,19 +127,26 @@
120127
});
121128
$adapter = new FlysystemCloudinaryAdapter($mock);
122129

123-
$meta = $adapter->updateStream($publicId, $contents, new Config);
124-
125-
$this->assertSame($contents, $meta['contents']);
126-
$this->assertSame('::etag::', $meta['etag']);
127-
$this->assertSame('text/plain', $meta['mimetype']);
128-
$this->assertSame($publicId, $meta['path']);
129-
$this->assertSame(789, $meta['size']);
130-
$this->assertSame(1633860610, $meta['timestamp']);
131-
$this->assertSame('file', $meta['type']);
132-
$this->assertSame(123456, $meta['version']);
133-
$this->assertSame('::version-id::', $meta['versionid']);
134-
$this->assertSame('public', $meta['visibility']);
135-
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 1);
130+
$resource = fopen('php://temp', 'r+');
131+
fwrite($resource, $contents);
132+
rewind($resource);
133+
try {
134+
$meta = $adapter->updateStream($publicId, $resource, new Config);
135+
136+
$this->assertSame($resource, $meta['contents']);
137+
$this->assertSame('::etag::', $meta['etag']);
138+
$this->assertSame('text/plain', $meta['mimetype']);
139+
$this->assertSame($publicId, $meta['path']);
140+
$this->assertSame(789, $meta['size']);
141+
$this->assertSame(1633860610, $meta['timestamp']);
142+
$this->assertSame('file', $meta['type']);
143+
$this->assertSame(123456, $meta['version']);
144+
$this->assertSame('::version-id::', $meta['versionid']);
145+
$this->assertSame('public', $meta['visibility']);
146+
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 1);
147+
} finally {
148+
fclose($resource);
149+
}
136150
});
137151

138152
it('update does not set lastUploadMetadata', function () {

tests/Feature/Adapter/UrlTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
$url = $adapter->getUrlViaRequest('::path::');
2222

2323
$this->assertSame('::secure-url::', $url);
24-
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 2);
24+
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 1);
2525
});
2626

2727
it('can get url', function () {

tests/Unit/CloudinaryPathNormalizerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
expect($normalizer->prefixed('file.jpg'))->toBe('app_uploads/file.jpg');
2222
expect($normalizer->prefixed('/nested/path'))->toBe('app_uploads/nested/path');
23+
expect($normalizer->prefixed(''))->toBe('app_uploads');
2324
});
2425

2526
it('strips folder prefix for logical path', function () {
@@ -33,4 +34,5 @@
3334
$normalizer = new CloudinaryPathNormalizer(' /my/folder/ ');
3435

3536
expect($normalizer->prefixed('x'))->toBe('my/folder/x');
37+
expect($normalizer->prefixed(''))->toBe('my/folder');
3638
});

tests/Unit/CloudinaryUrlBuilderTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
new CloudinaryPathNormalizer(null),
2323
new CloudinaryDiskOptions(null, null, [], true),
2424
new CloudinaryResourceOperations(new CloudinaryResponseLogger),
25-
new CloudinaryResponseLogger
2625
);
2726

2827
expect($builder->deliveryUrl('any'))->toBeFalse();
@@ -46,11 +45,10 @@
4645
new CloudinaryPathNormalizer(null),
4746
new CloudinaryDiskOptions(null, null, [], true),
4847
new CloudinaryResourceOperations($logger),
49-
$logger
5048
);
5149

5250
expect($builder->urlViaExplicit('file'))->toBe('https://secure');
53-
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 2);
51+
Event::assertDispatched(FlysystemCloudinaryResponseLog::class, 1);
5452
});
5553

5654
it('urlViaExplicit returns plain url when preferSecureUrl is false', function () {
@@ -71,7 +69,6 @@
7169
new CloudinaryPathNormalizer(null),
7270
new CloudinaryDiskOptions(null, null, [], false),
7371
new CloudinaryResourceOperations($logger),
74-
$logger
7572
);
7673

7774
expect($builder->urlViaExplicit('file'))->toBe('http://insecure');

0 commit comments

Comments
 (0)