forked from recca0120/upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilesystem.php
96 lines (82 loc) · 2.48 KB
/
Filesystem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace Recca0120\Upload;
use Recca0120\Upload\Exceptions\ResourceOpenException;
use Illuminate\Filesystem\Filesystem as IlluminateFilesystem;
class Filesystem extends IlluminateFilesystem
{
/**
* Extract the trailing name component from a file path.
*
* @param string $path
* @return string
*/
public function basename($path)
{
return pathinfo($path, PATHINFO_BASENAME);
}
/**
* tmpfilename.
*
* @param string $path
* @param string $hash
* @return string
*/
public function tmpfilename($path, $hash = null)
{
return md5($path.$hash).'.'.strtolower($this->extension($path));
}
/**
* appendStream.
*
* @param string $output
* @param string|resource $input
* @param int $offset
*/
public function appendStream($output, $input, $offset = 0)
{
$mode = ($offset === 0) ? 'wb' : 'ab';
$output = $this->convertToResource($output, $mode, 'output');
$input = $this->convertToResource($input, 'rb', 'input');
fseek($output, $offset);
while ($buffer = fread($input, 4096)) {
fwrite($output, $buffer);
}
fclose($output);
fclose($input);
}
/**
* createUploadedFile.
*
* @param string $path
* @param string $originalName
* @param string $mimeType
* @param int $size
* @return \Illuminate\Http\UploadedFile
*/
public function createUploadedFile($path, $originalName, $mimeType = null, $size = null)
{
$class = class_exists('Illuminate\Http\UploadedFile') === true ?
'Illuminate\Http\UploadedFile' : 'Symfony\Component\HttpFoundation\File\UploadedFile';
$mimeType = $mimeType ?: $this->mimeType($path);
$size = $size ?: $this->size($path);
return new $class($path, $originalName, $mimeType, $size, UPLOAD_ERR_OK, true);
}
/**
* convertToResource.
*
* @param string|resource $resource
* @param string $mode
* @param string $type
* @return resource
*/
protected function convertToResource($resource, $mode = 'wb', $type = 'input')
{
$resource = is_resource($resource) === true ?
$resource : @fopen($resource, $mode);
if (is_resource($resource) === false) {
$code = $type === 'input' ? 101 : 102;
throw new ResourceOpenException('Failed to open '.$type.' stream.', $code);
}
return $resource;
}
}