【调度系统】广东民航医疗快线调度系统源代码
wanglizhong
2025-06-24 a51d070d364b0da8e5f8ea9203a6e50c8b4c0af3
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php declare(strict_types=1);
 
namespace WeChatPay\Util;
 
use function basename;
use function sprintf;
use function json_encode;
 
use UnexpectedValueException;
 
use GuzzleHttp\Psr7\Utils;
use GuzzleHttp\Psr7\BufferStream;
use GuzzleHttp\Psr7\LazyOpenStream;
use GuzzleHttp\Psr7\MultipartStream;
use GuzzleHttp\Psr7\FnStream;
use GuzzleHttp\Psr7\CachingStream;
use Psr\Http\Message\StreamInterface;
 
/**
 * Util for Media(image, video or text/csv whose are the platform acceptable file types etc) uploading.
 */
class MediaUtil
{
    /**
     * @var string - local file path
     */
    private $filepath;
 
    /**
     * @var ?StreamInterface - The `file` stream
     */
    private $fileStream;
 
    /**
     * @var StreamInterface - The `meta` stream
     */
    private $metaStream;
 
    /**
     * @var MultipartStream - The `multipart/form-data` stream
     */
    private $multipart;
 
    /**
     * @var StreamInterface - multipart stream wrapper
     */
    private $stream;
 
    /**
     * Constructor
     *
     * @param string $filepath The media file path or file name,
     *                         should be one of the
     *                         images(jpg|bmp|png)
     *                         or
     *                         video(avi|wmv|mpeg|mp4|mov|mkv|flv|f4v|m4v|rmvb)
     *                         or
     *                         text/csv whose are the platform acceptable etc.
     * @param ?StreamInterface $fileStream  File content stream, optional
     */
    public function __construct(string $filepath, ?StreamInterface $fileStream = null)
    {
        $this->filepath = $filepath;
        $this->fileStream = $fileStream;
        $this->composeStream();
    }
 
    /**
     * Compose the GuzzleHttp\Psr7\FnStream
     */
    private function composeStream(): void
    {
        $basename = basename($this->filepath);
        $stream = $this->fileStream ?? new LazyOpenStream($this->filepath, 'rb');
        if ($stream instanceof StreamInterface && !($stream->isSeekable())) {
            $stream = new CachingStream($stream);
        }
        if (!($stream instanceof StreamInterface)) {
            throw new UnexpectedValueException(sprintf('Cannot open or caching the file: `%s`', $this->filepath));
        }
 
        $buffer = new BufferStream();
        $metaStream = FnStream::decorate($buffer, [
            'getSize' => static function () { return null; },
            // The `BufferStream` doen't have `uri` metadata(`null` returned),
            // but the `MultipartStream` did checked this prop with the `substr` method, which method described
            // the first paramter must be the string on the `strict_types` mode.
            // Decorate the `getMetadata` for this case.
            'getMetadata' => static function($key = null) use ($buffer) {
                if ('uri' === $key) { return 'php://temp'; }
                return $buffer->getMetadata($key);
            },
        ]);
 
        $this->fileStream = $this->fileStream ?? $stream;
        $this->metaStream = $metaStream;
 
        $this->setMeta();
 
        $multipart = new MultipartStream([
            [
                'name'     => 'meta',
                'contents' => $this->metaStream,
                'headers'  => [
                    'Content-Type' => 'application/json',
                ],
            ],
            [
                'name'     => 'file',
                'filename' => $basename,
                'contents' => $this->fileStream,
            ],
        ]);
        $this->multipart = $multipart;
 
        $this->stream = FnStream::decorate($multipart, [
            '__toString' => function () { return $this->getMeta(); },
            'getSize' => static function () { return null; },
        ]);
    }
 
    /**
     * Set the `meta` part of the `multipart/form-data` stream
     *
     * Note: The `meta` weren't be the `media file`'s `meta data` anymore.
     *
     *       Previous whose were designed as `{filename,sha256}`,
     *       but another API was described asof `{bank_type,filename,sha256}`.
     *
     *       Exposed the ability of setting the `meta` for the `new` data structure.
     *
     * @param ?string $json - The `meta` string
     * @since v1.3.2
     */
    public function setMeta(?string $json = null): int
    {
        $content = $json ?? (string)json_encode([
            'filename' => basename($this->filepath),
            'sha256' => $this->fileStream ? Utils::hash($this->fileStream, 'sha256') : '',
        ]);
        // clean the metaStream's buffer string
        $this->metaStream->getContents();
 
        return $this->metaStream->write($content);
    }
 
    /**
     * Get the `meta` string
     */
    public function getMeta(): string
    {
        $json = (string)$this->metaStream;
        $this->setMeta($json);
 
        return $json;
    }
 
    /**
     * Get the `FnStream` which is the `MultipartStream` decorator
     */
    public function getStream(): StreamInterface
    {
        return $this->stream;
    }
 
    /**
     * Get the `Content-Type` value from the `{$this->multipart}` instance
     */
    public function getContentType(): string
    {
        return 'multipart/form-data; boundary=' . $this->multipart->getBoundary();
    }
}