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

Commit e121be1

Browse files
Done with first draft 🚧
1 parent 64de604 commit e121be1

File tree

4 files changed

+240
-1
lines changed

4 files changed

+240
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateMediaTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('media', function (Blueprint $table) {
12+
$table->bigIncrements('id');
13+
$table->morphs('medially');
14+
$table->text('file_url');
15+
$table->string('file_name');
16+
$table->string('file_type')->nullable();
17+
$table->unsignedBigInteger('size');
18+
$table->timestamps();
19+
});
20+
}
21+
}

src/CloudinaryServiceProvider.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function boot()
2525
{
2626
$this->bootMacros();
2727
$this->bootResources();
28+
$this->bootMigrations();
2829
$this->bootDirectives();
2930
$this->bootComponents();
3031
$this->bootCommands();
@@ -42,6 +43,8 @@ public function register()
4243
$this->app->singleton(CloudinaryEngine::class, function ($app) {
4344
return new CloudinaryEngine;
4445
});
46+
47+
//$this->app->alias('cloudinary.engine', CloudinaryEngine::class);
4548
}
4649

4750

@@ -89,6 +92,20 @@ protected function bootResources()
8992
$this->loadViewsFrom(__DIR__.'/../resources/views', 'cloudinary');
9093
}
9194

95+
/**
96+
* Boot the package migrations.
97+
*
98+
* @return void
99+
*/
100+
protected function bootMigrations()
101+
{
102+
if ($this->app->runningInConsole()) {
103+
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
104+
}
105+
}
106+
107+
108+
92109
/**
93110
* Boot the package directives.
94111
*
@@ -121,7 +138,11 @@ protected function bootMacros()
121138
$engine = new CloudinaryEngine;
122139

123140
UploadedFile::macro('storeOnCloudinary', function ($folder = null) use ($engine) {
124-
return $engine->uploadFile($this->getRealPath())->getSecurePath();
141+
return $engine->uploadFile($this->getRealPath(), ['folder' => $folder])->getSecurePath();
142+
});
143+
144+
UploadedFile::macro('storeOnCloudinaryAs', function ($folder = null, $publicId = null) use ($engine) {
145+
return $engine->uploadFile($this->getRealPath(), ['folder' => $folder, 'public_id' => $publicId])->getSecurePath();
125146
});
126147
}
127148
}

src/MediaAlly.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace Unicodeveloper\Cloudinary;
4+
5+
use Exception;
6+
use Unicodeveloper\Cloudinary\Model\Media;
7+
8+
9+
/**
10+
* MediaAlly
11+
*
12+
* Provides functionality for attaching Cloudinary files to an eloquent model.
13+
* Whether the model should automatically reload its media relationship after modification.
14+
*
15+
*
16+
*/
17+
trait MediaAlly
18+
{
19+
20+
/**
21+
* Relationship for all attached media.
22+
*/
23+
public function medially()
24+
{
25+
return $this->morphMany(Media::class, 'medially');
26+
}
27+
28+
29+
/**
30+
* Attach Media Files to a Model
31+
*/
32+
public function attachMedia($file)
33+
{
34+
if(! file_exists($file)) {
35+
throw new Exception('Please pass in a file that exists');
36+
}
37+
38+
$response = resolve(CloudinaryEngine::class)->uploadFile($file->getRealPath());
39+
40+
$media = new Media();
41+
$media->file_name = $response->getFileName();
42+
$media->file_url = $response->getSecurePath();
43+
$media->size = $response->getSize();
44+
$media->file_type = $response->getFileType();
45+
46+
$this->medially()->save($media);
47+
}
48+
49+
/**
50+
* Attach Rwmote Media Files to a Model
51+
*/
52+
public function attachRemoteMedia($remoteFile)
53+
{
54+
$response = resolve(CloudinaryEngine::class)->uploadFile($remoteFile);
55+
56+
$media = new Media();
57+
$media->file_name = $response->getFileName();
58+
$media->file_url = $response->getSecurePath();
59+
$media->size = $response->getSize();
60+
$media->file_type = $response->getFileType();
61+
62+
$this->medially()->save($media);
63+
}
64+
65+
/**
66+
* Get all the Media files relating to a particular Model record
67+
*/
68+
public function fetchAllMedia()
69+
{
70+
return $this->medially()->get();
71+
}
72+
73+
/**
74+
* Get the first Media file relating to a particular Model record
75+
*/
76+
public function fetchFirstMedia()
77+
{
78+
return $this->medially()->first();
79+
}
80+
81+
/**
82+
* Delete all files associated with a particular Model record
83+
*/
84+
public function detachMedia()
85+
{
86+
87+
$items = $this->medially()->get();
88+
89+
foreach($items as $item) {
90+
resolve(CloudinaryEngine::class)->destroy($item->getFileName());
91+
}
92+
93+
return $this->medially()->delete();
94+
}
95+
96+
/**
97+
* Get the last Media file relating to a particular Model record
98+
*/
99+
public function fetchLastMedia()
100+
{
101+
return $this->medially()->get()->last();
102+
}
103+
104+
/**
105+
* Update the Media files relating to a particular Model record
106+
*/
107+
public function updateMedia($file)
108+
{
109+
$this->detachMedia();
110+
$this->attachMedia($file);
111+
}
112+
113+
/**
114+
* Update the Media files relating to a particular Model record (Specificially existing remote files)
115+
*/
116+
public function updateRemoteMedia($file)
117+
{
118+
$this->detachMedia();
119+
$this->attachRemoteMedia($file);
120+
}
121+
122+
}

src/Model/Media.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Unicodeveloper\Cloudinary\Model;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
use Unicodeveloper\Cloudinary\CloudinaryEngine;
7+
8+
use Carbon\Carbon;
9+
use Illuminate\Contracts\Filesystem\Filesystem;
10+
use Illuminate\Database\Eloquent\Builder;
11+
use Illuminate\Database\Eloquent\Model;
12+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
13+
use Illuminate\Database\Eloquent\Relations\Pivot;
14+
use Illuminate\Database\Eloquent\SoftDeletingScope;
15+
16+
class Media extends Model
17+
{
18+
19+
protected $table = 'media';
20+
21+
/**
22+
* Create the polymorphic relation.
23+
*
24+
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
25+
*/
26+
public function medially()
27+
{
28+
return $this->morphTo();
29+
}
30+
31+
/**
32+
* Get the file url / path of a Media File
33+
* @return string
34+
*/
35+
public function getSecurePath()
36+
{
37+
return $this->file_url;
38+
}
39+
40+
/**
41+
* Get the file name of a Media File
42+
* @return string
43+
*/
44+
public function getFileName()
45+
{
46+
return $this->file_name;
47+
}
48+
49+
/**
50+
* Get the mime type of a Media File
51+
* @return string
52+
*/
53+
public function getFileType()
54+
{
55+
return $this->file_type;
56+
}
57+
58+
/**
59+
* Get the Size of a Media File
60+
* @return Integer
61+
*/
62+
public function getSize()
63+
{
64+
return $this->size;
65+
}
66+
67+
/**
68+
* Get the Readable Size of a Media File
69+
* @return string
70+
*/
71+
public function getReadableSize()
72+
{
73+
return resolve(CloudinaryEngine::class)->getHumanReadableSize($this->size);
74+
}
75+
}

0 commit comments

Comments
 (0)