Exemple #1
0
 /**
  * @NoAdminRequired
  * @NoCSRFRequired
  */
 public function Add()
 {
     \OCP\JSON::setContentTypeHeader('application/json');
     if (isset($_POST['FILE']) && strlen($_POST['FILE']) > 0 && Tools::CheckURL($_POST['FILE']) && isset($_POST['OPTIONS'])) {
         try {
             if (!$this->AllowProtocolYT && !\OC_User::isAdminUser($this->CurrentUID)) {
                 throw new \Exception((string) $this->L10N->t('You are not allowed to use the YouTube protocol'));
             }
             $YouTube = new YouTube($this->YTDLBinary, $_POST['FILE']);
             if (!is_null($this->ProxyAddress) && $this->ProxyPort > 0 && $this->ProxyPort <= 65536) {
                 $YouTube->SetProxy($this->ProxyAddress, $this->ProxyPort);
             }
             if (isset($_POST['OPTIONS']['YTForceIPv4']) && strcmp($_POST['OPTIONS']['YTForceIPv4'], 'false') == 0) {
                 $YouTube->SetForceIPv4(false);
             }
             // Extract Audio YES
             if (isset($_POST['OPTIONS']['YTExtractAudio']) && strcmp($_POST['OPTIONS']['YTExtractAudio'], 'true') == 0) {
                 $VideoData = $YouTube->GetVideoData(true);
                 if (!isset($VideoData['AUDIO']) || !isset($VideoData['FULLNAME'])) {
                     return new JSONResponse(array('ERROR' => true, 'MESSAGE' => (string) $this->L10N->t('Unable to retrieve true YouTube audio URL')));
                 }
                 $DL = array('URL' => $VideoData['AUDIO'], 'FILENAME' => Tools::CleanString($VideoData['FULLNAME']), 'TYPE' => 'YT Audio');
             } else {
                 $VideoData = $YouTube->GetVideoData();
                 if (!isset($VideoData['VIDEO']) || !isset($VideoData['FULLNAME'])) {
                     return new JSONResponse(array('ERROR' => true, 'MESSAGE' => (string) $this->L10N->t('Unable to retrieve true YouTube video URL')));
                 }
                 $DL = array('URL' => $VideoData['VIDEO'], 'FILENAME' => Tools::CleanString($VideoData['FULLNAME']), 'TYPE' => 'YT Video');
             }
             // If target file exists, create a new one
             if (\OC\Files\Filesystem::file_exists($this->DownloadsFolder . '/' . $DL['FILENAME'])) {
                 $DL['FILENAME'] = time() . '_' . $DL['FILENAME'];
             }
             // Create the target file if the downloader is ARIA2
             if ($this->WhichDownloader == 0) {
                 \OC\Files\Filesystem::touch($this->DownloadsFolder . '/' . $DL['FILENAME']);
             } else {
                 if (!\OC\Files\Filesystem::is_dir($this->DownloadsFolder)) {
                     \OC\Files\Filesystem::mkdir($this->DownloadsFolder);
                 }
             }
             $OPTIONS = array('dir' => $this->AbsoluteDownloadsFolder, 'out' => $DL['FILENAME']);
             if (!is_null($this->ProxyAddress) && $this->ProxyPort > 0 && $this->ProxyPort <= 65536) {
                 $OPTIONS['all-proxy'] = rtrim($this->ProxyAddress, '/') . ':' . $this->ProxyPort;
                 if (!is_null($this->ProxyUser) && !is_null($this->ProxyPasswd)) {
                     $OPTIONS['all-proxy-user'] = $this->ProxyUser;
                     $OPTIONS['all-proxy-passwd'] = $this->ProxyPasswd;
                 }
             }
             if (!is_null($this->MaxDownloadSpeed) && $this->MaxDownloadSpeed > 0) {
                 $OPTIONS['max-download-limit'] = $this->MaxDownloadSpeed . 'K';
             }
             $AddURI = $this->WhichDownloader == 0 ? Aria2::AddUri(array($DL['URL']), array('Params' => $OPTIONS)) : CURL::AddUri($DL['URL'], $OPTIONS);
             if (isset($AddURI['result']) && !is_null($AddURI['result'])) {
                 $SQL = 'INSERT INTO `*PREFIX*ocdownloader_queue` (`UID`, `GID`, `FILENAME`, `PROTOCOL`, `STATUS`, `TIMESTAMP`) VALUES (?, ?, ?, ?, ?, ?)';
                 if ($this->DbType == 1) {
                     $SQL = 'INSERT INTO *PREFIX*ocdownloader_queue ("UID", "GID", "FILENAME", "PROTOCOL", "STATUS", "TIMESTAMP") VALUES (?, ?, ?, ?, ?, ?)';
                 }
                 $Query = \OCP\DB::prepare($SQL);
                 $Result = $Query->execute(array($this->CurrentUID, $AddURI['result'], $DL['FILENAME'], $DL['TYPE'], 1, time()));
                 sleep(1);
                 $Status = Aria2::TellStatus($AddURI['result']);
                 $Progress = 0;
                 if ($Status['result']['totalLength'] > 0) {
                     $Progress = $Status['result']['completedLength'] / $Status['result']['totalLength'];
                 }
                 $ProgressString = Tools::GetProgressString($Status['result']['completedLength'], $Status['result']['totalLength'], $Progress);
                 return new JSONResponse(array('ERROR' => false, 'MESSAGE' => (string) $this->L10N->t('Download started'), 'GID' => $AddURI['result'], 'PROGRESSVAL' => round($Progress * 100, 2) . '%', 'PROGRESS' => is_null($ProgressString) ? (string) $this->L10N->t('N/A') : $ProgressString, 'STATUS' => isset($Status['result']['status']) ? (string) $this->L10N->t(ucfirst($Status['result']['status'])) : (string) $this->L10N->t('N/A'), 'STATUSID' => Tools::GetDownloadStatusID($Status['result']['status']), 'SPEED' => isset($Status['result']['downloadSpeed']) ? Tools::FormatSizeUnits($Status['result']['downloadSpeed']) . '/s' : (string) $this->L10N->t('N/A'), 'FILENAME' => strlen($DL['FILENAME']) > 40 ? substr($DL['FILENAME'], 0, 40) . '...' : $DL['FILENAME'], 'PROTO' => $DL['TYPE'], 'ISTORRENT' => false));
             } else {
                 return new JSONResponse(array('ERROR' => true, 'MESSAGE' => (string) $this->L10N->t('Returned GID is null ! Is Aria2c running as a daemon ?')));
             }
         } catch (Exception $E) {
             return new JSONResponse(array('ERROR' => true, 'MESSAGE' => $E->getMessage()));
         }
     } else {
         return new JSONResponse(array('ERROR' => true, 'MESSAGE' => (string) $this->L10N->t('Please check the URL you\'ve just provided')));
     }
 }
 /**
  * @NoAdminRequired
  * @NoCSRFRequired
  */
 public function Add()
 {
     \OCP\JSON::setContentTypeHeader('application/json');
     if (isset($_POST['FILE']) && strlen($_POST['FILE']) > 0 && Tools::CheckURL($_POST['FILE']) && isset($_POST['OPTIONS'])) {
         try {
             $Target = Tools::CleanString(substr($_POST['FILE'], strrpos($_POST['FILE'], '/') + 1));
             // If target file exists, create a new one
             if (\OC\Files\Filesystem::file_exists($this->DownloadsFolder . '/' . $Target)) {
                 $Target = time() . '_' . $Target;
             }
             // Create the target file if the downloader is Aria2
             if ($this->WhichDownloader == 0) {
                 \OC\Files\Filesystem::touch($this->DownloadsFolder . '/' . $Target);
             } else {
                 if (!\OC\Files\Filesystem::is_dir($this->DownloadsFolder)) {
                     \OC\Files\Filesystem::mkdir($this->DownloadsFolder);
                 }
             }
             // Build OPTIONS array
             $OPTIONS = array('dir' => $this->AbsoluteDownloadsFolder, 'out' => $Target, 'follow-torrent' => false);
             if (isset($_POST['OPTIONS']['FTPUser']) && strlen(trim($_POST['OPTIONS']['FTPUser'])) > 0 && isset($_POST['OPTIONS']['FTPPasswd']) && strlen(trim($_POST['OPTIONS']['FTPPasswd'])) > 0) {
                 $OPTIONS['ftp-user'] = $_POST['OPTIONS']['FTPUser'];
                 $OPTIONS['ftp-passwd'] = $_POST['OPTIONS']['FTPPasswd'];
             }
             if (isset($_POST['OPTIONS']['FTPPasv']) && strlen(trim($_POST['OPTIONS']['FTPPasv'])) > 0) {
                 $OPTIONS['ftp-pasv'] = strcmp($_POST['OPTIONS']['FTPPasv'], "true") == 0 ? true : false;
             }
             if (!$this->ProxyOnlyWithYTDL && !is_null($this->ProxyAddress) && $this->ProxyPort > 0 && $this->ProxyPort <= 65536) {
                 $OPTIONS['all-proxy'] = rtrim($this->ProxyAddress, '/') . ':' . $this->ProxyPort;
                 if (!is_null($this->ProxyUser) && !is_null($this->ProxyPasswd)) {
                     $OPTIONS['all-proxy-user'] = $this->ProxyUser;
                     $OPTIONS['all-proxy-passwd'] = $this->ProxyPasswd;
                 }
             }
             $AddURI = $this->WhichDownloader == 0 ? Aria2::AddUri(array($_POST['FILE']), array('Params' => $OPTIONS)) : CURL::AddUri($_POST['FILE'], $OPTIONS);
             if (isset($AddURI['result']) && !is_null($AddURI['result'])) {
                 $SQL = 'INSERT INTO `*PREFIX*ocdownloader_queue` (`UID`, `GID`, `FILENAME`, `PROTOCOL`, `STATUS`, `TIMESTAMP`) VALUES (?, ?, ?, ?, ?, ?)';
                 if ($this->DbType == 1) {
                     $SQL = 'INSERT INTO *PREFIX*ocdownloader_queue ("UID", "GID", "FILENAME", "PROTOCOL", "STATUS", "TIMESTAMP") VALUES (?, ?, ?, ?, ?, ?)';
                 }
                 $Query = \OCP\DB::prepare($SQL);
                 $Result = $Query->execute(array($this->CurrentUID, $AddURI['result'], $Target, strtoupper(substr($_POST['FILE'], 0, strpos($_POST['FILE'], ':'))), 1, time()));
                 sleep(1);
                 $Status = $this->WhichDownloader == 0 ? Aria2::TellStatus($AddURI['result']) : CURL::TellStatus($AddURI['result']);
                 $Progress = 0;
                 if ($Status['result']['totalLength'] > 0) {
                     $Progress = $Status['result']['completedLength'] / $Status['result']['totalLength'];
                 }
                 $ProgressString = Tools::GetProgressString($Status['result']['completedLength'], $Status['result']['totalLength'], $Progress);
                 return new JSONResponse(array('ERROR' => false, 'MESSAGE' => (string) $this->L10N->t('Download started'), 'GID' => $AddURI['result'], 'PROGRESSVAL' => round($Progress * 100, 2) . '%', 'PROGRESS' => is_null($ProgressString) ? (string) $this->L10N->t('N/A') : $ProgressString, 'STATUS' => isset($Status['result']['status']) ? (string) $this->L10N->t(ucfirst($Status['result']['status'])) : (string) $this->L10N->t('N/A'), 'STATUSID' => Tools::GetDownloadStatusID($Status['result']['status']), 'SPEED' => isset($Status['result']['downloadSpeed']) ? Tools::FormatSizeUnits($Status['result']['downloadSpeed']) . '/s' : (string) $this->L10N->t('N/A'), 'FILENAME' => strlen($Target) > 40 ? substr($Target, 0, 40) . '...' : $Target, 'PROTO' => strtoupper(substr($_POST['FILE'], 0, strpos($_POST['FILE'], ':'))), 'ISTORRENT' => false));
             } else {
                 return new JSONResponse(array('ERROR' => true, 'MESSAGE' => (string) $this->L10N->t($this->WhichDownloader == 0 ? 'Returned GID is null ! Is Aria2c running as a daemon ?' : 'An error occurred while running the CURL download')));
             }
         } catch (Exception $E) {
             return new JSONResponse(array('ERROR' => true, 'MESSAGE' => $E->getMessage()));
         }
     } else {
         return new JSONResponse(array('ERROR' => true, 'MESSAGE' => (string) $this->L10N->t('Please check the URL you\'ve just provided')));
     }
 }
Exemple #3
0
 /**
  * @NoAdminRequired
  * @NoCSRFRequired
  */
 public function CompletelyRemoveAll()
 {
     \OCP\JSON::setContentTypeHeader('application/json');
     try {
         if (isset($_POST['GIDS']) && count($_POST['GIDS']) > 0) {
             $GIDS = array();
             foreach ($_POST['GIDS'] as $GID) {
                 $Status = $this->WhichDownloader == 0 ? Aria2::TellStatus($GID) : CURL::TellStatus($GID);
                 if (!isset($Status['error']) && strcmp($Status['result']['status'], 'removed') == 0) {
                     $Remove = $this->WhichDownloader == 0 ? Aria2::RemoveDownloadResult($GID) : CURL::RemoveDownloadResult($GID);
                 }
                 $SQL = 'DELETE FROM `*PREFIX*ocdownloader_queue` WHERE `UID` = ? AND `GID` = ?';
                 if ($this->DbType == 1) {
                     $SQL = 'DELETE FROM *PREFIX*ocdownloader_queue WHERE "UID" = ? AND "GID" = ?';
                 }
                 $Query = \OCP\DB::prepare($SQL);
                 $Result = $Query->execute(array($this->CurrentUID, $GID));
                 $GIDS[] = $GID;
             }
             return new JSONResponse(array('ERROR' => false, 'MESSAGE' => (string) $this->L10N->t('The download has been totally removed'), 'GIDS' => $GIDS));
         } else {
             return new JSONResponse(array('ERROR' => true, 'MESSAGE' => (string) $this->L10N->t('Bad GID')));
         }
     } catch (Exception $E) {
         return new JSONResponse(array('ERROR' => true, 'MESSAGE' => $E->getMessage()));
     }
 }
Exemple #4
0
 public static function GetQueue()
 {
     self::Load();
     try {
         $Params = array(self::$CurrentUID);
         $StatusReq = '(?, ?, ?, ?, ?)';
         $Params[] = 0;
         $Params[] = 1;
         $Params[] = 2;
         $Params[] = 3;
         $Params[] = 4;
         $IsCleanedReq = '(?, ?)';
         $Params[] = 0;
         $Params[] = 1;
         $SQL = 'SELECT * FROM `*PREFIX*ocdownloader_queue` WHERE `UID` = ? AND `STATUS` IN ' . $StatusReq . ' AND `IS_CLEANED` IN ' . $IsCleanedReq . ' ORDER BY `TIMESTAMP` ASC';
         if (self::$DbType == 1) {
             $SQL = 'SELECT * FROM *PREFIX*ocdownloader_queue WHERE "UID" = ? AND "STATUS" IN ' . $StatusReq . ' AND "IS_CLEANED" IN ' . $IsCleanedReq . ' ORDER BY "TIMESTAMP" ASC';
         }
         $Query = \OCP\DB::prepare($SQL);
         $Request = $Query->execute($Params);
         $Queue = [];
         while ($Row = $Request->fetchRow()) {
             $Status = self::$WhichDownloader == 0 ? Aria2::TellStatus($Row['GID']) : CURL::TellStatus($Row['GID']);
             $DLStatus = 5;
             // Error
             if (!is_null($Status)) {
                 if (!isset($Status['error'])) {
                     $Progress = 0;
                     if ($Status['result']['totalLength'] > 0) {
                         $Progress = $Status['result']['completedLength'] / $Status['result']['totalLength'];
                     }
                     $DLStatus = Tools::GetDownloadStatusID($Status['result']['status']);
                     $ProgressString = Tools::GetProgressString($Status['result']['completedLength'], $Status['result']['totalLength'], $Progress);
                     $Queue[] = array('GID' => $Row['GID'], 'PROGRESSVAL' => round($Progress * 100, 2), 'PROGRESS' => array('Message' => null, 'ProgressString' => is_null($ProgressString) ? 'N_A' : $ProgressString, 'NumSeeders' => isset($Status['result']['bittorrent']) && $Progress < 1 ? $Status['result']['numSeeders'] : null, 'UploadLength' => isset($Status['result']['bittorrent']) && $Progress == 1 ? Tools::FormatSizeUnits($Status['result']['uploadLength']) : null, 'Ratio' => isset($Status['result']['bittorrent']) ? round($Status['result']['uploadLength'] / $Status['result']['completedLength'], 2) : null), 'STATUS' => array('Value' => isset($Status['result']['status']) ? $Row['STATUS'] == 4 ? 'Removed' : ucfirst($Status['result']['status']) : 'N_A', 'Seeding' => isset($Status['result']['bittorrent']) && $Progress == 1 && $DLStatus != 3 ? true : false), 'STATUSID' => $Row['STATUS'] == 4 ? 4 : $DLStatus, 'SPEED' => isset($Status['result']['downloadSpeed']) ? $Progress == 1 ? isset($Status['result']['bittorrent']) ? $Status['result']['uploadSpeed'] == 0 ? '--' : Tools::FormatSizeUnits($Status['result']['uploadSpeed']) . '/s' : '--' : ($DLStatus == 4 ? '--' : Tools::FormatSizeUnits($Status['result']['downloadSpeed']) . '/s') : 'N_A', 'FILENAME' => $Row['FILENAME'], 'PROTO' => $Row['PROTOCOL'], 'ISTORRENT' => isset($Status['result']['bittorrent']));
                     if ($Row['STATUS'] != $DLStatus) {
                         $SQL = 'UPDATE `*PREFIX*ocdownloader_queue` SET `STATUS` = ? WHERE `UID` = ? AND `GID` = ? AND `STATUS` != ?';
                         if (self::$DbType == 1) {
                             $SQL = 'UPDATE *PREFIX*ocdownloader_queue SET "STATUS" = ? WHERE "UID" = ? AND "GID" = ? AND "STATUS" != ?';
                         }
                         $Query = \OCP\DB::prepare($SQL);
                         $Result = $Query->execute(array($DLStatus, self::$CurrentUID, $Row['GID'], 4));
                     }
                 } else {
                     $Queue[] = array('GID' => $Row['GID'], 'PROGRESSVAL' => 0, 'PROGRESS' => array('Message' => 'ErrorGIDnotfound', 'ProgressString' => null, 'NumSeeders' => null, 'UploadLength' => null, 'Ratio' => null), 'STATUS' => array('Value' => 'N_A', 'Seeding' => null), 'STATUSID' => $DLStatus, 'SPEED' => 'N_A', 'FILENAME' => $Row['FILENAME'], 'PROTO' => $Row['PROTOCOL'], 'ISTORRENT' => isset($Status['result']['bittorrent']));
                 }
             } else {
                 $Queue[] = array('GID' => $Row['GID'], 'PROGRESSVAL' => 0, 'PROGRESS' => array('Message' => self::$WhichDownloader == 0 ? 'ReturnedstatusisnullIsAria2crunningasadaemon' : 'Unabletofinddownloadstatusfile', 'ProgressString' => null, 'NumSeeders' => null, 'UploadLength' => null, 'Ratio' => null), 'STATUS' => array('Value' => 'N_A', 'Seeding' => null), 'STATUSID' => $DLStatus, 'SPEED' => 'N_A', 'FILENAME' => $Row['FILENAME'], 'PROTO' => $Row['PROTOCOL'], 'ISTORRENT' => isset($Status['result']['bittorrent']));
             }
         }
         return array('ERROR' => false, 'MESSAGE' => null, 'QUEUE' => $Queue, 'COUNTER' => Tools::GetCounters(self::$DbType, self::$CurrentUID));
     } catch (Exception $E) {
         return array('ERROR' => true, 'MESSAGE' => $E->getMessage(), 'QUEUE' => null, 'COUNTER' => null);
     }
 }