Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Http;
4
 
5
use Illuminate\Container\Container;
6
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
7
use Illuminate\Contracts\Filesystem\FileNotFoundException;
8
use Illuminate\Http\Testing\FileFactory;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Traits\Macroable;
11
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
12
 
13
class UploadedFile extends SymfonyUploadedFile
14
{
15
    use FileHelpers, Macroable;
16
 
17
    /**
18
     * Begin creating a new file fake.
19
     *
20
     * @return \Illuminate\Http\Testing\FileFactory
21
     */
22
    public static function fake()
23
    {
24
        return new FileFactory;
25
    }
26
 
27
    /**
28
     * Store the uploaded file on a filesystem disk.
29
     *
30
     * @param  string  $path
31
     * @param  array|string  $options
32
     * @return string|false
33
     */
34
    public function store($path, $options = [])
35
    {
36
        return $this->storeAs($path, $this->hashName(), $this->parseOptions($options));
37
    }
38
 
39
    /**
40
     * Store the uploaded file on a filesystem disk with public visibility.
41
     *
42
     * @param  string  $path
43
     * @param  array|string  $options
44
     * @return string|false
45
     */
46
    public function storePublicly($path, $options = [])
47
    {
48
        $options = $this->parseOptions($options);
49
 
50
        $options['visibility'] = 'public';
51
 
52
        return $this->storeAs($path, $this->hashName(), $options);
53
    }
54
 
55
    /**
56
     * Store the uploaded file on a filesystem disk with public visibility.
57
     *
58
     * @param  string  $path
59
     * @param  string  $name
60
     * @param  array|string  $options
61
     * @return string|false
62
     */
63
    public function storePubliclyAs($path, $name, $options = [])
64
    {
65
        $options = $this->parseOptions($options);
66
 
67
        $options['visibility'] = 'public';
68
 
69
        return $this->storeAs($path, $name, $options);
70
    }
71
 
72
    /**
73
     * Store the uploaded file on a filesystem disk.
74
     *
75
     * @param  string  $path
76
     * @param  string  $name
77
     * @param  array|string  $options
78
     * @return string|false
79
     */
80
    public function storeAs($path, $name, $options = [])
81
    {
82
        $options = $this->parseOptions($options);
83
 
84
        $disk = Arr::pull($options, 'disk');
85
 
86
        return Container::getInstance()->make(FilesystemFactory::class)->disk($disk)->putFileAs(
87
            $path, $this, $name, $options
88
        );
89
    }
90
 
91
    /**
92
     * Get the contents of the uploaded file.
93
     *
94
     * @return false|string
95
     *
96
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
97
     */
98
    public function get()
99
    {
100
        if (! $this->isValid()) {
101
            throw new FileNotFoundException("File does not exist at path {$this->getPathname()}.");
102
        }
103
 
104
        return file_get_contents($this->getPathname());
105
    }
106
 
107
    /**
108
     * Get the file's extension supplied by the client.
109
     *
110
     * @return string
111
     */
112
    public function clientExtension()
113
    {
114
        return $this->guessClientExtension();
115
    }
116
 
117
    /**
118
     * Create a new file instance from a base instance.
119
     *
120
     * @param  \Symfony\Component\HttpFoundation\File\UploadedFile  $file
121
     * @param  bool  $test
122
     * @return static
123
     */
124
    public static function createFromBase(SymfonyUploadedFile $file, $test = false)
125
    {
126
        return $file instanceof static ? $file : new static(
127
            $file->getPathname(),
128
            $file->getClientOriginalName(),
129
            $file->getClientMimeType(),
130
            $file->getError(),
131
            $test
132
        );
133
    }
134
 
135
    /**
136
     * Parse and format the given options.
137
     *
138
     * @param  array|string  $options
139
     * @return array
140
     */
141
    protected function parseOptions($options)
142
    {
143
        if (is_string($options)) {
144
            $options = ['disk' => $options];
145
        }
146
 
147
        return $options;
148
    }
149
}