Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ notice.txt
.phpunit.result.cache
docs/
.claude
.antigravity*
logs
data
tmp
tmp
22 changes: 20 additions & 2 deletions src/api/info.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,28 @@ function getInfoAboutHash($hash)
return array('status'=>'err','reason'=>'File not found');
$size = filesize($file);
$size_hr = renderSize($size);
$content_type = exec("file -bi " . escapeshellarg($file));

$content_type = false;
try {
if (class_exists('finfo')) {
$finfo = new finfo(FILEINFO_MIME);
$content_type = $finfo->file($file);
}
} catch (\Throwable $t) {
// ignore
}

if (!$content_type && function_exists('mime_content_type')) {
$content_type = @mime_content_type($file);
}

if (!$content_type && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
$content_type = @exec("file -bi " . escapeshellarg($file));
}

$type = $content_type;
if($content_type && strpos($content_type,'/')!==false && strpos($content_type,';')!==false)
{
$type = $content_type;
$c = explode(';',$type);
$type = $c[0];
}
Expand Down
2 changes: 1 addition & 1 deletion src/content-controllers/image/image.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ function gifToMP4($gifpath,$target)

function saveObjOfImage($im,$path,$type)
{
$tmppath = '/tmp/'.getNewHash($type,12);
$tmppath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . getNewHash($type,12);
switch($type)
{
case 'jpeg':
Expand Down
56 changes: 38 additions & 18 deletions src/inc/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,15 @@ function storageControllerUpload($hash)

function stringInFile($string,$file)
{
$handle = fopen($file, 'r');
if (!file_exists($file)) return false;
$handle = @fopen($file, 'r');
if (!$handle) return false;
while (($line = fgets($handle)) !== false) {
$line=trim($line);
if($line==$string) return true;
if($line==$string) {
fclose($handle);
return true;
}
}
fclose($handle);
return false;
Expand Down Expand Up @@ -484,18 +489,24 @@ function renderSize($byte)

function getTypeOfFile($url)
{
// on linux use the "file" command or it will handle everything as octet-stream
if(strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')
{
$content_type = exec("file -bi " . escapeshellarg($url));
if($content_type && strpos($content_type,'/')!==false && strpos($content_type,';')!==false)
$type = $content_type;
$type = false;
try {
if (class_exists('finfo')) {
$finfo = new finfo(FILEINFO_MIME);
$type = $finfo->file($url);
}
} catch (\Throwable $t) {
// ignore
}
else
{
//for windows we'll use mime_content_type. Make sure you have enabled the "exif" extension in php.ini
$type = mime_content_type($url);

if (!$type && function_exists('mime_content_type')) {
$type = @mime_content_type($url);
}

if (!$type && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
$type = @exec("file -bi " . escapeshellarg($url));
}

if(!$type) return false;
if(startsWith($type,'text')) return 'text';
$arr = explode(';', trim($type));
Expand Down Expand Up @@ -1387,13 +1398,22 @@ function rebuildMeta(){
function getFileMimeType($file)
{
try {
$finfo = new finfo(FILEINFO_MIME_TYPE);
return $finfo->file($file);
} catch (Exception $e) {
//fallback to shell command if finfo is not available
$mimeType = shell_exec('file --mime-type -b ' . escapeshellarg($file));
return trim($mimeType);
if (class_exists('finfo')) {
$finfo = new finfo(FILEINFO_MIME_TYPE);
return $finfo->file($file);
}
} catch (\Throwable $e) {
// ignore
}

if (function_exists('mime_content_type')) {
$mime = @mime_content_type($file);
if ($mime) return $mime;
}

//fallback to shell command if finfo and mime_content_type are not available
$mimeType = shell_exec('file --mime-type -b ' . escapeshellarg($file));
return trim($mimeType);
}

function getRelativeToDataPath(string $path): string
Expand Down
16 changes: 12 additions & 4 deletions web/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@
// redis
if(!defined('REDIS_CACHING') || REDIS_CACHING == true)
{
$GLOBALS['redis'] = new Redis();
$GLOBALS['redis']->connect((!defined('REDIS_SERVER'))?'localhost':REDIS_SERVER, (!defined('REDIS_PORT'))?6379:REDIS_PORT);
try {
$GLOBALS['redis'] = new Redis();
$redis_host = (!defined('REDIS_SERVER')) ? 'localhost' : REDIS_SERVER;
$redis_port = (!defined('REDIS_PORT')) ? 6379 : REDIS_PORT;
// connect with 1.0 second timeout to prevent locking up PHP workers if Redis is down
@$GLOBALS['redis']->connect($redis_host, $redis_port, 1.0);
} catch (\Throwable $e) {
$GLOBALS['redis'] = null;
error_log("Failed to connect to Redis: " . $e->getMessage());
}
}


//parse the URL to an array and filter it
$url = array_filter(explode('/',ltrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?? '','/')));
//parse the URL to an array and filter it, keeping '0' values
$url = array_filter(explode('/',ltrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?? '','/')), 'strlen');

if($url[0] == 'api')
{
Expand Down
Loading