Skip to content

Commit

Permalink
Add hostedvideo, prevent double downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
nasser committed May 31, 2015
1 parent f259d13 commit 7ec19e9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/stopwork/types/cloudapp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.match? slide
# url is adjusted to accomodate local images
# TODO figure this out
def url
cached "#{slide}/.png"
cached "#{slide}/.png", slide
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions lib/stopwork/types/hostedvideo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'viddl-rb'

module Stopwork
module Types
# Video slide
#
# Slides consist of a single embeded video. Currently YouTube and Vimeo only.
# Syntax is any valid youtube or vimeo url
class HostedVideo < Video
# match any video url
def self.match? slide
!! ViddlRb::PluginBase.registered_plugins.find { |p| p.matches_provider? slide }
end

def url
cached ViddlRb.get_urls(slide).first, slide
end
end
end
end
2 changes: 1 addition & 1 deletion lib/stopwork/types/imgur.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.match? slide
# url is adjusted to accomodate local images
# TODO figure this out
def url
cached "#{slide}.png"
cached "#{slide}.png", slide
end
end
end
Expand Down
28 changes: 18 additions & 10 deletions lib/stopwork/types/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,28 @@ def initialize slide, slideshow
@slideshow = slideshow
end

def cached url
url_hash = Digest::SHA1.hexdigest url
def cached url, key=nil
key = url if key.nil?
key_hash = Digest::SHA1.hexdigest key

if File.exists? @slideshow.cache_folder_path + "/" + url_hash
cached_finished = @slideshow.cache_folder_path + "/" + key_hash
cached_incomplete = cached_finished + "-incomplete"

if File.exists? cached_finished
# file exits in cache, use it
@slideshow.cache_folder_name + "/" + url_hash
@slideshow.cache_folder_name + "/" + key_hash

elsif File.exists? cached_incomplete
# file is being downloaded, dont start new download, use url for now
url
else
# file not in cache, download it
# file not in cache, not downloading, download it, use url for now
fork do
exec "echo \"#{url} -> #{url_hash}\";
curl -#L #{url} -o #{@slideshow.cache_folder_path}/#{url_hash}-incomplete;
mv #{@slideshow.cache_folder_path}/#{url_hash}-incomplete #{@slideshow.cache_folder_path}/#{url_hash}"
exec "echo \"#{key} -> #{key_hash}\";
curl -#L \"#{url}\" -o \"#{cached_incomplete}\";
mv \"#{cached_incomplete}\" \"#{cached_finished}\""
end

# return url for now
url
end
end
Expand All @@ -54,10 +61,11 @@ def template
require_relative "imgur"
require_relative "web"
require_relative "video"
require_relative "hostedvideo"

module Stopwork
module Types
self.match_order = [Imgur, CloudApp, Image, Video, Web, Text]
self.match_order = [Imgur, CloudApp, Image, HostedVideo, Video, Web, Text]

def self.render slide, slideshow
self.match_order.select { |f| f.match? slide }.first.new(slide, slideshow).render # TODO optimize
Expand Down

0 comments on commit 7ec19e9

Please sign in to comment.