1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Canvas\Filesystem; |
6
|
|
|
|
7
|
|
|
use Phalcon\Http\Request\File; |
|
|
|
|
8
|
|
|
use Exception; |
9
|
|
|
use Canvas\Models\FileSystem; |
10
|
|
|
use Phalcon\Di; |
11
|
|
|
|
12
|
|
|
class Helper |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Generate a unique name in a specific dir. |
16
|
|
|
* |
17
|
|
|
* @param string $dir the especific dir where the file will be saved |
18
|
|
|
* @param bool $withPath |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public static function generateUniqueName(File $file, string $dir, $withPath = false) : string |
23
|
|
|
{ |
24
|
|
|
// the provided path has to be a dir |
25
|
|
|
if (!is_dir($dir)) { |
26
|
|
|
throw new Exception("The dir provided: '{$dir}' isn't a valid one."); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$path = tempnam($dir . '/', ''); |
30
|
|
|
|
31
|
|
|
//this function creates a file (like touch) so, we have to delete it. |
32
|
|
|
unlink($path); |
33
|
|
|
$uniqueName = $path; |
34
|
|
|
if (!$withPath) { |
35
|
|
|
$uniqueName = str_replace($dir, '', $path); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $uniqueName . '.' . strtolower($file->getExtension()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a File instance from a given path. |
43
|
|
|
* |
44
|
|
|
* @param string $path Path of the file to be used |
45
|
|
|
* |
46
|
|
|
* @return File |
47
|
|
|
*/ |
48
|
|
|
public static function pathToFile(string $path) : File |
49
|
|
|
{ |
50
|
|
|
//Simulate the body of a Phalcon\Request\File class |
51
|
|
|
return new File([ |
52
|
|
|
'name' => basename($path), |
53
|
|
|
'type' => mime_content_type($path), |
54
|
|
|
'tmp_name' => $path, |
55
|
|
|
'error' => 0, |
56
|
|
|
'size' => filesize($path), |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Given a file create it in the filesystem. |
62
|
|
|
* |
63
|
|
|
* @param File $file |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public static function upload(File $file): FileSystem |
67
|
|
|
{ |
68
|
|
|
$di = Di::getDefault(); |
69
|
|
|
$config = $di->get('config'); |
70
|
|
|
|
71
|
|
|
//get the filesystem config |
72
|
|
|
$appSettingFileConfig = $di->get('app')->get('filesystem'); |
73
|
|
|
$fileSystemConfig = $config->filesystem->{$appSettingFileConfig}; |
74
|
|
|
|
75
|
|
|
//create local filesystem , for temp files |
76
|
|
|
$di->get('filesystem', ['local'])->createDir($config->filesystem->local->path); |
77
|
|
|
|
78
|
|
|
//get the tem file |
79
|
|
|
$filePath = self::generateUniqueName($file, $config->filesystem->local->path); |
80
|
|
|
$compleFilePath = $fileSystemConfig->path . $filePath; |
81
|
|
|
$uploadFileNameWithPath = $appSettingFileConfig == 'local' ? $filePath : $compleFilePath; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* upload file base on temp. |
85
|
|
|
* @todo change this to determine type of file and recreate it if its a image |
86
|
|
|
*/ |
87
|
|
|
$di->get('filesystem')->writeStream($uploadFileNameWithPath, fopen($file->getTempName(), 'r')); |
88
|
|
|
|
89
|
|
|
$fileSystem = new FileSystem(); |
90
|
|
|
$fileSystem->name = $file->getName(); |
91
|
|
|
$fileSystem->companies_id = $di->get('userData')->currentCompanyId(); |
92
|
|
|
$fileSystem->apps_id = $di->get('app')->getId(); |
93
|
|
|
$fileSystem->users_id = $di->get('userData')->getId(); |
94
|
|
|
$fileSystem->path = $compleFilePath; |
95
|
|
|
$fileSystem->url = $fileSystemConfig->cdn . DIRECTORY_SEPARATOR . $uploadFileNameWithPath; |
96
|
|
|
$fileSystem->file_type = $file->getExtension(); |
97
|
|
|
$fileSystem->size = $file->getSize(); |
98
|
|
|
|
99
|
|
|
$fileSystem->saveOrFail(); |
100
|
|
|
|
101
|
|
|
return $fileSystem; |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths