Skip to content
This repository has been archived by the owner on Jul 9, 2023. It is now read-only.

Commit

Permalink
add image type
Browse files Browse the repository at this point in the history
  • Loading branch information
AliGhaleyan committed Feb 27, 2020
1 parent 9bba7c2 commit aff3c26
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 45 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"type": "library",
"require": {
"php": "^7.2",
"laravel/framework": "^6.2"
"laravel/framework": "^6.2",
"intervention/image": "^2.5"
},
"keywords": [
"file",
Expand Down
27 changes: 18 additions & 9 deletions config/filemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@

"types" => [
"default" => [
"provider" => \AliGhale\FileManager\Types\File::class,
"path" => "default_files/test/",
"private" => false,
"date_time_prefix" => true,
"provider" => \AliGhale\FileManager\Types\File::class,
"path" => "default_files/test/",
"private" => false,
"date_time_prefix" => true,
"use_file_name_to_upload" => false,
"secret" => "ashkdsjka#sdkdjfsj22188455$$#$%dsDFsdf",
"download_link_expire" => 160, // minutes
"secret" => "ashkdsjka#sdkdjfsj22188455$$#$%dsDFsdf",
"download_link_expire" => 160, // minutes
],

"image" => [
"provider" => \AliGhale\FileManager\Types\Image::class,
"path" => "images/upload/documents/",
"sizes" => ["16", "24", "32", "64", "128", "320"],
"thumb" => "320"
],
"profile" => [
"parent" => "image",
"path" => "images/upload/profiles/",
"date_time_prefix" => false,
],
// "image" => [
// //
// ],
],
];
44 changes: 33 additions & 11 deletions src/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ abstract class BaseType
*/
private function __construct()
{
$type = config("filemanager.type");
$config = filemanager_config();
$this->setConfig($config);
$this->fetchProperties();
//
}


Expand All @@ -118,7 +115,7 @@ public static function getInstance()
* @param array|null $config
* @return $this
*/
protected function fetchProperties(array $config = null)
public function fetchProperties(array $config = null)
{
if (is_null($config)) $config = $this->getConfig();

Expand Down Expand Up @@ -207,11 +204,12 @@ public function upload($file)
protected function createFileRow($name = null)
{
$file = File::create([
"name" => $name ?? $this->getName() ?? $this->generateRandomName(),
"type" => $this->getType(),
"path" => $this->getFilePath(),
"format" => $this->getFormat(),
"private" => $this->public ? false : true,
"name" => $name ?? $this->getName() ?? $this->generateRandomName(),
"file_name" => $this->getFileName(),
"type" => $this->getType(),
"base_path" => $this->getUploadPath(),
"format" => $this->getFormat(),
"private" => $this->public ? false : true,
]);
$this->setFile($file);
return $file;
Expand Down Expand Up @@ -347,6 +345,17 @@ public function getPath()
}


/**
* get full path
*
* @return string
*/
public function getFullPath()
{
return $this->getStorageFolder($this->getUploadPath());
}


/**
* get upload location path
* we append the prefix
Expand Down Expand Up @@ -399,6 +408,19 @@ public function getPrefix()
}


/**
* get set prefix
*
* @param $prefix
* @return string
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
return $this;
}


/**
* check storage path
*
Expand Down Expand Up @@ -501,7 +523,7 @@ public function isPublic()
* @param array $parameters
* @return string
*/
public function getFilePath($parameters = [])
public function getFilePath()
{
return $this->getUploadPath() . $this->getFileName();
}
Expand Down
30 changes: 18 additions & 12 deletions src/Controllers/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,32 @@ class DownloadController
* @return BinaryFileResponse
* @throws InternalErrorException
*/
public function download($id)
public function download($file)
{
/** @var File $file */
$file = File::query()
->findOrFail($id);
->where("id", $file)
->orWhere("name", $file)
->firstOrFail();

$config = filemanager_config();

$secret = "";
if ($config['secret']) {
$secret = $config['secret'];
}

$hash = $secret . $file->id . request()->ip() . request('t');

if ((Carbon::createFromTimestamp(request('t')) > Carbon::now()) &&
Hash::check($hash, request('mac'))) {
if ($file->isPublic) {
return $file->download();
} else {
throw new InternalErrorException("link not valid");
$secret = "";
if ($config['secret']) {
$secret = $config['secret'];
}

$hash = $secret . $file->id . request()->ip() . request('t');

if ((Carbon::createFromTimestamp(request('t')) > Carbon::now()) &&
Hash::check($hash, request('mac'))) {
return $file->download();
} else {
throw new InternalErrorException("link not valid");
}
}
}
}
5 changes: 4 additions & 1 deletion src/Facades/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @package AliGhale\FileManager\Facades
*
* @method static BaseType useFileNameToUpload($status = true)
* @method static upload($file)
* @method static BaseType upload($file)
* @method static \AliGhale\FileManager\Models\File getFile($name = null)
* @method static BaseType setFile(File $file)
* @method static BaseType setConfig(array $config)
Expand All @@ -26,6 +26,7 @@
* @method static getUploadPath()
* @method static BaseType dateTimePrefix($value = true)
* @method static getPrefix()
* @method static BaseType setPrefix($prefix)
* @method static getStorageFolder($src)
* @method static BaseType setName(string $name)
* @method static getName()
Expand All @@ -35,6 +36,8 @@
* @method static BaseType isPublic()
* @method static getFilePath($parameters = [])
* @method static getFileName()
* @method static BaseType fetchProperties(array $config = null)
* @method static BaseType type($type = null)
*/
class File extends Facade
{
Expand Down
15 changes: 12 additions & 3 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,29 @@

class File
{
/**
* select type
*
* @param null $type
* @return \AliGhale\FileManager\BaseType
*/
public function type($type = null)
{
if (is_null($type))
$type = config("filemanager.type");

$typeConfig = config("filemanager.types.{$type}");
$config = filemanager_config($type);

/** @var BaseType $providerClass */
$providerClass = $typeConfig['provider'];
$providerClass = $config['provider'];
$providerClass = $providerClass::getInstance();
$providerClass->setType($type);
$providerClass->setType($type)
->setConfig($config)
->fetchProperties();
return $providerClass;
}


public function __call($method, $parameters)
{
return $this->type()->$method(...$parameters);
Expand Down
4 changes: 2 additions & 2 deletions src/FileManagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public function registerConfig()

public function registerMigrations()
{
$this->loadMigrationsFrom(__DIR__ . '/Migrations');
$this->publishes([
__DIR__ . '/Migrations/' => database_path('migrations')
realpath(__DIR__ . '/Migrations') => database_path('migrations')
], 'migrations');
$this->loadMigrationsFrom(realpath(__DIR__ . '/Migrations'));
}

protected function registerRoutes()
Expand Down
3 changes: 2 additions & 1 deletion src/Migrations/2020_02_18_155709_serjik_file_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function up()
Schema::create('files', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->unique();
$table->text('path');
$table->string('file_name')->unique();
$table->text('base_path');
$table->tinyInteger('private')->default(0);
$table->string("type")->default("default");
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
Expand Down
19 changes: 16 additions & 3 deletions src/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*
* @property $id
* @property $name
* @property $path
* @property $file_name
* @property $base_path
* @property $private
* @property $isPrivate
* @property $isPublic
Expand All @@ -30,7 +31,8 @@ class File extends Model
protected $fillable = [
"id",
"name",
"path",
"file_name",
"base_path",
"private",
"type",
"created_at",
Expand All @@ -46,6 +48,17 @@ public function getIsPrivateAttribute()
{
return $this->private ? true : false;
}


/**
* is private this file
*
* @return bool
*/
public function getPathAttribute()
{
return $this->base_path . $this->file_name;
}


/**
Expand Down Expand Up @@ -75,7 +88,7 @@ public function generateLink()
}

if (isset($config['download_link_expire'])) {
$expireTime = (int) $config['download_link_expire'];
$expireTime = (int)$config['download_link_expire'];
}

/** @var int $expireTime */
Expand Down
Loading

0 comments on commit aff3c26

Please sign in to comment.