gq vor 7 Jahren
Ursprung
Commit
8b71122bc3

+ 13 - 33
server/app/Http/Controllers/Admin/Dream/InfoController.php

xqd xqd xqd xqd
@@ -128,7 +128,7 @@ class InfoController extends Controller
             if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
                 return back()->with('error','File type allow MP4!');
             }
-            if (!$file = VideoUpload::mvFile('file')) return back()->with('error','上传失败');
+            if (!$file = VideoUpload::mvFile('video')) return back()->with('error','上传失败');
             $data["video"] = $file;
         }
         $data['created_at'] = date('Y-m-d H:i:s');
@@ -231,8 +231,13 @@ class InfoController extends Controller
             $data['score'] = (log($care_num,$x) + ($a/$t) + $b)*100000000000000 ;
         }
         $pics = (array) request('pic');
-      /*  if (request("file")) {
-            $file = request("file");
+        if (!empty(request("video"))) {
+            $md5 = $this->getarea($dream->video);
+            if (!empty($md5)) {
+                $attache = new \App\Services\Base\Attachment();
+                $attache->deleteAttachment($md5);
+            }
+            $file = request("video");
             $fileSize = $file->getSize();
             $size =  200 * 1024 * 1024;
             if ($fileSize > $size) {
@@ -245,17 +250,9 @@ class InfoController extends Controller
             if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
                 return back()->with('error','File type allow MP4!');
             }
-            if (!$file = VideoUpload::mvFile('file')) return back()->with('error','上传失败');
+            if (!$file = VideoUpload::mvFile('video')) return back()->with('error','上传失败');
             $data["video"] = $file;
-        }else{
-//            更新是视频不存在则删除
-            $dream = $this->repository->find(request('id'));
-            if (is_file('.'.str_replace(getenv('APP_URL'),'',$dream->video))) {
-                unlink('.'.str_replace(getenv('APP_URL'),'',$dream->video));
-            }
-            $dream->video = '';
-            $dream->save();
-        }*/
+        }
         if (!empty($pics)) {
 //            图片不为空
             $old_data_pics =  $this->repository->find(request('id'))->imgs->toArray();
@@ -268,34 +265,17 @@ class InfoController extends Controller
                         $attache->deleteAttachment($md5);
                     }
                 }
-
-
-
-            $old_pics = array_column($old_data_pics,'pic');
-            $a = array_diff($pics['url'],$old_pics);
-            $b = array_diff($old_pics,$pics['url']);
-            if (!empty($a) || !empty($b)) {       //有图片变化执行
-                //        删除以前图片 重新插入
-                if (!empty($b)) {
-                    foreach ($b as $old_pic){
-                        if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
-                            unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
-                        }
-                        BaseAttachmentModel::where('url',$old_pic)->delete();
-                        DreamImages::where('pic',$old_pic)->delete();
-                    }
-                }
+            DreamImages::where('dream_id',request('id'))->delete();
                 $arr = [];                         //插入新的图片
-                foreach ($a as $pic) {
+                foreach ($pics['url'] as $pic) {
                     $arr[] = [
-                        'pic'=>getenv('APP_URL').$pic,
+                        'pic'=>$pic,
                         'dream_id'=>request('id'),
                         'created_at'=>date('Y-m-d H:i:s'),
                         'updated_at'=>date('Y-m-d H:i:s'),
                     ];
                 }
                 DreamImages::insert($arr);
-            }
         }
         $ok = $this->repository->update(request('id'),$data);
         if ($data['status'] == 1) {   //审核通过

+ 30 - 40
server/app/Http/Controllers/Admin/Interaction/InfoController.php

xqd xqd xqd xqd xqd
@@ -59,12 +59,12 @@ class InfoController extends Controller
         $pics = (array) request('pic');
         if($pics){
             foreach ($pics['url'] as $key => $pic) {
-                $data['pic'.($key+1)] = getenv('APP_URL').$pic;
+                $data['pic'.($key+1)] = $pic;
             }
         }
 
-        if (request("file")) {
-            $file = request("file");
+        if (request("video")) {
+            $file = request("video");
             $fileSize = $file->getSize();
             $size =  200 * 1024 * 1024;
             if ($fileSize > $size) {
@@ -77,7 +77,7 @@ class InfoController extends Controller
             if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
                 return back()->with('error','File type allow MP4!');
             }
-            if (!$file = VideoUpload::mvFile('file')) return back()->with('error','上传失败');
+            if (!$file = VideoUpload::mvFile('video')) return back()->with('error','上传失败');
             $data["video"] = $file;
         }
         $id = InteractionInfo::create($data);
@@ -119,43 +119,32 @@ class InfoController extends Controller
         $data = (array) request('data');
         $interaction_id = request('id');
         $interaction = InteractionInfo::find($interaction_id)->toArray();
+        $interaction['title'] = $data['title'];
         $old_pics = [];
         for ($i = 1; $i <=9; $i++) {
             if (!empty($interaction['pic'.($i)])) {
-                $old_pics[] = $interaction['pic'.($i)];
+                $old_pics[$i] = $interaction['pic'.($i)];
             }
         }
+
         $new_pics = (array) request('pic');
-        $a = array_diff($new_pics['url'],$old_pics);
-        $b = array_diff($old_pics,$new_pics['url']);
-        if (!empty($a) || !empty($b)) {       //有图片变化执行
-            //        删除以前图片 重新插入
-            if (!empty($b)) {
-                foreach ($b as $old_pic){
-                    $key = array_search($old_pic,$interaction);
-                    $interaction[$key] = '';
-                    if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
-                        unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
-                    }
-                    BaseAttachmentModel::where('url',$old_pic)->delete();
-                    InteractionInfo::find(request('id'))->update($interaction);
-                }
+        if (!empty($new_pics)) {
+//            图片不为空
+            //操作成功,删除原来的图片
+            foreach ($new_pics['url'] as $key => $pic) {
+                $interaction['pic'.($key+1)] = $pic;
             }
-
-            $a = array_values($a);
-            $c = -1;
-            for ($i = 1; $i <=9; $i++) {
-                if (empty($interaction['pic'.($i)])) {
-                    $c++;
-                    if (array_key_exists($c,$a)) {
-                        $interaction['pic'.($i)] = getenv('APP_URL').$a[$c];
-                    }
-
+            foreach ($old_pics as $k => $pic) {
+                if (!in_array($pic, $new_pics['url'])) {
+                    $md5 = $this->getarea($pic);
+                    $attache = new \App\Services\Base\Attachment();
+                    $attache->deleteAttachment($md5);
+                    $interaction['pic'.($k+1)] = '';
                 }
             }
         }
-        if (!empty(request("file"))) {
-            $file = request("file");
+        if (!empty(request("video"))) {
+            $file = request("video");
             $fileSize = $file->getSize();
             $size =  200 * 1024 * 1024;
             if ($fileSize > $size) {
@@ -168,16 +157,8 @@ class InfoController extends Controller
             if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
                 return back()->with('error','File type allow MP4!');
             }
-            if (!$file = VideoUpload::mvFile('file')) return back()->with('error','上传失败');
+            if (!$file = VideoUpload::mvFile('video')) return back()->with('error','上传失败');
             $interaction["video"] = $file;
-        }else{
-//            更新是视频不存在则删除
-            $info = InteractionInfo::find(request('id'));
-            if (is_file('.'.str_replace(getenv('APP_URL'),'',$info->video))) {
-                unlink('.'.str_replace(getenv('APP_URL'),'',$info->video));
-            }
-            $info->video = null;
-            $info->save();
         }
         $ok = InteractionInfo::find(request('id'))->update($interaction);
         if($ok) {
@@ -270,4 +251,13 @@ class InfoController extends Controller
         }
      return view('admin.comment.edit',compact('interaction_id'));
     }
+    //    获取视频图片后缀码
+    public function getarea($str)
+    {
+        $start = strripos($str, '/');
+        $first = substr($str, $start + 1);
+        $end = strripos($first, '.');
+        $last = substr($first, 0, $end);
+        return $last;
+    }
 }

+ 36 - 22
server/app/Widget/Tools/VideoUpload.php

xqd xqd xqd
@@ -2,8 +2,11 @@
 
 namespace App\Widget\Tools;
 
+use App\Models\BaseAttachmentModel;
 use Illuminate\Support\Facades\Request;
 use Intervention\Image\Facades\Image;
+use Symfony\Component\HttpFoundation\File\Exception\FileException;
+
 //use FFMpeg;
 /**
  * Created by PhpStorm.
@@ -18,9 +21,7 @@ class VideoUpload
     {
 
         if (!is_readable($path)) {
-
             mkdir($path, 0700, true);
-
         }
 
     }
@@ -31,32 +32,45 @@ class VideoUpload
      *
      */
 
-    public static function mvFile($fileName, $sizex = 150, $sizey = 200,$isImage = false, $dir = 'dream/video')//200 270
+    public static function mvFile($fileName)
     {
-
+        $rel_path =  '/upload/video/' . date('Ymd');
+        $path = public_path() . $rel_path;
         if (!Request::hasFile($fileName)) return false;
 
         $file = Request::file($fileName);
 
-        VideoUpload::mkFolder(public_path('upload/'));
-
-        if(Request::get('article_id'))$dir=Request::get('article_id');
-        $filePath = public_path('upload/'.$dir.'/');
-        VideoUpload::mkFolder($filePath);
-
-        $fileName = time() . '.' . $file->getClientOriginalExtension();
-
-//        $fileName = $clientName . '.' . $file->getClientOriginalExtension();
-
-        \Log::info($filePath. $fileName);
-        if($isImage){
-            if (!Image::make($file)->resize($sizex, $sizey)->save($filePath. $fileName)) return false;
-
-        }else{
-            $file->move($filePath, $fileName);
+        $fileSize = $file->getSize();
+
+
+        $clientName = $file->getClientOriginalName();
+        $md5 = md5($clientName . time());
+        $md5_filename = $md5 . '.' . $file->getClientOriginalExtension();
+        $fileMimeType = $file->getMimeType();
+        try {
+            if(!$file->move($path, $md5_filename)){
+                return  false;
+            }
+
+            $real_path = $path . '/' . $md5_filename;
+            $url_path = $rel_path . '/' . $md5_filename;
+
+            $attachment = new BaseAttachmentModel();
+            $attachment->name = $clientName;
+            $attachment->md5 = $md5;
+            $attachment->path = $real_path;
+            $attachment->url = $url_path;
+            $attachment->size = $fileSize;
+            $attachment->file_type = $fileMimeType;
+            if ($attachment->save()) {
+                return env('APP_URL').'/attachment/'.$md5;
+            } else {
+                @unlink($real_path);
+                return  false;
+            }
+        } catch (FileException $e) {
+            return  false;
         }
-
-        return '/upload/' .$dir.'/'. $fileName;
     }
 
 }

+ 1 - 1
server/resources/views/admin/dream/info/edit.blade.php

xqd
@@ -86,7 +86,7 @@
                             @endif>
                         <label class="control-label col-sm-3" >&nbsp;</label>
                         <div class="col-sm-9" style="width: 50%;height: 50%">
-                            <video style="width: 300px" src="{{ $data['video'] or ''}}" controls >
+                            <video style="width: 300px;height:200px;" src="{{ $data['video'] or ''}}" controls >
                             </video>
                         </div>
                     </div>

+ 2 - 2
server/resources/views/admin/interaction/info/edit.blade.php

xqd xqd
@@ -57,7 +57,7 @@
                                 <div  id="is_video" style="display: none">
                                     <div class="form-group">
                                         <label class="control-label col-sm-3" >视频(mp4格式,200MB内)</label>
-                                        <input type="file" name="file" >
+                                        <input type="file" name="video" >
                                     </div>
                                     <div class="form-group" @if(empty($data))
                                     style="display: none"
@@ -95,7 +95,7 @@
                                     <div class="col-sm-9">
                                         <input type="hidden" name="_referer" value="<?php echo urlencode(request()->server('HTTP_REFERER'));?>"/>
                                         <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"/>
-                                        <input type="hidden" name="data[dream_id]" value="@if(isset($dream_id)) {{$dream_id or ''}} @elseif(isset($data['dream_id']))) {{ $data['video'] or ''}} @endif">
+                                        <input type="hidden" name="data[dream_id]" value="@if(isset($dream_id)) {{$dream_id or ''}}  @endif">
                                         <input type="submit" class="btn btn-success" style="margin-right:20px;">
                                         <input type="reset" class="btn btn-default" >
                                     </div>