From 7634d075dca27e8bc8457338c64c71ce16025acc Mon Sep 17 00:00:00 2001 From: anke1460 Date: Tue, 10 Mar 2020 21:05:34 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=AF=E5=88=A0=E9=99=A4=E8=A7=86=E9=A2=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/courses_controller.rb | 1 - app/controllers/users/videos_controller.rb | 3 --- app/models/video.rb | 24 +++++++++++++++++++ .../videos/dispatch_callback_service.rb | 2 ++ ...0200310080919_add_delete_state_to_video.rb | 6 +++++ 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20200310080919_add_delete_state_to_video.rb diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index d35741c61..d93f0a5bd 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -132,7 +132,6 @@ class CoursesController < ApplicationController tip_exception(404, "找不到资源") if video.blank? tip_exception(403, "...") unless (video.user_id == current_user.id || current_user.admin_or_business?) video.destroy! - AliyunVod::Service.delete_video([video.uuid]) rescue nil end render_ok diff --git a/app/controllers/users/videos_controller.rb b/app/controllers/users/videos_controller.rb index 32f81f6ed..d4f021389 100644 --- a/app/controllers/users/videos_controller.rb +++ b/app/controllers/users/videos_controller.rb @@ -26,7 +26,6 @@ class Users::VideosController < Users::BaseController return render_error('该状态下不能删除视频') unless video.pending? video.destroy! - AliyunVod::Service.delete_video([video.uuid]) rescue nil render_ok end @@ -61,8 +60,6 @@ class Users::VideosController < Users::BaseController video.destroy! - AliyunVod::Service.delete_video([video.uuid]) rescue nil - render_ok end diff --git a/app/models/video.rb b/app/models/video.rb index 02cccd8f1..486c64a9e 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -1,6 +1,13 @@ class Video < ApplicationRecord include AASM + alias_method :hard_destroy, :destroy + default_scope -> { where(delete_state: nil) } + scope :deleted, -> { unscope(where: :delete_state).where.not(delete_state: nil) } + + BEGIN_DELETE = 1 # 标记软删除 + FINISH_DELETE = 2 # 视频资源完成删除 + # 标准视频转码组 NORMAL_TRANSCODE_GROUP_ID = 'a0277c5c0c7458458e171b0cee6ebf5e' @@ -41,4 +48,21 @@ class Video < ApplicationRecord def video_play_duration (play_duration / (60*60.0)).ceil end + + def destroy + run_callbacks(:destroy) do + if persisted? + update_column(:delete_state, Video::BEGIN_DELETE) + + # result = AliyunVod::Service.delete_video([self.uuid]) + # if result && result['RequestId'] + # update_column(:delete_state, Video::FINISH_DELETE) + # end + end + + @destroyed = true + end + freeze + end + end \ No newline at end of file diff --git a/app/services/videos/dispatch_callback_service.rb b/app/services/videos/dispatch_callback_service.rb index 149009c96..6bfb9f70d 100644 --- a/app/services/videos/dispatch_callback_service.rb +++ b/app/services/videos/dispatch_callback_service.rb @@ -23,6 +23,8 @@ class Videos::DispatchCallbackService < ApplicationService when 'StreamTranscodeComplete' then # 转码完成 return if video.play_url.present? video.update!(play_url: params['FileUrl'], transcoded: true) + when 'DeleteMediaComplete' #完成云端视频删除 + video.update_column(:delete_state, Video::FINISH_DELETE) end rescue => ex diff --git a/db/migrate/20200310080919_add_delete_state_to_video.rb b/db/migrate/20200310080919_add_delete_state_to_video.rb new file mode 100644 index 000000000..a6854620a --- /dev/null +++ b/db/migrate/20200310080919_add_delete_state_to_video.rb @@ -0,0 +1,6 @@ +class AddDeleteStateToVideo < ActiveRecord::Migration[5.2] + def change + add_column :videos, :delete_state, :integer + add_index :videos, :delete_state + end +end