Jelajahi Sumber

Implementing DDL. Not tested yet!
Need to provide a sample conf for nginx and test it :)

Antoine Leroyer 11 tahun lalu
induk
melakukan
f0d0ed2c85
5 mengubah file dengan 51 tambahan dan 3 penghapusan
  1. 1 0
      lib/rSeed.rb
  2. 12 0
      lib/rSeed/rtorrent.rb
  3. 17 1
      lib/rSeed/server.rb
  4. 15 0
      public/js/custom_dl.js
  5. 6 2
      views/download.haml

+ 1 - 0
lib/rSeed.rb

@@ -10,6 +10,7 @@ require "haml"
 require "fileutils"
 require "net/http"
 require "sinatra"
+require "sinatra/xsendfile"
 require "xmlrpc/client"
 require "sys/filesystem"
 require "yaml"

+ 12 - 0
lib/rSeed/rtorrent.rb

@@ -35,6 +35,18 @@ module RSeed
       end
       return hash
     end
+ 
+    def getName(hash)
+      begin
+        response = @client.call("d.name", "#{hash}")
+      rescue XMLRPC::FaultException => e
+        puts "Error:"
+        puts e.faultCode
+        puts e.faultString
+      end
+      return response
+    end
+ 
 
     def getFilesInfo(torrentHash)
       begin

+ 17 - 1
lib/rSeed/server.rb

@@ -8,6 +8,7 @@ module RSeed
     set :root, File.join(File.dirname(__FILE__), '..', '..')
     set :haml, :format => :html5
     set :server, 'thin'
+    set :xsf_header, 'X-Accel-Redirect'
 
     before do
       cache_control :public, :must_revalidate, :max_age => 60
@@ -32,6 +33,7 @@ module RSeed
     # -------
     helpers do
       include Rack::Utils
+      include Sinatra::Xsendfile
       
       def host
         "http://#{request.host}#{request.port == 80 ? '' : ':' + request.port.to_s}"
@@ -90,12 +92,26 @@ module RSeed
     # ---
     get %r{/d/([a-zA-Z0-9]*)} do |hash|
       if publicLink(hash)
-        haml :download, :locals => { :tfiles => @rtorrent.getFilesInfo(hash) }  
+        haml :download, :locals => { 
+          :tfiles => @rtorrent.getFilesInfo(hash), 
+          :tname => @rtorrent.getName(hash),
+          :thash => hash
+        }  
       else
         404
       end
     end
 
+    post %r{/d/([a-zA-Z0-9]*)} do |hash|
+      if publicLink(hash)
+        @rtorrent.getFilesInfo(hash).each do |file| 
+          if file.has_value?(params[:filename]) 
+            x_send_file(file["path"])
+          end
+        end
+      end
+    end
+
     # Public link generator
     # ---------------------
     post '/share' do

+ 15 - 0
public/js/custom_dl.js

@@ -0,0 +1,15 @@
+$(document).ready(function() {
+  $.ajaxSetup({ cache: false });
+  $("a.dl-file").click(function() {
+    var filename = $(this).text();
+    var hash = $("h3[data-torrent-id]").attr("data-torrent-id");
+    $.ajax({
+      type: "POST",
+      url: hash,
+      data: { filename : filename }
+    })
+    .done(function( msg ) {
+      $.notify( msg );
+    });
+  });
+});

+ 6 - 2
views/download.haml

@@ -13,13 +13,17 @@
     %section.main-section{style: "margin-top : 50px;"}
       .row
         .large-12.columns
-          %h3 Title goes here
+          %h3{data: {torrent_id: thash}} #{tname}
           %ul.square
             - tfiles.each do |f|
-              %li= "#{f["name"]} <span class=\"right\">#{f["size_bytes"].to_i/1024/1024} Mo</span>" 
+              %li
+                %a.dl-file #{f["name"]}
+                %span.right #{f["size_bytes"].to_i/1024/1024} Mo
     %a.exit-off-canvas
 
 %script{:src => '/js/vendor/jquery.js'}
 %script{:src => '/js/foundation.min.js'}
+%script{:src => '/js/notify-combined.min.js'}
+%script{:src => '/js/custom_dl.js'}
 %script
   $(document).foundation();