<?php
namespace App\Http\Controllers\Service\v1;
use App\Http\Controllers\Utils\Code;
use Illuminate\Support\Facades\Storage;
class FileService extends BaseService
{
private static $instance;
public static function getInstance()
{
if (!self::$instance instanceof self) {
self::$instance = new static();
}
return self::$instance;
}
public function getFileLists($form, array $permission = [])
{
$this->return['lists'] = getFileLists(getFilePath($form['path'], $form['basename']), $permission);
return $this->return;
}
public function readFile($form)
{
if (!file_exists($form['path'])) {
$this->return['code'] = Code::ERROR;
$this->return['message'] = 'File does not exist';
}
if (filesize($form['path']) > 1024 * 1024 * 3) {
$this->return['code'] = Code::ERROR;
$this->return['message'] = 'File size exceeds limit';
}
$result = getFileContent($form['path']);
!empty($result['code']) ? $this->return = $result : $this->return['lists'] = $result;
return $this->return;
}
public function updateFile($form)
{
if (!file_exists($form['path'])) {
$this->return['code'] = Code::ERROR;
$this->return['message'] = 'File does not exist';
}
$result = writeFile($form['path'], $form['content']);
!empty($result['code']) ? $this->return = $result : $this->return['lists'] = $form;
return $this->return;
}
public function gZipFile($form)
{
if (!file_exists($form['path'])) {
$this->return['code'] = Code::ERROR;
$this->return['message'] = 'File does not exist';
}
$result = gZipFile($form['docLists'], $form['path'], $form['resource'].'_'.date('YmdHis').'.zip');
!empty($result['code']) ? $this->return = $result : $this->return['lists'] = $form;
return $this->return;
}
public function unGZipFile($form)
{
if (!file_exists($form['path'])) {
$this->return['code'] = Code::ERROR;
$this->return['message'] = 'File does not exist';
}
$result = unGZipFile($form['path'], $form['resource'], false);
!empty($result['code']) ? $this->return = $result : $this->return['lists'] = $form;
return $this->return;
}
public function removeFile($form)
{
if (!file_exists($form['path'])) {
$this->return['code'] = Code::ERROR;
$this->return['message'] = 'File does not exist';
}
chmod($form['path'], 0777);
$result = removeFiles($form['path']);
!empty($result['code']) ? $this->return = $result : $this->return['lists'] = $form;
return $this->return;
}
public function createFile($form)
{
if (!file_exists($form['path'])) {
$this->return['code'] = Code::ERROR;
$this->return['message'] = 'File does not exist';
}
$result = createFile($form['path']);
!empty($result['code']) ? $this->return = $result : $this->return['lists'] = $form;
return $this->return;
}
public function setFileAuth($form)
{
if (!file_exists($form['path'])) {
$this->return['code'] = Code::ERROR;
$this->return['message'] = 'File does not exist';
}
chmod($form['path'], octdec((int)'0'.$form['auth']));
$result = chmodFile($form['path']) == $form['auth'];
!empty($result['code']) ? $this->return = $result : $this->return['lists'] = $form;
return $this->return;
}
public function renameFile($form)
{
$result = renameFile($form['oldFile'], $form['newFile']);
!empty($result['code']) ? $this->return = $result : $this->return['lists'] = $form;
return $this->return;
}
public function uploadFile($request, $form)
{
$file = $request->file('file');
if (!$file->isValid()) {
$this->return['code'] = Code::ERROR;
$this->return['message'] = 'upload file failed';
return $this->return;
}
$ext = $file->getClientOriginalExtension();
$path = $file->getRealPath();
$imgExt = ['jpg', 'jpeg', 'png', 'gif'];
if (in_array(strtolower($ext), $imgExt)) {
if ($file->getSize() > 2 * 1024 * 1024) {
$this->return['code'] = Code::ERROR;
$this->return['message'] = 'upload image size error';
return $this->return;
}
}
if (strtolower($ext) == 'mp4') {
if ($file->getSize() > 5 * 1024 * 1024) {
$this->return['code'] = Code::ERROR;
$this->return['message'] = 'upload video size error';
return $this->return;
}
}
$imgExt[] = 'mp4';
if (!in_array(strtolower($ext), $imgExt)) {
$this->return['code'] = Code::ERROR;
$this->return['message'] = 'Unsupported file format';
return $this->return;
}
if (in_array(strtolower($ext), $imgExt) && !empty($form['round_name'])) {
$filename = date('Ymd') . DIRECTORY_SEPARATOR . time() . '.' . $ext;
Storage::disk('public')->put($filename, file_get_contents($path));
$this->return['code'] = Code::SUCCESS;
$this->return['message'] = 'upload file successfully';
$this->return['lists'] = array('src'=>config('app.url').'storage/'.$filename);
return $this->return;
}
$imgExt[] = 'php';
if (in_array(strtolower($ext), $imgExt) && empty($form['round_name'])) {
$filename = $file->getClientOriginalName();
$file->move($form['path'], $filename);
$this->return['code'] = Code::SUCCESS;
$this->return['message'] = 'upload file successfully';
$this->return['lists'] = array('src'=>config('app.url').'storage/'.$filename, 'name' => $filename);
return $this->return;
}
return $this->return;
}
}