-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encoder now supports download from FTP URLs
- Loading branch information
Daniel Neto
committed
Jan 29, 2025
1 parent
7e9eb3e
commit 8e32981
Showing
5 changed files
with
177 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
class FTPDownloader | ||
{ | ||
private $ftpUrl; | ||
private $ftpHost; | ||
private $ftpUser; | ||
private $ftpPass; | ||
private $ftpPort; | ||
private $ftpConn; | ||
private $remotePath = '/'; | ||
|
||
public function __construct($ftpUrl) | ||
{ | ||
global $global; | ||
$this->ftpUrl = addLastSlash($ftpUrl); | ||
$this->parseFtpUrl($ftpUrl); | ||
} | ||
|
||
private function parseFtpUrl($ftpUrl) | ||
{ | ||
$parsedUrl = parse_url($ftpUrl); | ||
if (!$parsedUrl || !isset($parsedUrl['scheme']) || $parsedUrl['scheme'] !== 'ftp') { | ||
throw new Exception("Invalid FTP URL"); | ||
} | ||
|
||
$this->ftpHost = $parsedUrl['host'] ?? ''; | ||
$this->ftpUser = $parsedUrl['user'] ?? 'anonymous'; | ||
$this->ftpPass = $parsedUrl['pass'] ?? ''; | ||
$this->ftpPort = $parsedUrl['port'] ?? 21; | ||
$this->remotePath = $parsedUrl['path'] ?? '/'; | ||
} | ||
|
||
public function connect() | ||
{ | ||
$this->ftpConn = ftp_connect($this->ftpHost, $this->ftpPort); | ||
if (!$this->ftpConn) { | ||
throw new Exception("Could not connect to FTP server"); | ||
} | ||
|
||
if (!ftp_login($this->ftpConn, $this->ftpUser, $this->ftpPass)) { | ||
throw new Exception("Could not log in to FTP server"); | ||
} | ||
|
||
ftp_pasv($this->ftpConn, true); // Enable passive mode | ||
} | ||
|
||
public function queueFiles() | ||
{ | ||
$files = ftp_nlist($this->ftpConn, $this->remotePath); | ||
if ($files === false) { | ||
throw new Exception("Could not list files in directory"); | ||
} | ||
|
||
foreach ($files as $file) { | ||
if (preg_match('/\.(mp4|mp3)$/i', $file)) { | ||
$basename = basename($file); | ||
$link = "{$this->ftpUrl}{$basename}"; | ||
addVideo($link, Login::getStreamerId(), $basename); | ||
} | ||
} | ||
} | ||
|
||
static function copy($ftpUrl, $savePath) | ||
{ | ||
_error_log("FTP copy($ftpUrl, $savePath)"); | ||
|
||
$savePath = str_replace('..', '', $savePath); | ||
|
||
$command = "wget -O \"$savePath\" \"$ftpUrl\" "; | ||
|
||
exec($command); | ||
|
||
return file_exists($savePath) && filesize($savePath) > 20; | ||
} | ||
|
||
|
||
public function close() | ||
{ | ||
if ($this->ftpConn) { | ||
ftp_close($this->ftpConn); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters