<?php

namespace app\common\service;

use think\File;
use think\facade\Request;
use think\facade\Validate;
use think\facade\Filesystem;
use app\common\service\Service;
use Workerman\Worker;

class FileService extends Service
{
    /**
     * 上传图片
     * 本地运行如果无法写入图片可能是没有给public目录写权限
     * 
     * @param string $path 上传路径,默认为'default'
     * @return array 上传后的图片路径
     */
    public function uploadImage($path = 'default')
    {
        // 获取表单上传文件
        $files = request()->file();
        // 验证上传文件格式和大小
        $this->validate($files, Validate::rule(['file' => 'fileSize:2048000|fileExt:jpeg,jpg,png,webp']));
        $file = request()->file('file');
        try {
            // 保存上传文件到public目录下的$path目录中
            $path = Filesystem::disk('public')->putFile($path, $file);
        } catch (\Exception $e) {
            throw $this->warpException($e, '上传图片失败:\n');
        }

        // 获取图片的url地址
        $path = Request::domain() . getVirRootDir() . '/storage/' . $path;

        return [
            'path' => $path
        ];
    }

    /**
     * 上传附件
     * 本地运行如果无法写入图片可能是没有给public目录写权限
     * 
     * @param string $path 上传路径,默认为'default'
     * @return array 上传后的图片路径
     */
    public function uploadAttachment($path = 'default')
    {
        // 获取表单上传文件
        if (count(Worker::getAllWorkers()) > 0) {
            $file = $_FILES[0]['file_data'];
            //$tmp_file  = tempnam('','tm_');  这种写法最终保存时扩展名为.tmp
            $tmp_file = sys_get_temp_dir() . '/' . uniqid() . '.' . explode('/', $_FILES[0]['file_type'])[1];
            file_put_contents($tmp_file, $file);
            $file = new File($tmp_file);
            // 验证上传文件格式和大小
            $files = ['file' => $file];
            if (
                !in_array($file->getMime(), [
                    'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // docx
                    'application/pdf', 
                    'application/msword' //doc
                ])
            ) {
                throw $this->exception('文件mime不符合要求,(请使用pdf,doc,docx)', 777);
            }
        } else {
            // 获取表单上传文件
            $files = request()->file();
            $file = request()->file('file');
            $this->validate($files, Validate::rule(['file' => 'fileExt:pdf,doc,docx']));
        }
        try {
            // 保存上传文件到public目录下的$path目录中
            $path = Filesystem::disk('public')->putFile($path, $file);
        } catch (\Exception $e) {
            throw $this->warpException($e, '上传图片失败:\n');
        }

        // 获取图片的url地址
        $path = Request::domain() . getVirRootDir() . '/storage/' . $path;

        return [
            'path' => $path
        ];
    }



    /**
     * 获取当前网站的域名地址
     * 
     * @return string 域名地址
     */
    protected static function get_domain()
    {
        $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
        return $sys_protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
    }

}