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

Commit aff3c26

Browse files
committed
add image type
1 parent 9bba7c2 commit aff3c26

File tree

11 files changed

+235
-45
lines changed

11 files changed

+235
-45
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"type": "library",
55
"require": {
66
"php": "^7.2",
7-
"laravel/framework": "^6.2"
7+
"laravel/framework": "^6.2",
8+
"intervention/image": "^2.5"
89
},
910
"keywords": [
1011
"file",

config/filemanager.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,25 @@
55

66
"types" => [
77
"default" => [
8-
"provider" => \AliGhale\FileManager\Types\File::class,
9-
"path" => "default_files/test/",
10-
"private" => false,
11-
"date_time_prefix" => true,
8+
"provider" => \AliGhale\FileManager\Types\File::class,
9+
"path" => "default_files/test/",
10+
"private" => false,
11+
"date_time_prefix" => true,
1212
"use_file_name_to_upload" => false,
13-
"secret" => "ashkdsjka#sdkdjfsj22188455$$#$%dsDFsdf",
14-
"download_link_expire" => 160, // minutes
13+
"secret" => "ashkdsjka#sdkdjfsj22188455$$#$%dsDFsdf",
14+
"download_link_expire" => 160, // minutes
15+
],
16+
17+
"image" => [
18+
"provider" => \AliGhale\FileManager\Types\Image::class,
19+
"path" => "images/upload/documents/",
20+
"sizes" => ["16", "24", "32", "64", "128", "320"],
21+
"thumb" => "320"
22+
],
23+
"profile" => [
24+
"parent" => "image",
25+
"path" => "images/upload/profiles/",
26+
"date_time_prefix" => false,
1527
],
16-
// "image" => [
17-
// //
18-
// ],
1928
],
2029
];

src/BaseType.php

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ abstract class BaseType
9191
*/
9292
private function __construct()
9393
{
94-
$type = config("filemanager.type");
95-
$config = filemanager_config();
96-
$this->setConfig($config);
97-
$this->fetchProperties();
94+
//
9895
}
9996

10097

@@ -118,7 +115,7 @@ public static function getInstance()
118115
* @param array|null $config
119116
* @return $this
120117
*/
121-
protected function fetchProperties(array $config = null)
118+
public function fetchProperties(array $config = null)
122119
{
123120
if (is_null($config)) $config = $this->getConfig();
124121

@@ -207,11 +204,12 @@ public function upload($file)
207204
protected function createFileRow($name = null)
208205
{
209206
$file = File::create([
210-
"name" => $name ?? $this->getName() ?? $this->generateRandomName(),
211-
"type" => $this->getType(),
212-
"path" => $this->getFilePath(),
213-
"format" => $this->getFormat(),
214-
"private" => $this->public ? false : true,
207+
"name" => $name ?? $this->getName() ?? $this->generateRandomName(),
208+
"file_name" => $this->getFileName(),
209+
"type" => $this->getType(),
210+
"base_path" => $this->getUploadPath(),
211+
"format" => $this->getFormat(),
212+
"private" => $this->public ? false : true,
215213
]);
216214
$this->setFile($file);
217215
return $file;
@@ -347,6 +345,17 @@ public function getPath()
347345
}
348346

349347

348+
/**
349+
* get full path
350+
*
351+
* @return string
352+
*/
353+
public function getFullPath()
354+
{
355+
return $this->getStorageFolder($this->getUploadPath());
356+
}
357+
358+
350359
/**
351360
* get upload location path
352361
* we append the prefix
@@ -399,6 +408,19 @@ public function getPrefix()
399408
}
400409

401410

411+
/**
412+
* get set prefix
413+
*
414+
* @param $prefix
415+
* @return string
416+
*/
417+
public function setPrefix($prefix)
418+
{
419+
$this->prefix = $prefix;
420+
return $this;
421+
}
422+
423+
402424
/**
403425
* check storage path
404426
*
@@ -501,7 +523,7 @@ public function isPublic()
501523
* @param array $parameters
502524
* @return string
503525
*/
504-
public function getFilePath($parameters = [])
526+
public function getFilePath()
505527
{
506528
return $this->getUploadPath() . $this->getFileName();
507529
}

src/Controllers/DownloadController.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,32 @@ class DownloadController
2121
* @return BinaryFileResponse
2222
* @throws InternalErrorException
2323
*/
24-
public function download($id)
24+
public function download($file)
2525
{
2626
/** @var File $file */
2727
$file = File::query()
28-
->findOrFail($id);
28+
->where("id", $file)
29+
->orWhere("name", $file)
30+
->firstOrFail();
2931

3032
$config = filemanager_config();
3133

32-
$secret = "";
33-
if ($config['secret']) {
34-
$secret = $config['secret'];
35-
}
36-
37-
$hash = $secret . $file->id . request()->ip() . request('t');
38-
39-
if ((Carbon::createFromTimestamp(request('t')) > Carbon::now()) &&
40-
Hash::check($hash, request('mac'))) {
34+
if ($file->isPublic) {
4135
return $file->download();
4236
} else {
43-
throw new InternalErrorException("link not valid");
37+
$secret = "";
38+
if ($config['secret']) {
39+
$secret = $config['secret'];
40+
}
41+
42+
$hash = $secret . $file->id . request()->ip() . request('t');
43+
44+
if ((Carbon::createFromTimestamp(request('t')) > Carbon::now()) &&
45+
Hash::check($hash, request('mac'))) {
46+
return $file->download();
47+
} else {
48+
throw new InternalErrorException("link not valid");
49+
}
4450
}
4551
}
4652
}

src/Facades/File.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @package AliGhale\FileManager\Facades
1515
*
1616
* @method static BaseType useFileNameToUpload($status = true)
17-
* @method static upload($file)
17+
* @method static BaseType upload($file)
1818
* @method static \AliGhale\FileManager\Models\File getFile($name = null)
1919
* @method static BaseType setFile(File $file)
2020
* @method static BaseType setConfig(array $config)
@@ -26,6 +26,7 @@
2626
* @method static getUploadPath()
2727
* @method static BaseType dateTimePrefix($value = true)
2828
* @method static getPrefix()
29+
* @method static BaseType setPrefix($prefix)
2930
* @method static getStorageFolder($src)
3031
* @method static BaseType setName(string $name)
3132
* @method static getName()
@@ -35,6 +36,8 @@
3536
* @method static BaseType isPublic()
3637
* @method static getFilePath($parameters = [])
3738
* @method static getFileName()
39+
* @method static BaseType fetchProperties(array $config = null)
40+
* @method static BaseType type($type = null)
3841
*/
3942
class File extends Facade
4043
{

src/File.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,29 @@
99

1010
class File
1111
{
12+
/**
13+
* select type
14+
*
15+
* @param null $type
16+
* @return \AliGhale\FileManager\BaseType
17+
*/
1218
public function type($type = null)
1319
{
1420
if (is_null($type))
1521
$type = config("filemanager.type");
1622

17-
$typeConfig = config("filemanager.types.{$type}");
23+
$config = filemanager_config($type);
1824

1925
/** @var BaseType $providerClass */
20-
$providerClass = $typeConfig['provider'];
26+
$providerClass = $config['provider'];
2127
$providerClass = $providerClass::getInstance();
22-
$providerClass->setType($type);
28+
$providerClass->setType($type)
29+
->setConfig($config)
30+
->fetchProperties();
2331
return $providerClass;
2432
}
2533

34+
2635
public function __call($method, $parameters)
2736
{
2837
return $this->type()->$method(...$parameters);

src/FileManagerServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public function registerConfig()
3333

3434
public function registerMigrations()
3535
{
36-
$this->loadMigrationsFrom(__DIR__ . '/Migrations');
3736
$this->publishes([
38-
__DIR__ . '/Migrations/' => database_path('migrations')
37+
realpath(__DIR__ . '/Migrations') => database_path('migrations')
3938
], 'migrations');
39+
$this->loadMigrationsFrom(realpath(__DIR__ . '/Migrations'));
4040
}
4141

4242
protected function registerRoutes()

src/Migrations/2020_02_18_155709_serjik_file_manager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public function up()
1717
Schema::create('files', function (Blueprint $table) {
1818
$table->bigIncrements('id');
1919
$table->string('name')->unique();
20-
$table->text('path');
20+
$table->string('file_name')->unique();
21+
$table->text('base_path');
2122
$table->tinyInteger('private')->default(0);
2223
$table->string("type")->default("default");
2324
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

src/Models/File.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
*
1717
* @property $id
1818
* @property $name
19-
* @property $path
19+
* @property $file_name
20+
* @property $base_path
2021
* @property $private
2122
* @property $isPrivate
2223
* @property $isPublic
@@ -30,7 +31,8 @@ class File extends Model
3031
protected $fillable = [
3132
"id",
3233
"name",
33-
"path",
34+
"file_name",
35+
"base_path",
3436
"private",
3537
"type",
3638
"created_at",
@@ -46,6 +48,17 @@ public function getIsPrivateAttribute()
4648
{
4749
return $this->private ? true : false;
4850
}
51+
52+
53+
/**
54+
* is private this file
55+
*
56+
* @return bool
57+
*/
58+
public function getPathAttribute()
59+
{
60+
return $this->base_path . $this->file_name;
61+
}
4962

5063

5164
/**
@@ -75,7 +88,7 @@ public function generateLink()
7588
}
7689

7790
if (isset($config['download_link_expire'])) {
78-
$expireTime = (int) $config['download_link_expire'];
91+
$expireTime = (int)$config['download_link_expire'];
7992
}
8093

8194
/** @var int $expireTime */

0 commit comments

Comments
 (0)