Youtube-dl-WebUI/class/FileHandler.php

162 lines
3.2 KiB
PHP
Raw Normal View History

<?php
class FileHandler
{
private $config = [];
public function __construct()
{
$this->config = require dirname(__DIR__).'/config/config.php';
}
2016-01-21 00:36:34 +08:00
public function listFiles()
{
2016-01-21 00:36:34 +08:00
$files = [];
if(!$this->outuput_folder_exists())
return;
$folder = $this->get_downloads_folder().'/';
2016-01-21 00:36:34 +08:00
foreach(glob($folder.'*.*', GLOB_BRACE) as $file)
{
2016-01-21 00:36:34 +08:00
$content = [];
$content["name"] = str_replace($folder, "", $file);
$content["size"] = $this->to_human_filesize(filesize($file));
2016-01-21 00:36:34 +08:00
$files[] = $content;
}
2016-01-21 00:36:34 +08:00
return $files;
}
2016-01-21 05:06:26 +08:00
public function is_log_enabled()
{
return !!($this->config["log"]);
}
public function countLogs()
{
if(!$this->config["log"])
return;
if(!$this->outuput_folder_exists())
return;
$folder = dirname(__DIR__).'/'.$this->config["logFolder"].'/';
return count(glob($folder.'*.txt', GLOB_BRACE));
}
2016-01-21 04:55:28 +08:00
public function listLogs()
{
$files = [];
if(!$this->config["log"])
return;
if(!$this->outuput_folder_exists())
return;
$folder = $this->get_downloads_folder().'/';
2016-01-21 04:55:28 +08:00
2016-01-21 05:02:24 +08:00
foreach(glob($folder.'*.txt', GLOB_BRACE) as $file)
2016-01-21 04:55:28 +08:00
{
$content = [];
$content["name"] = str_replace($folder, "", $file);
$content["size"] = $this->to_human_filesize(filesize($file));
2016-06-20 16:39:06 +08:00
try {
$lines = explode("\r", file_get_contents($file));
2016-07-13 00:35:55 +08:00
$content["lastline"] = array_slice($lines, -1)[0];
2016-06-27 20:27:58 +08:00
$content["100"] = strpos($lines[count($lines)-1], ' 100% of ') > 0;
2016-06-20 16:39:06 +08:00
} catch (Exception $e) {
2016-07-13 00:35:55 +08:00
$content["lastline"] = '';
2016-06-20 16:39:06 +08:00
$content["100"] = False;
}
2016-06-21 00:18:20 +08:00
try {
$handle = fopen($file, 'r');
fseek($handle, filesize($file) - 1);
$lastc = fgets($handle, 1);
fclose($handle);
$content["ended"] = ($lastc === "\n");
} catch (Exception $e) {
2016-06-21 16:07:41 +08:00
$content["ended"] = False;
}
2016-06-21 00:18:20 +08:00
2016-06-20 16:39:06 +08:00
2016-01-21 04:55:28 +08:00
$files[] = $content;
}
return $files;
}
2016-01-21 05:08:07 +08:00
public function delete($id)
{
$folder = $this->get_downloads_folder().'/';
2016-01-21 04:55:28 +08:00
foreach(glob($folder.'*.*', GLOB_BRACE) as $file)
{
if(sha1(str_replace($folder, "", $file)) == $id)
2016-01-21 04:55:28 +08:00
{
unlink($file);
}
}
}
2016-01-21 05:08:07 +08:00
public function deleteLog($id)
2016-01-21 04:55:28 +08:00
{
$folder = dirname(__DIR__).'/'.$this->config["logFolder"].'/';
2016-01-21 05:02:24 +08:00
foreach(glob($folder.'*.txt', GLOB_BRACE) as $file)
{
if(sha1(str_replace($folder, "", $file)) == $id)
{
unlink($file);
}
}
}
private function outuput_folder_exists()
{
if(!is_dir($this->get_downloads_folder()))
{
//Folder doesn't exist
if(!mkdir($this->get_downloads_folder(),0777))
{
return false; //No folder and creation failed
}
}
return true;
}
public function to_human_filesize($bytes, $decimals = 0)
{
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
public function free_space()
{
return $this->to_human_filesize(disk_free_space($this->get_downloads_folder()));
}
public function get_downloads_folder()
{
$path = $this->config["outputFolder"];
if(strpos($path , "/") !== 0)
{
$path = dirname(__DIR__).'/' . $path;
}
return $path;
}
2016-01-21 05:06:26 +08:00
public function get_logs_folder()
{
return $this->config["logFolder"];
}
}
?>