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

Commit de92af9

Browse files
Make archive command work
1 parent c76fc43 commit de92af9

File tree

8 files changed

+50
-102
lines changed

8 files changed

+50
-102
lines changed

src/CloudinaryAdapter.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public function write($path, $contents, Config $options)
6868
/**
6969
* Write a new file using a stream.
7070
*
71-
* @param string $path
72-
* @param resource $resource
73-
* @param Config $options Config object
71+
* @param string $path
72+
* @param resource $resource
73+
* @param Config $options Config object
7474
*
7575
* @return array|false false on failure file meta data on success
7676
*/
@@ -82,10 +82,10 @@ public function writeStream($path, $resource, Config $options)
8282

8383
$fileExtension = pathinfo($publicId, PATHINFO_EXTENSION);
8484

85-
$newPublicId = $fileExtension ? substr($publicId, 0, -(strlen($fileExtension) + 1)) : $publicId;
85+
$newPublicId = $fileExtension ? substr($publicId, 0, - (strlen($fileExtension) + 1)) : $publicId;
8686

8787
$uploadOptions = [
88-
'public_id' => $newPublicId,
88+
'public_id' => $newPublicId,
8989
'resource_type' => $resourceType
9090
];
9191

@@ -100,9 +100,9 @@ public function writeStream($path, $resource, Config $options)
100100
* Update a file using a stream.
101101
* Cloudinary has no specific update method. Overwrite instead.
102102
*
103-
* @param string $path
104-
* @param resource $resource
105-
* @param Config $options Config object
103+
* @param string $path
104+
* @param resource $resource
105+
* @param Config $options Config object
106106
*
107107
* @return array|false false on failure file meta data on success
108108
*/
@@ -123,7 +123,7 @@ public function updateStream($path, $resource, Config $options)
123123
*/
124124
public function rename($path, $newpath)
125125
{
126-
$pathInfo = pathinfo($path);
126+
$pathInfo = pathinfo($path);
127127
$newPathInfo = pathinfo($newpath);
128128

129129
$remotePath = ($pathInfo['dirname'] != '.') ? pathInfo['dirname'] . '/' . $pathInfo['filename'] : $pathInfo['filename'];
@@ -181,6 +181,8 @@ public function delete($path)
181181
* @param string $dirname
182182
*
183183
* @return bool
184+
*
185+
* @throws ApiError
184186
*/
185187
public function deleteDir($dirname)
186188
{
@@ -205,6 +207,8 @@ protected function adminApi()
205207
* @param Config $options
206208
*
207209
* @return bool
210+
*
211+
* @throws ApiError
208212
*/
209213
public function createDir($dirname, Config $options)
210214
{
@@ -251,7 +255,7 @@ public function readStream($path)
251255
{
252256
$resource = (array)$this->adminApi()->resource($path);
253257

254-
$stream = fopen($resource['secure_url'], 'r');
258+
$stream = fopen($resource['secure_url'], 'rb');
255259

256260
return compact('stream', 'path');
257261
}
@@ -260,7 +264,7 @@ public function readStream($path)
260264
* List contents of a directory.
261265
*
262266
* @param string $directory
263-
* @param bool $recursive
267+
* @param bool $hasR ecursive
264268
*
265269
* @return array
266270
*/
@@ -298,15 +302,17 @@ protected function prepareResourceMetadata($resource)
298302
{
299303
$resource['type'] = 'file';
300304
$resource['path'] = $resource['public_id'];
301-
$resource = array_merge($resource, $this->prepareSize($resource));
302-
$resource = array_merge($resource, $this->prepareTimestamp($resource));
303-
$resource = array_merge($resource, $this->prepareMimetype($resource));
305+
$resource = array_merge($resource, $this->prepareSize($resource));
306+
$resource = array_merge($resource, $this->prepareTimestamp($resource));
307+
$resource = array_merge($resource, $this->prepareMimetype($resource));
304308
return $resource;
305309
}
306310

307311
/**
308312
* prepare size response
313+
*
309314
* @param array $resource
315+
*
310316
* @return array
311317
*/
312318
protected function prepareSize($resource)
@@ -316,8 +322,10 @@ protected function prepareSize($resource)
316322
}
317323

318324
/**
319-
* prepare timestpamp response
325+
* prepare timestamp response
326+
*
320327
* @param array $resource
328+
*
321329
* @return array
322330
*/
323331
protected function prepareTimestamp($resource)
@@ -328,7 +336,9 @@ protected function prepareTimestamp($resource)
328336

329337
/**
330338
* prepare mimetype response
339+
*
331340
* @param array $resource
341+
*
332342
* @return array
333343
*/
334344
protected function prepareMimetype($resource)

src/Commands/BackupFilesCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Unicodeveloper\Cloudinary\Commands;
44

5+
use Exception;
56
use Illuminate\Console\Command;
67
use Illuminate\Support\Facades\File;
78
use Unicodeveloper\Cloudinary\CloudinaryEngine;

src/Commands/DeleteFilesCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Unicodeveloper\Cloudinary\Commands;
44

5+
use Exception;
56
use Illuminate\Console\Command;
67
use Unicodeveloper\Cloudinary\CloudinaryEngine;
78

src/Commands/FetchFilesCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Unicodeveloper\Cloudinary\Commands;
44

5+
use Exception;
56
use Illuminate\Console\Command;
67
use Unicodeveloper\Cloudinary\CloudinaryEngine;
78

src/Commands/GenerateArchiveCommand.php

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

33
namespace Unicodeveloper\Cloudinary\Commands;
44

5+
use Exception;
56
use Illuminate\Console\Command;
67
use Unicodeveloper\Cloudinary\CloudinaryEngine;
78

@@ -16,7 +17,9 @@ class GenerateArchiveCommand extends Command
1617
*
1718
* @var string
1819
*/
19-
protected $signature = "cloudinary:archive {remote-url}";
20+
protected $signature = "cloudinary:archive
21+
{--tags=* : The tags of the assets you want included in the arhive}
22+
{--public_ids=* : The public IDs of the assets you want included in the archive}";
2023

2124
/**
2225
* The console command description.
@@ -38,27 +41,25 @@ public function handle(CloudinaryEngine $engine)
3841
return;
3942
}
4043

41-
if (is_numeric($this->argument('remote-url'))) {
42-
$this->warn('This is a number, not a valid remote file url. Please try again with a valid URL.');
44+
if (!$this->option('tags') && !$this->option('public_ids')) {
45+
$this->warn(
46+
'Please ensure you pass in at least a tag with --tags, or at least a public_id with --public_ids'
47+
);
4348

4449
return;
4550
}
4651

47-
if (!filter_var($this->argument('remote-url'), FILTER_VALIDATE_URL)) {
48-
$this->warn('Please add a valid remote file url as an argument.');
49-
50-
return;
51-
}
52-
53-
$remoteUrl = $this->argument('remote-url');
54-
55-
$this->info('Extracting remote file...');
52+
$this->info('Generating Archive...');
5653

5754
try {
58-
$engine->uploadFile($remoteUrl);
59-
$this->info('Uploading in progress...');
55+
$response = $engine->createArchive(
56+
[
57+
'tags' => $this->option('tags') ?? null,
58+
'public_ids' => $this->option('public_ids') ?? null
59+
]
60+
)['secure_url'];
6061

61-
$this->info('Upload to Cloudinary completed!');
62+
$this->info("Archive: {$response}");
6263
} catch (Exception $exception) {
6364
$this->warn("Backup of files to Cloudinary failed because: {$exception->getMessage()}.");
6465
}

src/Commands/GenerateZipCommand.php

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/Commands/RenameFilesCommand.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Unicodeveloper\Cloudinary\Commands;
44

5+
use Exception;
56
use Illuminate\Console\Command;
6-
use Illuminate\Support\Facades\File;
77
use Unicodeveloper\Cloudinary\CloudinaryEngine;
88

99
/**
@@ -33,7 +33,7 @@ class RenameFilesCommand extends Command
3333
*/
3434
public function handle(CloudinaryEngine $engine)
3535
{
36-
if(! config('cloudinary.cloud_url')) {
36+
if (!config('cloudinary.cloud_url')) {
3737
$this->warn('Please ensure your Cloudinary credentials are set before continuing.');
3838

3939
return;
@@ -42,16 +42,15 @@ public function handle(CloudinaryEngine $engine)
4242
$fromPublicId = $this->argument('fromPublicId');
4343
$toPublicId = $this->argument('toPublicId');
4444

45-
if (! is_string($fromPublicId) || ! is_string($toPublicId)) {
46-
$this->warn("Please ensure a valid public Id is passed as an argument.");
45+
if (!is_string($fromPublicId) || !is_string($toPublicId)) {
46+
$this->warn("Please ensure a valid public Id is passed as an argument.");
4747

48-
return;
48+
return;
4949
}
5050

5151
$this->info("About to rename {$fromPublicId} file to {$toPublicId} on Cloudinary...");
5252

5353
try {
54-
5554
$engine->rename($fromPublicId, $toPublicId);
5655

5756
$this->info('File renamed successfully on Cloudinary!');

src/Commands/UploadFileCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Unicodeveloper\Cloudinary\Commands;
44

5+
use Exception;
56
use Illuminate\Console\Command;
67
use Unicodeveloper\Cloudinary\CloudinaryEngine;
78

0 commit comments

Comments
 (0)