Skip to content

Commit

Permalink
fixing file upload bug
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Jul 23, 2016
1 parent cdd2c37 commit 7e8283b
Showing 1 changed file with 51 additions and 63 deletions.
114 changes: 51 additions & 63 deletions src/Frozennode/Administrator/Fields/File.php
@@ -1,77 +1,65 @@
<?php

namespace Frozennode\Administrator\Fields;

use Frozennode\Administrator\Includes\Multup;

class File extends Field
{
/**
* The specific defaults for subclasses to override.
*
* @var array
*/
protected $defaults = array(
'naming' => 'random',
'length' => 32,
'mimes' => false,
'size_limit' => 2,
'display_raw_value' => false,
'path' => '/',
);

/**
* The specific rules for subclasses to override.
*
* @var array
*/
protected $rules = array(
'path' => 'string',
'length' => 'integer|min:0',
'mimes' => 'string',
);
class File extends Field {

/**
* Builds a few basic options.
*/
public function build()
{
parent::build();
/**
* The specific defaults for subclasses to override
*
* @var array
*/
protected $defaults = array(
'naming' => 'random',
'length' => 32,
'mimes' => false,
'size_limit' => 2,
'display_raw_value' => false,
);

//set the upload url depending on the type of config this is
$url = $this->validator->getUrlInstance();
$route = $this->config->getType() === 'settings' ? 'admin_settings_file_upload' : 'admin_file_upload';
/**
* The specific rules for subclasses to override
*
* @var array
*/
protected $rules = array(
'location' => 'required|string|directory',
'naming' => 'in:keep,random',
'length' => 'integer|min:0',
'mimes' => 'string',
);

//set the upload url to the proper route
$this->suppliedOptions['upload_url'] = $url->route($route, array($this->config->getOption('name'), $this->suppliedOptions['field_name']));
}
/**
* Builds a few basic options
*/
public function build()
{
parent::build();

/**
* This static function is used to perform the actual upload and resizing using the Multup class.
*
* @return array
*/
public function doUpload()
{
$disk = \Storage::disk($this->getOption('disk'));
$file = \Input::file('file');
//set the upload url depending on the type of config this is
$url = $this->validator->getUrlInstance();
$route = $this->config->getType() === 'settings' ? 'admin_settings_file_upload' : 'admin_file_upload';

$ext = $file->getClientOriginalExtension() ?: $file->guessClientExtension();
//set the upload url to the proper route
$this->suppliedOptions['upload_url'] = $url->route($route, array($this->config->getOption('name'), $this->suppliedOptions['field_name']));
}

$filename = md5_file($file->getRealPath()).'.'.$ext;
$put_path = trim($this->getOption('path'), ' /').'/'.$filename;
/**
* This static function is used to perform the actual upload and resizing using the Multup class
*
* @return array
*/
public function doUpload()
{
$mimes = $this->getOption('mimes') ? '|mimes:' . $this->getOption('mimes') : '';

if (!$disk->exists($put_path)) {
$f_res = fopen($file->getRealPath(), 'r');
$disk->put($put_path, $f_res);
fclose($f_res);
}
//use the multup library to perform the upload
$result = Multup::open('file', 'max:' . $this->getOption('size_limit') * 1000 . $mimes, $this->getOption('location'),
$this->getOption('naming') === 'random')
->set_length($this->getOption('length'))
->upload();

return [
'errors' => [],
'path' => trim($put_path, '/'),
'filename' => trim($put_path, '/'),
'original_name' => $file->getClientOriginalName(),
];
}
return $result[0];
}
}

0 comments on commit 7e8283b

Please sign in to comment.