Example #1
0
 public static function Add($URL)
 {
     try {
         self::Load();
         $URL = urldecode($URL);
         if (Tools::CheckURL($URL)) {
             if (preg_match('/^https{0,1}:\\/\\/www\\.youtube\\.com\\/watch\\?v=.*$/', $URL) == 1) {
                 if (!self::$AllowProtocolYT && !\OC_User::isAdminUser(self::$CurrentUID)) {
                     return array('ERROR' => true, 'MESSAGE' => 'Notallowedtouseprotocolyt');
                 }
                 $YouTube = new YouTube(self::$YTDLBinary, $URL);
                 if (!is_null(self::$ProxyAddress) && self::$ProxyPort > 0 && self::$ProxyPort <= 65536) {
                     $YouTube->SetProxy(self::$ProxyAddress, self::$ProxyPort);
                 }
                 $VideoData = $YouTube->GetVideoData();
                 if (!isset($VideoData['VIDEO']) || !isset($VideoData['FULLNAME'])) {
                     return array('ERROR' => true, 'MESSAGE' => 'UnabletoretrievetrueYouTubevideoURL');
                 }
                 $DL = array('URL' => $VideoData['VIDEO'], 'FILENAME' => Tools::CleanString($VideoData['FULLNAME']), 'PROTO' => 'Video');
             } else {
                 if (!self::$AllowProtocolHTTP && !\OC_User::isAdminUser(self::$CurrentUID) && Tools::StartsWith(strtolower($URL), 'http')) {
                     return array('ERROR' => true, 'MESSAGE' => 'Notallowedtouseprotocolhttp');
                 } elseif (!self::$AllowProtocolFTP && !\OC_User::isAdminUser(self::$CurrentUID) && Tools::StartsWith(strtolower($URL), 'ftp')) {
                     return array('ERROR' => true, 'MESSAGE' => 'Notallowedtouseprotocolftp');
                 }
                 $DL = array('URL' => $URL, 'FILENAME' => Tools::CleanString(substr($URL, strrpos($URL, '/') + 1)), 'PROTO' => strtoupper(substr($URL, 0, strpos($URL, ':'))));
             }
             $OPTIONS = array('dir' => self::$AbsoluteDownloadsFolder, 'out' => $DL['FILENAME'], 'follow-torrent' => false);
             if (!is_null(self::$ProxyAddress) && self::$ProxyPort > 0 && self::$ProxyPort <= 65536) {
                 $OPTIONS['all-proxy'] = rtrim(self::$ProxyAddress, '/') . ':' . self::$ProxyPort;
                 if (!is_null(self::$ProxyUser) && !is_null(self::$ProxyPasswd)) {
                     $OPTIONS['all-proxy-user'] = self::$ProxyUser;
                     $OPTIONS['all-proxy-passwd'] = self::$ProxyPasswd;
                 }
             }
             $AddURI = self::$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`, `IS_CLEANED`, `STATUS`, `TIMESTAMP`) VALUES (?, ?, ?, ?, ?, ?, ?)';
                 if (self::$DbType == 1) {
                     $SQL = 'INSERT INTO *PREFIX*ocdownloader_queue ("UID", "GID", "FILENAME", "PROTOCOL", "IS_CLEANED", "STATUS", "TIMESTAMP") VALUES (?, ?, ?, ?, ?, ?, ?)';
                 }
                 $Query = \OCP\DB::prepare($SQL);
                 $Result = $Query->execute(array(self::$CurrentUID, $AddURI['result'], $DL['FILENAME'], strcmp($DL['PROTO'], 'Video') == 0 ? 'YT ' . (string) self::$L10N->t('Video') : $DL['PROTO'], 1, 1, time()));
                 return array('ERROR' => false, 'FILENAME' => $DL['FILENAME']);
             } else {
                 return array('ERROR' => true, 'MESSAGE' => 'ReturnedGIDisnullIsAria2crunningasadaemon');
             }
         } else {
             return array('ERROR' => true, 'MESSAGE' => 'InvalidURL');
         }
     } catch (Exception $E) {
         return array('ERROR' => true, 'MESSAGE' => 'Unabletolaunchthedownload');
     }
 }
Example #2
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')));
     }
 }