Skip to content

Commit

Permalink
* lib/parkplace/torrent.rb: adding more data to the tracker (bytes t…
Browse files Browse the repository at this point in the history
…ransferred, file sizes).

 * lib/parkplace/models.rb: switch to using migrations, clean up.
 * lib/parkplace.rb: ditto.
  • Loading branch information
_why committed Aug 19, 2006
1 parent a69599f commit 273437e
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 150 deletions.
4 changes: 3 additions & 1 deletion lib/parkplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ module ParkPlace

class << self
def create
v = 0.0
v = 1.0 if Models::Bucket.table_exists?
Camping::Models::Session.create_schema
ParkPlace::Models.create_schema
Models.create_schema :assume => v
end
def options
require 'ostruct'
Expand Down
296 changes: 148 additions & 148 deletions lib/parkplace/models.rb
Original file line number Diff line number Diff line change
@@ -1,169 +1,169 @@
module ParkPlace
module Models
module ParkPlace::Models

class FileInfo
attr_accessor :path, :mime_type, :disposition, :size, :md5
end
class FileInfo
attr_accessor :path, :mime_type, :disposition, :size, :md5
end

class User < Base
has_many :bits, :foreign_key => 'owner_id'
validates_length_of :login, :within => 3..40
validates_uniqueness_of :login
validates_uniqueness_of :key
validates_presence_of :password
validates_confirmation_of :password
def before_save
@password_clean = self.password
self.password = hmac_sha1(self.password, self.secret)
end
def after_save
self.password = @password_clean
end
class User < Base
has_many :bits, :foreign_key => 'owner_id'
validates_length_of :login, :within => 3..40
validates_uniqueness_of :login
validates_uniqueness_of :key
validates_presence_of :password
validates_confirmation_of :password
def before_save
@password_clean = self.password
self.password = hmac_sha1(self.password, self.secret)
end
def after_save
self.password = @password_clean
end
end

class Bit < Base
acts_as_nested_set
serialize :meta
serialize :obj
belongs_to :owner, :class_name => 'User', :foreign_key => 'owner_id'
has_and_belongs_to_many :users
has_one :torrent
validates_length_of :name, :within => 3..255
class Bit < Base
acts_as_nested_set
serialize :meta
serialize :obj
belongs_to :owner, :class_name => 'User', :foreign_key => 'owner_id'
has_and_belongs_to_many :users
has_one :torrent
validates_length_of :name, :within => 3..255

def fullpath; File.join(STORAGE_PATH, name) end
def grant hsh
if hsh[:access]
self.access = hsh[:access]
self.save
end
end
def access_readable
name, _ = CANNED_ACLS.find { |k, v| v == self.access }
if name
name
else
[0100, 0010, 0001].map do |i|
[[4, 'r'], [2, 'w'], [1, 'x']].map do |k, v|
(self.access & (i * k) == 0 ? '-' : v )
end
end.join
end
end
def check_access user, group_perm, user_perm
!!( if owned_by?(user) or (user and access & group_perm > 0) or (access & user_perm > 0)
true
elsif user
acl = users.find(user.id) rescue nil
acl and acl.access.to_i & user_perm
end )
end
def owned_by? user
user and owner_id == user.id
def fullpath; File.join(STORAGE_PATH, name) end
def grant hsh
if hsh[:access]
self.access = hsh[:access]
self.save
end
def readable_by? user
check_access(user, READABLE_BY_AUTH, READABLE)
end
def writable_by? user
check_access(user, WRITABLE_BY_AUTH, WRITABLE)
end
def access_readable
name, _ = CANNED_ACLS.find { |k, v| v == self.access }
if name
name
else
[0100, 0010, 0001].map do |i|
[[4, 'r'], [2, 'w'], [1, 'x']].map do |k, v|
(self.access & (i * k) == 0 ? '-' : v )
end
end.join
end
end
def check_access user, group_perm, user_perm
!!( if owned_by?(user) or (user and access & group_perm > 0) or (access & user_perm > 0)
true
elsif user
acl = users.find(user.id) rescue nil
acl and acl.access.to_i & user_perm
end )
end
def owned_by? user
user and owner_id == user.id
end
def readable_by? user
check_access(user, READABLE_BY_AUTH, READABLE)
end
def writable_by? user
check_access(user, WRITABLE_BY_AUTH, WRITABLE)
end
end

class Bucket < Bit
validates_format_of :name, :with => /^[-\w]+$/
def self.find_root(bucket_name)
find(:first, :conditions => ['parent_id IS NULL AND name = ?', bucket_name]) or raise NoSuchBucket
end
def find_slot(oid)
Slot.find(:first, :conditions => ['parent_id = ? AND name = ?', self.id, oid]) or raise NoSuchKey
end
class Bucket < Bit
validates_format_of :name, :with => /^[-\w]+$/
def self.find_root(bucket_name)
find(:first, :conditions => ['parent_id IS NULL AND name = ?', bucket_name]) or raise NoSuchBucket
end
def find_slot(oid)
Slot.find(:first, :conditions => ['parent_id = ? AND name = ?', self.id, oid]) or raise NoSuchKey
end
end

class Slot < Bit
def fullpath; File.join(STORAGE_PATH, obj.path) end
def etag
if self.obj.respond_to? :md5
self.obj.md5
else
%{"#{MD5.md5(self.obj)}"}
end
class Slot < Bit
def fullpath; File.join(STORAGE_PATH, obj.path) end
def etag
if self.obj.respond_to? :md5
self.obj.md5
else
%{"#{MD5.md5(self.obj)}"}
end
end
end

class Torrent < Base
belongs_to :bit
has_many :torrent_peers
end
class Torrent < Base
belongs_to :bit
has_many :torrent_peers
end

class TorrentPeer < Base
belongs_to :torrent
end
class TorrentPeer < Base
belongs_to :torrent
end

def self.create_schema
unless Torrent.table_exists?
ActiveRecord::Schema.define do
create_table :parkplace_torrents do |t|
t.column :id, :integer, :null => false
t.column :bit_id, :integer
t.column :info_hash, :string, :limit => 40
t.column :metainfo, :binary
t.column :seeders, :integer, :null => false, :default => 0
t.column :leechers, :integer, :null => false, :default => 0
t.column :hits, :integer, :null => false, :default => 0
t.column :total, :integer, :null => false, :default => 0
t.column :updated_at, :timestamp
end
create_table :parkplace_torrent_peers do |t|
t.column :id, :integer, :null => false
t.column :torrent_id, :integer
t.column :guid, :string, :limit => 40
t.column :ipaddr, :string
t.column :port, :integer
t.column :uploaded, :integer, :null => false, :default => 0
t.column :downloaded, :integer, :null => false, :default => 0
t.column :remaining, :integer, :null => false, :default => 0
t.column :compact, :integer, :null => false, :default => 0
t.column :event, :integer, :null => false, :default => 0
t.column :key, :string, :limit => 55
t.column :created_at, :timestamp
t.column :updated_at, :timestamp
end
end
class SetupParkPlace < V 1.0
def self.up
create_table :parkplace_bits do |t|
t.column :id, :integer, :null => false
t.column :owner_id, :integer
t.column :parent_id, :integer
t.column :lft, :integer
t.column :rgt, :integer
t.column :type, :string, :limit => 6
t.column :name, :string, :limit => 255
t.column :created_at, :timestamp
t.column :updated_at, :timestamp
t.column :access, :integer
t.column :meta, :text
t.column :obj, :text
end
unless Bucket.table_exists?
ActiveRecord::Schema.define do
create_table :parkplace_bits do |t|
t.column :id, :integer, :null => false
t.column :owner_id, :integer
t.column :parent_id, :integer
t.column :lft, :integer
t.column :rgt, :integer
t.column :type, :string, :limit => 6
t.column :name, :string, :limit => 255
t.column :created_at, :timestamp
t.column :updated_at, :timestamp
t.column :access, :integer
t.column :meta, :text
t.column :obj, :text
end
create_table :parkplace_users do |t|
t.column :id, :integer, :null => false
t.column :login, :string, :limit => 40
t.column :password, :string, :limit => 40
t.column :email, :string, :limit => 64
t.column :key, :string, :limit => 64
t.column :secret, :string, :limit => 64
t.column :created_at, :datetime
t.column :activated_at, :datetime
t.column :superuser, :integer, :default => 0
t.column :deleted, :integer, :default => 0
end
create_table :parkplace_bits_users do |t|
t.column :bit_id, :integer
t.column :user_id, :integer
t.column :access, :integer
end
end
create_table :parkplace_users do |t|
t.column :id, :integer, :null => false
t.column :login, :string, :limit => 40
t.column :password, :string, :limit => 40
t.column :email, :string, :limit => 64
t.column :key, :string, :limit => 64
t.column :secret, :string, :limit => 64
t.column :created_at, :datetime
t.column :activated_at, :datetime
t.column :superuser, :integer, :default => 0
t.column :deleted, :integer, :default => 0
end
create_table :parkplace_bits_users do |t|
t.column :bit_id, :integer
t.column :user_id, :integer
t.column :access, :integer
end
create_table :parkplace_torrents do |t|
t.column :id, :integer, :null => false
t.column :bit_id, :integer
t.column :info_hash, :string, :limit => 40
t.column :metainfo, :binary
t.column :seeders, :integer, :null => false, :default => 0
t.column :leechers, :integer, :null => false, :default => 0
t.column :hits, :integer, :null => false, :default => 0
t.column :total, :integer, :null => false, :default => 0
t.column :updated_at, :timestamp
end
create_table :parkplace_torrent_peers do |t|
t.column :id, :integer, :null => false
t.column :torrent_id, :integer
t.column :guid, :string, :limit => 40
t.column :ipaddr, :string
t.column :port, :integer
t.column :uploaded, :integer, :null => false, :default => 0
t.column :downloaded, :integer, :null => false, :default => 0
t.column :remaining, :integer, :null => false, :default => 0
t.column :compact, :integer, :null => false, :default => 0
t.column :event, :integer, :null => false, :default => 0
t.column :key, :string, :limit => 55
t.column :created_at, :timestamp
t.column :updated_at, :timestamp
end
end
def self.down
drop_table :parkplace_bits
drop_table :parkplace_users
drop_table :parkplace_bits_users
drop_table :parkplace_torrents
drop_table :parkplace_torrent_peers
end
end

end
9 changes: 8 additions & 1 deletion lib/parkplace/torrent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def get
class CTrackerIndex < R '/tracker'
def get
@torrents = torrent_list @input.info_hash
@transfer = TorrentPeer.sum :downloaded, :group => :torrent
render :torrent_index
end
end
Expand All @@ -202,18 +203,24 @@ def torrent_index
thead do
tr do
th "Name"
th "Size"
th "Seeders"
th "Leechers"
th "Downloads"
th "Transferred"
th "Since"
end
end
tbody do
torrents.each do |t|
tr do
td t.bit.name
th t.bit.name
td number_to_human_size(t.bit.obj.size)
td t.seeders
td t.leechers
td t.total
td number_to_human_size(@transfer[t])
# td t.metainfo.creation_date
end
end
end
Expand Down

0 comments on commit 273437e

Please sign in to comment.