Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit ab17a8c

Browse files
release v1.0.12
1 parent 5d533e8 commit ab17a8c

File tree

12 files changed

+648
-209
lines changed

12 files changed

+648
-209
lines changed

README.md

Lines changed: 257 additions & 87 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
}
3333
},
3434
"require": {
35-
"php": "^7.1.0"
35+
"php": "^7.1.0",
36+
"aminyazdanpanah/php-ffmpeg-video-streaming": "dev-master",
37+
"gumlet/php-image-resize": "1.9.*",
38+
"wapmorgan/unified-archive": "~0.1.2"
3639
},
3740
"suggest": {
3841
"aminyazdanpanah/php-ffmpeg-video-streaming": "aminyazdanpanah/php-ffmpeg-video-streaming is suggested for validate video files and create DASH and HLS files."

examples/example.php

Lines changed: 248 additions & 38 deletions
Large diffs are not rendered by default.

src/File.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function file($filename)
4242
private function getFile()
4343
{
4444
if (!isset($_FILES[$this->filename])) {
45-
throw new Exception("There is no \"" .$this->filename . "\" key in the \$_FILES. Be sure your key name is correct", 500);
45+
throw new Exception("There is no \"" .$this->filename . "\" key in the \$_FILES. Be sure your key name is correct");
4646
}
4747

4848
if($code = $_FILES[$this->filename]['error']){
@@ -115,31 +115,31 @@ private function throwErrorException($code)
115115
{
116116
switch ($code){
117117
case 1:
118-
throw new Exception("The uploaded file exceeds the upload_max_filesize directive in php.ini",500);
118+
throw new Exception("The uploaded file exceeds the upload_max_filesize directive in php.ini");
119119
break;
120120
case 2:
121-
throw new Exception("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",500);
121+
throw new Exception("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form");
122122
break;
123123
case 3:
124-
throw new Exception("The uploaded file was only partially uploaded",500);
124+
throw new Exception("The uploaded file was only partially uploaded");
125125
break;
126126
case 4:
127-
throw new Exception("No file was uploaded",500);
127+
throw new Exception("No file was uploaded");
128128
break;
129129
case 5:
130-
throw new Exception("Unknown error occurred.",500);
130+
throw new Exception("Unknown error occurred.");
131131
break;
132132
case 6:
133-
throw new Exception("Missing a temporary folder.",500);
133+
throw new Exception("Missing a temporary folder.");
134134
break;
135135
case 7:
136-
throw new Exception("Failed to write file to disk",500);
136+
throw new Exception("Failed to write file to disk");
137137
break;
138138
case 8:
139-
throw new Exception("A PHP extension stopped the file upload",500);
139+
throw new Exception("A PHP extension stopped the file upload");
140140
break;
141141
default:
142-
throw new Exception("Unknown error occurred.",500);
142+
throw new Exception("Unknown error occurred.");
143143
}
144144

145145

src/Filter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
namespace AYazdanpanah\SaveUploadedFiles;
2020

2121

22-
class Filter
22+
class Filter implements \Countable
2323
{
2424
private $extractions;
2525

src/Helper.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2019 Amin Yazdanpanah<http://www.aminyazdanpanah.com>.
5+
*
6+
* Licensed under the MIT License;
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/MIT
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace AYazdanpanah\SaveUploadedFiles;
20+
21+
22+
class Helper
23+
{
24+
/**
25+
* @param $type
26+
* @param $filename
27+
* @return bool
28+
*/
29+
public static function isType($type, $filename): bool
30+
{
31+
return boolval(strstr(mime_content_type($filename), $type));
32+
}
33+
34+
/**
35+
* @param int $length
36+
* @return bool|string
37+
*/
38+
public static function str_random($length = 10)
39+
{
40+
return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length);
41+
}
42+
43+
/**
44+
* @return array
45+
*/
46+
public static function videoTypes(): array
47+
{
48+
return [
49+
'webm', 'mkv', 'flv', 'vob', 'ogv', 'ogg', 'drc', 'gif', 'gifv', 'avi',
50+
'MTS', 'M2TS', 'mov', 'qt', 'yuv', 'rm', 'rmvb', 'asf', 'amv', 'mp4',
51+
'm4p ', 'm4v', 'mpg', 'mp2', 'mpeg', 'mpe', 'mpg', 'mpeg', 'm2v', '3gp',
52+
'3g2', 'mxf', 'roq', 'nsv', 'flv', 'f4v', 'f4p', 'f4a', 'f4b',
53+
];
54+
}
55+
}

src/Save.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ abstract class Save implements FileInterface
4949
*/
5050
private $save_as;
5151

52+
/**
53+
* @var mixed
54+
*/
55+
private $export_data;
56+
5257
/**
5358
* Save constructor.
5459
* @param Validator $validator
@@ -60,18 +65,20 @@ public function __construct(Validator $validator)
6065

6166
/**
6267
* @param $path
68+
* @param callable $export
6369
* @return array
6470
*/
65-
public function save($path)
71+
public function save($path, callable $export)
6672
{
6773
$this->path = $path;
6874

6975
try {
7076
$this->validate();
7177
$this->moveFile();
72-
return $this->message(true, "The file \"" . $this->getBaseNameFile() . "\" has been uploaded.");
78+
$this->export($export);
79+
return $this->output(true, "The file \"" . $this->getBaseNameFile() . "\" has been uploaded.");
7380
} catch (SaveExceptionInterface $e) {
74-
return $this->message(false, $e->getMessage());
81+
return $this->output(false, $e->getMessage());
7582
}
7683
}
7784

@@ -112,19 +119,19 @@ private function moveFile()
112119

113120
/**
114121
* @param $status
115-
* @param $message
122+
* @param $output
116123
* @return array
117124
*/
118-
private function message($status, $message)
125+
private function output($status, $output)
119126
{
120-
$message = [
127+
$output = [
121128
'status' => $status,
122-
'message' => $message,
129+
'output' => $output,
123130
];
124131

125132
try {
126-
$message = array_merge(
127-
$message,
133+
$output = array_merge(
134+
$output,
128135
[
129136
'file_details' => [
130137
'name' => $this->getFileName(),
@@ -137,13 +144,16 @@ private function message($status, $message)
137144
'dir_path' => $this->path,
138145
'file_path' => $this->file_path,
139146
'upload_datetime' => date("Y-m-d H:i:s"),
140-
'stat' => stat($this->file_path)
147+
'stat' => !is_file($this->file_path)?:stat($this->file_path),
148+
'mime_content_type' => !is_file($this->file_path)?:mime_content_type($this->file_path),
149+
'export' => $this->export_data,
141150
]
142151
]
143152
);
144-
} catch (Exception $e) {}
153+
} catch (Exception $e) {
154+
}
145155

146-
return $message;
156+
return $output;
147157
}
148158

149159
/**
@@ -181,4 +191,9 @@ public function getSaveAs()
181191
{
182192
return $this->save_as;
183193
}
194+
195+
private function export(callable $export)
196+
{
197+
$this->export_data = $export($this->file_path);
198+
}
184199
}

src/Files.php renamed to src/Upload.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
use AYazdanpanah\SaveUploadedFiles\Exception\Exception;
2323

24-
class Files
24+
class Upload
2525
{
2626
/**
2727
* @param $files
@@ -31,19 +31,24 @@ class Files
3131
public static function files($files)
3232
{
3333
if (!is_array($files) && count($files) > 0) {
34-
throw new Exception("File must be an array", 500);
34+
throw new Exception("File must be an array");
3535
}
3636

3737
$extractions = [];
3838

3939
foreach ($files as $file) {
4040
if (!isset($file['name'], $file['save_to'])) {
41-
throw new Exception("Filename or path is not specified", 500);
41+
throw new Exception("Filename or path is not specified");
4242
}
4343

4444
$validator = static::validator(static::mockValidator());
4545
$override = false;
4646
$save_as = null;
47+
$export = static::mockCallback();
48+
49+
if(isset($file['export'])){
50+
$export = $file['export'];
51+
}
4752

4853
if (isset($file['validator'])) {
4954
$validator = static::validator($file['validator']);
@@ -61,7 +66,7 @@ public static function files($files)
6166
->file($file['name'])
6267
->setOverride($override)
6368
->setSaveAs($save_as)
64-
->save($file['save_to']);
69+
->save($file['save_to'], $export);
6570
}
6671

6772
return new Filter($extractions);
@@ -74,26 +79,33 @@ public static function files($files)
7479
*/
7580
private static function validator($validator): Validator
7681
{
77-
if (!isset($validator['min_size'], $validator['max_size'], $validator['types']) || !is_array($validator['types'])) {
78-
throw new Exception("Invalid validator inputs: check min_size, max_size, and types again", 500);
82+
if (!isset($validator['min_size'], $validator['max_size'], $validator['allowed_extensions']) || !is_array($validator['allowed_extensions'])) {
83+
throw new Exception("Invalid validator inputs: check min_size, max_size, and types again");
7984
}
8085

8186
return (new Validator())
8287
->setMinSize($validator['min_size'])
8388
->setMaxSize($validator['max_size'])
84-
->setType($validator['types']);
89+
->setType($validator['allowed_extensions']);
8590
}
8691

8792
/**
8893
* @return array
8994
*/
90-
private static function mockValidator()
95+
private static function mockValidator():array
9196
{
9297
return [
9398
'min_size' => 1,
9499
'max_size' => 999999,
95-
'types' => ['*']
100+
'allowed_extensions' => ['*']
96101

97102
];
98103
}
104+
105+
private static function mockCallback(): callable
106+
{
107+
return function ($filename){
108+
return $filename;
109+
};
110+
}
99111
}

src/Validate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function validate()
3939
throw new Exception("The file size is " . intval($this->getFileSize() / 1024) . "KB! It must be at least " . intval($this->getMinSize() / 1024) . "KB");
4040
}
4141

42-
if (!in_array($this->getFileExtension(), $this->getType()) && !in_array("*", $this->getType())) {
42+
if (!in_array(strtolower($this->getFileExtension()), $this->getTypes()) && !in_array("*", $this->getTypes())) {
4343
throw new Exception("Sorry, the \"" . $this->getFileExtension() . "\" files are not allowed! Only " . implode(", ", $this->getType()) . " files are allowed. ");
4444
}
4545
}

src/Validator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function getMaxSize()
4444
/**
4545
* @return mixed
4646
*/
47-
public function getType()
47+
public function getTypes()
4848
{
4949
return $this->type;
5050
}

0 commit comments

Comments
 (0)