gdoo/app/Gdoo/System/Controllers/MediaController.php

152 lines
4.5 KiB
PHP

<?php namespace Gdoo\System\Controllers;
use Session;
use Request;
use Validator;
use DB;
use Gdoo\System\Models\Media;
use Gdoo\Index\Controllers\DefaultController;
use Illuminate\Support\Str;
class MediaController extends DefaultController
{
public $permission = ['dialog', 'create', 'delete', 'download', 'folder'];
public function create()
{
$folderId = Request::get('folder_id');
$file = Request::file('Filedata');
$rules = [
'file' => 'mimes:'.$this->setting['upload_type'],
];
$v = Validator::make(['file' => $file], $rules);
if ($file->isValid() && $v->passes()) {
// 获取上传uri第一个目录
$path = 'media/'.date('Y/m');
$upload_path = upload_path().'/'.$path;
// 文件后缀名
$extension = $file->getClientOriginalExtension();
// 文件新名字
$filename = date('dhis_').Str::random(4).'.'.$extension;
$filename = mb_strtolower($filename);
$fileTypes = ['image/png', 'image/jpg', 'image/jpeg'];
$mimeType = $file->getMimeType();
$filesize = $file->getSize();
if ($file->move($upload_path, $filename)) {
$data = [
'name' => mb_strtolower($file->getClientOriginalName()),
'path' => $path.'/'.$filename,
'type' => $extension,
'folder_id' => $folderId,
'size' => $filesize,
];
if (in_array($mimeType, $fileTypes)) {
$path = pathinfo($path.'/'.$filename);
$thumb = $path['dirname'].'/'.thumb($upload_path.'/'.$filename, 750);
$data['thumb'] = $thumb;
}
$id = Media::insertGetId($data);
if ($id) {
$data['id'] = $id;
return $this->json($data, true);
} else {
return $this->json('文件上传失败');
}
}
}
return $this->json('文件上传失败');
}
public function dialog()
{
if (Request::method() == 'POST') {
$folder = Request::get('folder');
$folderId = Request::get('folder_id');
$userId = auth()->id();
$model = Media::where('created_id', $userId);
if ($folder == 1) {
$model->whereRaw('folder = 1');
} else {
$model->whereRaw('isnull(folder, 0) = 0');
if ($folderId) {
$model->where('folder_id', $folderId);
}
}
$rows = $model->orderBy('id', 'desc')->get()->toArray();
foreach($rows as &$row) {
if (in_array($row['type'], ['png', 'jpg', 'jpeg'])) {
$file = $row['path'];
$path = pathinfo($file);
$thumb = $path['dirname'].'/t750_'.$path['basename'];
if (!is_file(upload_path().'/'.$thumb)) {
thumb(upload_path().'/'.$file, 750);
}
$row['path'] = $file;
$row['thumb'] = $thumb;
}
}
return $this->json($rows, true);
}
return $this->render();
}
/**
* 新建文件夹
*/
public function folder()
{
if (Request::method() == 'POST') {
$gets = Request::all();
$gets['folder'] = 1;
Media::insertGetId($gets);
return $this->json('操作成功', true);
}
return $this->render();
}
/**
* 下载文件
*/
public function download()
{
$id = Request::get('id');
if ($id) {
$row = Media::where('id', $id)->first();
$path = upload_path().'/'.$row['path'];
return response()->download($path, $row['name']);
}
}
/**
* 删除文件
*/
public function delete()
{
$id = (array)Request::get('id');
if (empty($id)) {
return $this->json('删除失败');
}
$rows = Media::whereIn('id', $id)->get();
foreach ($rows as $row) {
$file = upload_path().'/'.$row['path'];
if (is_file($file)) {
unlink($file);
}
}
Media::whereIn('id', $id)->delete();
return $this->json('删除成功', true);
}
}