Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

增加 MD5流程

  • Loading branch information...
commit 9be45e33b979ac0e9eaee6b1e8f0ceeeb47e5a9a 1 parent 823b8da
@fushang318 fushang318 authored
View
1  apps/upload-server/lib/upload_server.rb
@@ -15,6 +15,7 @@ module UploadServer
require 'upload_server/extend_method'
require 'resque'
require 'upload_server/video_util'
+require 'upload_server/copy_media_file_resque'
require 'upload_server/media_file_encode_resque'
require "upload_server/merge_slice_temp_file_resque"
View
12 apps/upload-server/lib/upload_server/copy_media_file_resque.rb
@@ -0,0 +1,12 @@
+class CopyMediaFileResque
+ @queue = :copy_media_file_resque_queue
+
+ def self.enqueue(slice_temp_file_id,media_file_id)
+ Resque.enqueue(CopyMediaFileResque, slice_temp_file_id,media_file_id)
+ end
+
+ def self.perform(slice_temp_file_id,media_file_id)
+ p ">>>>>>>>>>>>>>>>>>>>>>>> #{slice_temp_file_id}"
+ SliceTempFile.find(slice_temp_file_id).copy_media_file_and_post_status(media_file_id)
+ end
+end
View
24 apps/upload-server/lib/upload_server/server.rb
@@ -14,6 +14,10 @@ class Server < Sinatra::Base
add_cross_domain_response_header
end
+ options "/copy_media_file" do
+ add_cross_domain_response_header
+ end
+
post '/new_upload' do
add_cross_domain_response_header
file_name = params[:file_name]
@@ -52,6 +56,26 @@ class Server < Sinatra::Base
end
end
+ post "/copy_media_file" do
+ begin
+ add_cross_domain_response_header
+ file_name = params[:file_name]
+ file_size = params[:file_size]
+ creator_id = params[:creator_id]
+ media_file_id = params[:media_file_id]
+
+ slice_temp_file = SliceTempFile.get(file_name,file_size,creator_id)
+ slice_temp_file.create_copy_media_file(media_file_id)
+ status 200
+ slice_temp_file.media_file_id.to_s
+ rescue Exception=>ex
+ p ex.message
+ puts ex.backtrace*"\n"
+ status 500
+ ex.message
+ end
+ end
+
def add_cross_domain_response_header
response.headers["Access-Control-Allow-Origin"] = R::EDU_SNS_SITE
end
View
82 apps/upload-server/lib/upload_server/slice_temp_file.rb
@@ -111,6 +111,9 @@ def file_merge_complete_url
URI.parse File.join(R::EDU_SNS_SITE, "media_files/#{self.media_file_id}/file_merge_complete")
end
+ def file_copy_complete_url
+ URI.parse File.join(R::EDU_SNS_SITE, "media_files/#{self.media_file_id}/file_copy_complete")
+ end
# -------------
@@ -127,18 +130,22 @@ def save_new_blob(file_blob)
# 如果所有文件片段都已经上传完毕,就合并文件并创建 media_file
if is_complete_upload?
- # 发送 media_file_meta_info 到 sns
- res = Net::HTTP.post_form(CREATE_MEDIA_FILE_URL, media_file_info.to_hash)
- raise "#{res.code} #{res.body}" if '200' != res.code
-
- self.media_file_id = JSON.parse(res.body)['media_file']['id']
- self.save
+ self.create_meida_file
# 发送任务到合并队列
MergeSliceTempFileResque.enqueue(self.id)
end
end
+ def create_meida_file
+ # 发送 media_file_meta_info 到 sns
+ res = Net::HTTP.post_form(CREATE_MEDIA_FILE_URL, media_file_info.to_hash)
+ raise "#{res.code} #{res.body}" if '200' != res.code
+
+ self.media_file_id = JSON.parse(res.body)['media_file']['id']
+ self.save
+ end
+
# 当前 slice_temp_file 对应的 media_file_info
def media_file_info
MediaFileInfo.new(self)
@@ -161,16 +168,69 @@ def merge_and_save_and_post_status
self.save
# 发送 创建完成状态 给 sns
- res = Net::HTTP.post_form(self.file_merge_complete_url, {})
- raise "#{res.code} #{res.body}" if '200' != res.code
- # 删除文件片段
- FileUtils.rm_rf(self.blob_dir)
- self.delete
+ self.post_merge_complete_url
# 如果是视频就转码
if is_video?
MediaFileEncodeResque.enqueue(self.media_file_id)
end
end
+ def create_copy_media_file(media_file_id)
+ self.create_meida_file
+ # 发送任务到复制队列
+ CopyMediaFileResque.enqueue(self.id,media_file_id)
+ end
+
+ def copy_media_file_and_post_status(copy_media_file_id)
+ src_origin_path = SliceTempFile.media_file_path(copy_media_file_id)
+ src_flv_path = "#{src_origin_path}.flv"
+
+ # 复制MD5相同的文件到 slice_temp_file 合并的文件位置
+ FileUtils.cp(src_origin_path, self.file_path)
+ # 把复制的文件放到 media_file 应该存放的位置
+ self.entry = File.open(self.file_path, 'r')
+ self.merged = true
+ self.save
+
+ dest_origin_path = self.entry.path(:original)
+ dest_flv_path = "#{dest_origin_path}.flv"
+ # 复制 flv 文件
+ if File.exists?(src_flv_path)
+ FileUtils.cp(src_flv_path, dest_flv_path)
+ end
+
+ # 复制其他文件,如截图文件
+ src_dir = File.dirname(src_origin_path)
+ src_files = Dir[File.join(src_dir,"*")]
+ src_files.delete(src_origin_path)
+ src_files.delete(src_flv_path)
+ src_files.each do |file_path|
+ file_name = File.basename(file_path)
+ dest_origin_dir = File.dirname(dest_origin_path)
+ dest_file_path = File.join(dest_origin_dir, file_name)
+ FileUtils.cp(file_path, dest_file_path)
+ end
+
+ self.post_copy_complete_url(copy_media_file_id)
+ end
+
+ def post_copy_complete_url(copy_media_file_id)
+ res = Net::HTTP.post_form(self.file_copy_complete_url, {:copy_media_file_id=>copy_media_file_id})
+ raise "#{res.code} #{res.body}" if '200' != res.code
+ # 删除文件片段
+ FileUtils.rm_rf(self.blob_dir)
+ self.delete
+ end
+
+ def post_merge_complete_url
+ # 发送 创建完成状态 给 sns
+ md5 = `md5sum '#{self.entry.path}'`.split(" ").first
+ res = Net::HTTP.post_form(self.file_merge_complete_url, {:md5=>md5})
+ raise "#{res.code} #{res.body}" if '200' != res.code
+ # 删除文件片段
+ FileUtils.rm_rf(self.blob_dir)
+ self.delete
+ end
+
end
View
3  management/lib/path_config.rb
@@ -13,6 +13,7 @@ module PathConfig
QUEUES = [
"media_file_encode_resque_queue",
- "merge_slice_temp_file_resque_queue"
+ "merge_slice_temp_file_resque_queue",
+ "copy_media_file_resque_queue"
]
end
Please sign in to comment.
Something went wrong with that request. Please try again.