2016-10-28 05:30:57 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Helpers;
|
|
|
|
|
|
|
|
use Illuminate\Http\UploadedFile;
|
2016-10-30 18:36:34 +00:00
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
2016-10-28 05:30:57 +01:00
|
|
|
|
|
|
|
class FileHelper
|
|
|
|
{
|
2016-10-30 18:36:34 +00:00
|
|
|
public static function deleteIfEmpty($folderPath)
|
|
|
|
{
|
|
|
|
// Another request may have got here first!
|
|
|
|
if (!is_dir($folderPath))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$queueIterator = new \DirectoryIterator($folderPath);
|
|
|
|
$files = 0;
|
|
|
|
|
|
|
|
foreach ($queueIterator as $item)
|
|
|
|
{
|
|
|
|
if ($item->isDot())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$files++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($files == 0)
|
|
|
|
{
|
|
|
|
@rmdir($folderPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 05:30:57 +01:00
|
|
|
public static function getQueuePath($queueUid)
|
|
|
|
{
|
|
|
|
$path = join(DIRECTORY_SEPARATOR, [
|
|
|
|
dirname(dirname(__DIR__)),
|
|
|
|
'storage',
|
|
|
|
'app',
|
|
|
|
'analysis-queue',
|
|
|
|
str_replace(['.', '/', '\\'], '', $queueUid)
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!file_exists($path))
|
|
|
|
{
|
|
|
|
mkdir($path, 0755, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
public static function saveExtractedFile(File $extractedFile, $destinationPath, $overrideFilename = null)
|
|
|
|
{
|
|
|
|
$tempFilename = join(DIRECTORY_SEPARATOR, [
|
|
|
|
$destinationPath,
|
|
|
|
is_null($overrideFilename) ? MiscHelper::randomString(20) : basename($overrideFilename)
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Only add an extension if an override filename was not given, assume this is present
|
|
|
|
if (is_null($overrideFilename))
|
|
|
|
{
|
|
|
|
$extension = $extractedFile->guessExtension();
|
|
|
|
if (!is_null($extension))
|
|
|
|
{
|
|
|
|
$tempFilename .= '.' . $extension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$extractedFile->move(dirname($tempFilename), basename($tempFilename));
|
|
|
|
return new File($tempFilename);
|
|
|
|
}
|
|
|
|
|
2016-10-28 05:30:57 +01:00
|
|
|
public static function saveUploadedFile(UploadedFile $uploadedFile, $destinationPath, $overrideFilename = null)
|
|
|
|
{
|
|
|
|
$tempFilename = join(DIRECTORY_SEPARATOR, [
|
|
|
|
$destinationPath,
|
|
|
|
is_null($overrideFilename) ? MiscHelper::randomString(20) : basename($overrideFilename)
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Only add an extension if an override filename was not given, assume this is present
|
|
|
|
if (is_null($overrideFilename))
|
|
|
|
{
|
|
|
|
$extension = $uploadedFile->guessExtension();
|
|
|
|
if (!is_null($extension))
|
|
|
|
{
|
|
|
|
$tempFilename .= '.' . $extension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$uploadedFile->move(dirname($tempFilename), basename($tempFilename));
|
|
|
|
return new File($tempFilename);
|
|
|
|
}
|
|
|
|
}
|