save() public method

Save torrent file to disk
public save ( $filename = null ) : boolean
return boolean file has been saved or not
Beispiel #1
0
 /**
  * Upload un torrent
  * 
  * @access public
  * @return View torrent.upload
  *
  */
 public function upload()
 {
     $user = Auth::user();
     // Post et fichier upload
     if (Request::isMethod('post')) {
         // No torrent file uploaded OR an Error has occurred
         if (Input::hasFile('torrent') == false) {
             Session::put('message', 'You must provide a torrent for the upload');
             return View::make('torrent.upload', array('categories' => Category::all(), 'user' => $user));
         } else {
             if (Input::file('torrent')->getError() != 0 && Input::file('torrent')->getClientOriginalExtension() != 'torrent') {
                 Session::put('message', 'An error has occurred');
                 return View::make('torrent.upload', array('categories' => Category::all(), 'user' => $user));
             }
         }
         // Deplace et decode le torrent temporairement
         TorrentTools::moveAndDecode(Input::file('torrent'));
         // Array from decoded from torrent
         $decodedTorrent = TorrentTools::$decodedTorrent;
         // Tmp filename
         $fileName = TorrentTools::$fileName;
         // Info sur le torrent
         $info = Bencode::bdecode_getinfo(getcwd() . '/files/torrents/' . $fileName, true);
         // Si l'announce est invalide ou si le tracker et privée
         if ($decodedTorrent['announce'] != route('announce', ['passkey' => $user->passkey]) && Config::get('other.freeleech') == true) {
             Session::put('message', 'Your announce URL is invalid');
             return View::make('torrent.upload', array('categories' => Category::all(), 'user' => $user));
         }
         // Find the right category
         $category = Category::find(Input::get('category_id'));
         // Create the torrent (DB)
         $torrent = new Torrent(['name' => Input::get('name'), 'slug' => Str::slug(Input::get('name')), 'description' => Input::get('description'), 'info_hash' => $info['info_hash'], 'file_name' => $fileName, 'num_file' => $info['info']['filecount'], 'announce' => $decodedTorrent['announce'], 'size' => $info['info']['size'], 'nfo' => Input::hasFile('nfo') ? TorrentTools::getNfo(Input::file('nfo')) : '', 'category_id' => $category->id, 'user_id' => $user->id]);
         // Validation
         $v = Validator::make($torrent->toArray(), $torrent->rules);
         if ($v->fails()) {
             if (file_exists(getcwd() . '/files/torrents/' . $fileName)) {
                 unlink(getcwd() . '/files/torrents/' . $fileName);
             }
             Session::put('message', 'An error has occured may bee this file is already online ?');
         } else {
             // Savegarde le torrent
             $torrent->save();
             // Compte et sauvegarde le nombre de torrent dans  cette catégorie
             $category->num_torrent = Torrent::where('category_id', '=', $category->id)->count();
             $category->save();
             // Sauvegarde les fichiers que contient le torrent
             $fileList = TorrentTools::getTorrentFiles($decodedTorrent);
             foreach ($fileList as $file) {
                 $f = new TorrentFile();
                 $f->name = $file['name'];
                 $f->size = $file['size'];
                 $f->torrent_id = $torrent->id;
                 $f->save();
                 unset($f);
             }
             return Redirect::route('torrent', ['slug' => $torrent->slug, 'id' => $torrent->id])->with('message', trans('torrent.your_torrent_is_now_seeding'));
         }
     }
     return View::make('torrent.upload', array('categories' => Category::all(), 'user' => $user));
 }
Beispiel #2
0
 public function postUpload()
 {
     $data = Input::all();
     $rules = array('category' => 'required');
     $validator = Validator::make($data, $rules);
     if ($validator->passes()) {
         if (Input::hasFile('file')) {
             $contents = file_get_contents(Input::file('file')->getRealPath());
             $decoded = Bencode::decode($contents);
             $torrent = new Torrent();
             $torrent->name = $decoded['info']['name'];
             $torrent->size = 0;
             $torrent->downloads = 0;
             $torrent->file = $contents;
             $torrent->category_id = $data['category'];
             $torrent->uploader_id = Auth::user()->id;
             $torrent->save();
             $files = array();
             if (!empty($decoded['info']['files'])) {
                 foreach ($decoded['info']['files'] as $file) {
                     $files[] = array('torrent_id' => $torrent->id, 'name' => implode('/', $file['path']), 'size' => $file['length']);
                     $torrent->size += $file['length'];
                 }
             } else {
                 $files[] = array('torrent_id' => $torrent->id, 'name' => $decoded['info']['name'], 'size' => $decoded['info']['length']);
                 $torrent->size += $decoded['info']['length'];
             }
             //http://stackoverflow.com/questions/12702812/bulk-insertion-in-laravel-using-eloquent-orm
             TorrentFile::insert($files);
             $torrent->save();
             return Redirect::action('TorrentController@getDetails', array($torrent->id))->with('success', 'Your torrent has been added!');
         }
     }
     return Redirect::to('/upload')->withInput()->withErrors($validator);
 }
Beispiel #3
0
function checktorrent($filename)
{
    $torrent = new Torrent($filename);
    echo 'check torrent: ' . $filename . "\n";
    $file_media = $torrent->file_media();
    $count = count($file_media);
    for ($i = 0; $i + 0 < $count + 0; $i++) {
        if ($file_media[$i] + 0 < 0) {
            echo $file_media[$i] . "\n";
            $file_media[$i] = 0;
        }
    }
    $torrent->file_media($file_media);
    $torrent->save($filename);
    echo 'check torrent: ' . $filename . " over \n";
}
         $torrent->announce_list($announce_list);
     }
 } else {
     $torrent = new Torrent($path_edit, array(), $piece_size, $callback_log, $callback_err);
 }
 if (isset($request['comment'])) {
     $comment = trim($request['comment']);
     if (strlen($comment)) {
         $torrent->comment($comment);
     }
 }
 if ($request['private']) {
     $torrent->is_private(true);
 }
 $fname = rTask::formatPath($taskNo) . '/result.torrent';
 $torrent->save($fname);
 if ($request['start_seeding']) {
     $fname = getUniqueUploadedFilename($torrent->info['name'] . '.torrent');
     $path_edit = trim($request['path_edit']);
     if (is_dir($path_edit)) {
         $path_edit = addslash($path_edit);
     }
     if (rTorrentSettings::get()->correctDirectory($path_edit)) {
         $path_edit = dirname($path_edit);
         if ($resumed = rTorrent::fastResume($torrent, $path_edit)) {
             $torrent = $resumed;
         }
         $torrent->save($fname);
         rTorrent::sendTorrent($torrent, true, true, $path_edit, null, true, isLocalMode());
         @chmod($fname, $profileMask & 0666);
     }
<?php

require_once 'Torrent.class.php';
// create torrent
$torrent = new Torrent('./torrents', 'http://torrent.tracker/annonce');
if (!($error = $torrent->error())) {
    $torrent->save('test.torrent');
    // save to disk
} else {
    echo '<br>DEBUG: ', $error;
    // error method return the last error message
}
// print torrent info
$torrent = new Torrent('./test.torrent');
echo '<pre>private: ', $torrent->is_private() ? 'yes' : 'no', '<br>annonce: ';
var_dump($torrent->announce());
echo '<br>name: ', $torrent->name(), '<br>comment: ', $torrent->comment(), '<br>piece_length: ', $torrent->piece_length(), '<br>size: ', $torrent->size(2), '<br>hash info: ', $torrent->hash_info(), '<br>stats: ';
var_dump($torrent->scrape());
echo '<br>content: ';
var_dump($torrent->content());
echo '<br>source: ', $torrent;
// modify torrent
$torrent->announce('http://alternate-torrent.tracker/annonce');
// add a tracker
$torrent->announce(false);
// reset announce trackers
$torrent->announce(array('http://torrent.tracker/annonce', 'http://alternate-torrent.tracker/annonce'));
// set tracker(s), it also works with a 'one tracker' array...
$torrent->announce(array(array('http://torrent.tracker/annonce', 'http://alternate-torrent.tracker/annonce'), 'http://another-torrent.tracker/annonce'));
// set tiered trackers
$torrent->comment('hello world');
Beispiel #6
0
 /**
  * Try to save when no announce has been given. The code we are testing is AFTER the code that
  * checks if the file specified is writeable, so make sure the argument to save() is a file that
  * is writeable.
  *
  * @expectedException RuntimeException
  * @expectedExceptionMessage Announce URL is missing
  * @covers PHP\BitTorrent\Torrent::save
  */
 public function testSaveWithNoAnnounce()
 {
     $target = tempnam(sys_get_temp_dir(), 'PHP\\BitTorrent');
     $this->torrent->save($target);
 }
 /** @function TorrentGen() Inserts the new torrent or updates if it already exists.
  * @return $Torrent returns the torrent.
  */
 private function TorrentGen()
 {
     $Torrent = current($this->getClass('TorrentManager')->find(array('hash' => bin2hex($_REQUEST['info_hash']))));
     if (!$Torrent || !$Torrent->isValid()) {
         $Torrent = new Torrent(array('hash' => bin2hex($_REQUEST['info_hash'])));
     } else {
         $Torrent->set('hash', bin2hex($_REQUEST['info_hash']));
     }
     $Torrent->save();
     return $Torrent;
 }