[UTIL] Add option for setting a file suffix on TemporaryFile constructor and add missing TemporaryFileException

This commit is contained in:
Hugo Sales 2021-04-29 20:27:41 +00:00
parent a165e533bb
commit 29457ef50d
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 41 additions and 1 deletions

View File

@ -0,0 +1,37 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace App\Util\Exception;
/**
* Exception wrapper for TemporaryFile errors
*
* @category Exception
* @package GNUsocial
*
* @author Alexei Sorokin <sor.alexei@meowr.ru>
* @author Hugo Sales <hugo@hsal.es>
* @copyright 2020, 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class TemporaryFileException extends \Exception
{
}

View File

@ -19,6 +19,8 @@
namespace App\Util;
use App\Util\Exception\TemporaryFileException;
/**
* Class oriented at providing automatic temporary file handling.
*
@ -39,9 +41,10 @@ class TemporaryFile extends \SplFileInfo
*/
public function __construct(
?string $prefix = null,
?string $suffix = null,
?string $mode = null
) {
$filename = tempnam(sys_get_temp_dir(), $prefix ?? 'gs-php');
$filename = tempnam(sys_get_temp_dir(), $prefix ?? 'gs-php') . ($suffix ?? '');
if ($filename === false) {
throw new TemporaryFileException('Could not create file: ' . $filename);