Skip to content

Commit

Permalink
- Fixed tests.
Browse files Browse the repository at this point in the history
- Automatically saves model to DB.
  • Loading branch information
tylercd100 committed Aug 25, 2016
1 parent 4d10736 commit 026fc9e
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 132 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Expand Up @@ -2,5 +2,9 @@

All notable changes to `laravel-upload` will be documented in this file.

### 1.0.0
- Initial release and connected with packagist
### 0.0.2
- Fixed tests.
- Automatically saves model to DB.

### 0.0.1
- Initial release and connected with packagist.
8 changes: 4 additions & 4 deletions migrations/2016_01_01_000000_create_uploads_tables.php
Expand Up @@ -3,7 +3,8 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateUploadsTables extends Migration {
class CreateUploadsTables extends Migration
{

/**
* Run the migrations.
Expand All @@ -12,7 +13,7 @@ class CreateUploadsTables extends Migration {
*/
public function up()
{
Schema::create(config('upload.table'), function(Blueprint $table) {
Schema::create(config('upload.table'), function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('mime');
$table->string('path');
Expand All @@ -33,5 +34,4 @@ public function down()
{
Schema::drop(config('upload.table'));
}

}
}
2 changes: 1 addition & 1 deletion src/Facades/Upload.php
Expand Up @@ -18,4 +18,4 @@ protected static function getFacadeAccessor()
{
return 'upload';
}
}
}
32 changes: 16 additions & 16 deletions src/Models/File.php
@@ -1,28 +1,28 @@
<?php
<?php

namespace Zenapply\Upload\Models;

use Illuminate\Database\Eloquent\Model;

class File extends Model
{
protected $table;
protected $guarded = ['id'];
protected $table;
protected $guarded = ['id'];

public function __construct(array $attributes = [])
public function __construct(array $attributes = [])
{
if(empty($this->table)){
$this->table = config('upload.table');
}
if (empty($this->table)) {
$this->table = config('upload.table');
}
parent::__construct($attributes);
}

// [
// 'mime'
// 'path'
// 'disk'
// 'filename'
// 'extension'
// 'fingerprint'
// ]
}
// [
// 'mime'
// 'path'
// 'disk'
// 'filename'
// 'extension'
// 'fingerprint'
// ]
}
21 changes: 13 additions & 8 deletions src/Providers/Upload.php
Expand Up @@ -7,20 +7,25 @@

class Upload extends ServiceProvider
{
public function register() {
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../../config/upload.php', 'upload');

$this->app->singleton('upload', function() {
return new UploadMaster;
});
$this->app->singleton(
'upload',
function () {
return new UploadMaster;
}
);
}

public function boot()
{
$this->publishes([
$this->publishes(
[
__DIR__ . '/../../config/upload.php' => base_path('config/upload.php'),
__DIR__ . '/../../migrations/2016_01_01_000000_create_uploads_tables.php' => base_path('database/migrations/2016_01_01_000000_create_uploads_tables.php'),

]);
]
);
}
}
}
139 changes: 74 additions & 65 deletions src/Upload.php
Expand Up @@ -7,69 +7,78 @@
use Exception;
use Zenapply\Upload\Models\File;

class Upload {

protected $config = [];

public function __construct(){
$this->config = config('upload');

}

public function create(UploadedFile $file){
$disk = $this->config['disk'];
$filename = $this->getRandomString(32);
$extension = $file->extension();
$directory = $this->getRandomDirectory();
$contents = file_get_contents($file);
$mime = mime_content_type($file->path());
$fingerprint = md5_file($file->path());

$path = "/{$directory}/{$filename}.{$extension}";

$result = $this->getDisk()->put($path, $contents);

if($result === false){
throw new Exception("Could not save the file on the disk! Disk: ".$disk);
}

$model = new File([
"filename" => $filename,
"mime" => $mime,
"path" => $path,
"disk" => $disk,
"extension" => $extension,
"fingerprint" => $fingerprint,
]);

return $model;
}

public function read($id){
// Code go here
}

public function destroy($id){
// Code go here
}

protected function getDisk(){
return Storage::disk($this->config['disk']);
}

protected function getRandomString($len = 16){
$alphanum = array_merge(range('a','z'),range('0','9'));
$str = "";
for($i = 0; $i < $len; $i++){
$str.=$alphanum[array_rand($alphanum)];
}
return $str;
}

protected function getRandomDirectory(){
$directory1 = $this->getRandomString(1);
$directory2 = $this->getRandomString(1);
return "{$directory1}/{$directory2}";
}
class Upload
{
protected $config = [];

public function __construct()
{
$this->config = config('upload');
}

public function create(UploadedFile $file)
{
$disk = $this->config['disk'];
$filename = $this->getRandomString(32);
$extension = $file->extension();
$directory = $this->getRandomDirectory();
$contents = file_get_contents($file);
$mime = mime_content_type($file->path());
$fingerprint = md5_file($file->path());

$path = "/{$directory}/{$filename}.{$extension}";

$result = $this->getDisk()->put($path, $contents);

if ($result === false) {
throw new Exception("Could not save the file on the disk! Disk: ".$disk);
}

$model = new File(
[
"filename" => $filename,
"mime" => $mime,
"path" => $path,
"disk" => $disk,
"extension" => $extension,
"fingerprint" => $fingerprint,
]
);

$model->save();

return $model;
}

public function read($id)
{
// Code go here
}

public function destroy($id)
{
// Code go here
}

protected function getDisk()
{
return Storage::disk($this->config['disk']);
}

protected function getRandomString($len = 16)
{
$alphanum = array_merge(range('a', 'z'), range('0', '9'));
$str = "";
for ($i = 0; $i < $len; $i++) {
$str.=$alphanum[array_rand($alphanum)];
}
return $str;
}

protected function getRandomDirectory()
{
$directory1 = $this->getRandomString(1);
$directory2 = $this->getRandomString(1);
return "{$directory1}/{$directory2}";
}
}

35 changes: 23 additions & 12 deletions tests/TestCase.php
Expand Up @@ -37,42 +37,53 @@ protected function getPackageAliases($app)
];
}

protected function migrate(){
$this->artisan('migrate', [
protected function migrate()
{
$this->artisan(
'migrate', [
'--database' => 'testbench',
'--realpath' => realpath(__DIR__.'/migrations'),
]);
'--realpath' => realpath(__DIR__.'/../migrations'),
]
);
}

protected function migrateReset(){
protected function migrateReset()
{
$this->artisan('migrate:reset');
}

/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('upload', [
$app['config']->set(
'upload', [
'table' => 'uploads',
'disk'=>'test'
]);
]
);

$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
$app['config']->set(
'database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
]
);

$app['config']->set('filesystems.disks', [
$app['config']->set(
'filesystems.disks', [
'test' => [
'driver' => 'local',
'root' => __DIR__.'/tmp',
],
]);
]
);
}

/**
Expand Down

0 comments on commit 026fc9e

Please sign in to comment.