| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace App\Module\File;
- use UCore\Exception\LogicException;
- use Illuminate\Support\Facades\Storage;
- class Temporary
- {
- /**
- *
- * @return \Illuminate\Contracts\Filesystem\Filesystem
- */
- private static function getDisk()
- {
- $storage = \App\Module\File\Storage::getStorage();
- $tempStorage = env('FILESYSTEM_TEMP_DISK', $storage);
- return Storage::disk($tempStorage);
- }
- static public function getDir()
- {
- return config('app.name') . '_temp/' . date('Ym/d/');
- }
- /**
- * 获取储存地址
- *
- * @param $ext
- * @return string
- */
- static private function getStorePath($ext)
- {
- $path = self::getDir() . uniqid() . '.' . $ext;
- return $path;
- }
- static public function save($ext, $fileString)
- {
- $path = self::getStorePath($ext);
- $res = self::getDisk()->put($path, $fileString);
- if (!$res) {
- throw new LogicException("临时储存失败");
- }
- return $path;
- }
- /**
- * 读取并下载
- *
- * @param $path
- * @return \Symfony\Component\HttpFoundation\StreamedResponse
- */
- static public function getDownUrl($path)
- {
- $path = config('app.name') . '_temp/' . $path;
- // dd($path);
- $res = self::getDisk()->url($path);
- return $res;
- }
- static public function getDown($path)
- {
- $path = config('app.name') . '_temp/' . $path;
- // dd($path);
- $disk = self::getDisk();
- // dd($oss);
- $res = $disk->read($path);
- $headers = [];
- $headers['Content-Type'] = $disk->mimeType($path);
- $headers['Content-Length'] = $disk->size($path);
- // dd($headers);
- $resp = response('', 200, $headers)->setContent($res);
- // dd($res);
- return $resp;
- }
- static public function path2url($path)
- {
- $new = substr($path, strpos($path, 'temp'));
- return $new;
- }
- /**
- * 获取本地驱动的临时文件一个
- *
- * @return string
- */
- static public function getTempLocalFile()
- {
- $file = sys_get_temp_dir() . '/' . uniqid();
- // dd($file);
- return $file;
- }
- }
|