source: trunk/wtorrent/home/cls/Files.cls.php @ 4

Revision 4, 4.5 KB checked in by royger, 3 years ago (diff)
Line 
1<?php
2/*
3This file is part of wTorrent.
4
5wTorrent is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 3 of the License, or
8(at your option) any later version.
9
10wTorrent is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program.  If not, see <http://www.gnu.org/licenses/>.
17*/
18class Files extends rtorrent
19{
20        private  $files;
21        private  $hash;
22        private  $info_files = array('', '',
23                                                                'f.get_path=', 
24                                                        'f.get_completed_chunks=', 
25                                                        'f.get_size_chunks=',
26                                                        'f.get_priority=');
27    private $position = 0;
28
29    public function construct()
30        {
31                $this->hash = $this->_request['hash'];
32               
33                if(!$this->setClient())
34                        return false;
35                       
36                if(isset($this->_request['ch_pr']) && count($this->_request['files']) > 0) $this->changePriorities($this->_request['files'], $this->_request['priority'], $this->hash);
37                $this->getTorrents($this->_request['hash']);
38        }
39       
40       
41        public function getHash()
42        {
43                return $this->hash;
44        }
45        public function getFiles()
46        {
47                return $this->files;
48        }
49        public function getName()
50        {
51                return $this->details['name'];
52        }
53        public function getTorrent()
54        {
55                return $this->details['torrent_file'];
56        }
57        public function getDataPath()
58        {
59                return $this->details['data_path'];
60        }
61        public function getPercent()
62        {
63                return $this->details['percent'];
64        }
65        public function getRatio()
66        {
67                return $this->details['ratio'];
68        }
69        public function getSize($key)
70        {
71                return $this->getCorrectUnits($this->files[$key]['size']);
72        }
73        public function getDone($key)
74        {
75                return $this->getCorrectUnits($this->files[$key]['size_done']);
76        }
77        public function getUp()
78        {
79                return $this->getCorrectUnits($this->details['bytes_up']);
80        }
81        public function getPriorityStr($key)
82        {
83                switch($this->files[$key]['priority'])
84                {
85                        case 0:
86                                return $this->_str['file_off'];
87                                break;
88                        case 1:
89                                return $this->_str['file_normal'];
90                                break;
91                        case 2:
92                                return $this->_str['file_high'];
93                                break;
94                }
95        }
96        public function getPriorities()
97        {
98                return array('0' => $this->_str['file_off'], '1' => $this->_str['file_normal'], '2' => $this->_str['file_high']);
99        }
100        private function getCorrectUnits($size)
101    {
102                $size_units = 'bytes';
103                if($size >= 1024)
104                {
105                $size /= 1024;
106                $size_units = 'Kb';
107                }
108                if($size >= 1024)
109                {
110            $size /= 1024;
111            $size_units = 'Mb';
112        }
113                if($size >= 1024)
114        {
115            $size /= 1024;
116            $size_units = 'Gb';
117        }
118        return round($size, 2) .  $size_units;
119
120    }
121
122        private function getTorrents($hash)
123    {
124       
125        $this->info_files[0] = $hash;
126       
127                foreach($this->info_files as $param)
128                        $array_post[] = new xmlrpcval($param, 'string');
129               
130                // Get chunk size
131                $message = new xmlrpcmsg("d.get_chunk_size", array(new xmlrpcval($hash, 'string')));
132                $result = $this->client->send($message);
133                $chunk_size = $result->val;
134                // Get file info
135        $message = new xmlrpcmsg("f.multicall", $array_post);
136                $result = $this->client->send($message);
137        //print_r($result);
138        foreach($result->val as $key => $file)
139        {
140                if(SCRAMBLE === true)
141                        $file[0] = $this->scramble($file[0]);
142                       
143                $this->files[$key]['name'] = $file[0];
144                $this->files[$key]['size_in_chunks'] = $file[2];
145                $this->files[$key]['completed_chunks'] = $file[1];
146                $this->files[$key]['priority'] = $file[3];
147                $this->files[$key]['percent'] = floor(($this->files[$key]['completed_chunks']/$this->files[$key]['size_in_chunks'])*100);
148                $this->files[$key]['size'] = $this->files[$key]['size_in_chunks'] * $chunk_size;
149                $this->files[$key]['size_done'] = $this->files[$key]['completed_chunks'] * $chunk_size;
150        }
151        //print_r($this->files);
152    }
153    private function changePriorities($files, $priorities, $hash)
154    {
155        $f_index = array_keys($files);
156        //print_r($f_index);
157       
158        foreach($f_index as $param)
159                        $array_post[] = new xmlrpcmsg('f.set_priority', array(new xmlrpcval($hash, 'string'), new xmlrpcval($param, 'int'), new xmlrpcval($priorities, 'int')));
160               
161                //print_r($array_post);
162                $responses = $this->client->multicall($array_post);
163               
164                $mesage = new xmlrpcmsg('d.update_priorities', array(new xmlrpcval($hash, 'string')));
165                $this->client->send($mesage);
166    }
167}
168?>
Note: See TracBrowser for help on using the repository browser.