Skip to content
This repository was archived by the owner on Jul 15, 2020. It is now read-only.

Commit 5fc2a7d

Browse files
Add and boot Cloudinary Driver
1 parent ba59445 commit 5fc2a7d

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/CloudinaryAdapter.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,20 @@ public function writeStream($path, $resource, Config $options)
6262

6363
$resourceType = $options->has('resource_type') ? $options->get('resource_type') : 'auto';
6464

65+
$fileExtension = pathinfo($publicId, PATHINFO_EXTENSION);
66+
67+
$newPublicId = $fileExtension ? substr($publicId, 0, - (strlen($fileExtension) + 1)) : $publicId;
68+
6569
$uploadOptions = [
66-
'public_id' => $publicId,
70+
'public_id' => $newPublicId,
6771
'resource_type' => $resourceType
68-
]
72+
];
6973

7074
$resourceMetadata = stream_get_meta_data($resource);
7175

72-
return $this->uploadApi()->upload($resourceMetadata['uri'], $uploadOptions)
76+
$result = resolve(CloudinaryEngine::class)->upload($resourceMetadata['uri'], $uploadOptions);
77+
78+
return $result;
7379
}
7480

7581

@@ -119,9 +125,9 @@ public function rename($path, $newpath)
119125
$pathInfo = pathinfo($path);
120126
$newPathInfo = pathinfo($newpath);
121127

122-
$remotePath = $pathInfo['dirname'] != '.') ? pathInfo['dirname'] . '/' . $pathInfo['filename'] : $pathInfo['filename'];
128+
$remotePath = ($pathInfo['dirname'] != '.') ? pathInfo['dirname'] . '/' . $pathInfo['filename'] : $pathInfo['filename'];
123129

124-
$remoteNewPath = $pathInfo['dirname'] != '.') ? $newPathInfo['dirname'] . '/' . $newPathInfo['filename'] : $newPathInfo['filename'];
130+
$remoteNewPath = ($pathInfo['dirname'] != '.') ? $newPathInfo['dirname'] . '/' . $newPathInfo['filename'] : $newPathInfo['filename'];
125131

126132
$result = $this->uploadApi()->rename($remotePath, $remoteNewPath);
127133

@@ -195,13 +201,7 @@ public function createDir($dirname, Config $options)
195201
*/
196202
public function has($path)
197203
{
198-
try {
199-
$this->adminApi()->resource($path);
200-
} catch (Exception $e) {
201-
return false;
202-
}
203-
204-
return true;
204+
return file_exists($path);
205205
}
206206

207207
/**
@@ -275,7 +275,7 @@ public function listContents($directory = '', $hasRecursive = false)
275275
*/
276276
public function getMetadata($path)
277277
{
278-
return $this->prepareResourceMetadata($this->getResource($path));
278+
return $this->prepareResourceMetadata($this->getResource($path));
279279
}
280280

281281
/**

src/CloudinaryServiceProvider.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ public function boot()
3434
$this->bootComponents();
3535
$this->bootCommands();
3636
$this->bootPublishing();
37-
38-
Storage::extend('cloudinary', function () {
39-
return new Filesystem(new CloudinaryAdapter(config('cloudinary.cloud_url')));
40-
});
37+
$this->bootCloudinaryDriver();
4138
}
4239

4340
/**
@@ -54,7 +51,7 @@ public function register()
5451
}
5552

5653

57-
public function bootCommands()
54+
protected function bootCommands()
5855
{
5956
/**
6057
* Register Laravel Cloudinary Artisan commands
@@ -151,4 +148,13 @@ protected function bootMacros()
151148
return $engine->uploadFile($this->getRealPath(), ['folder' => $folder, 'public_id' => $publicId]);
152149
});
153150
}
151+
152+
protected function bootCloudinaryDriver()
153+
{
154+
$this->app['config']['filesystems.disks.cloudinary'] = ['driver' => 'cloudinary'];
155+
156+
Storage::extend('cloudinary', function ($app, $config) {
157+
return new Filesystem(new CloudinaryAdapter(config('cloudinary.cloud_url')));
158+
});
159+
}
154160
}

0 commit comments

Comments
 (0)