Explicit creation and deletion of a temporary file

This commit is contained in:
Vitaliy Chesnokov 2019-11-26 18:29:31 +03:00 committed by Kuzia
parent 0ccaaeed3f
commit 681ff8f310

View File

@ -136,12 +136,15 @@ class Message extends HttpApi
private function prepareFile(string $fieldName, array $filePath): array
{
$filename = isset($filePath['filename']) ? $filePath['filename'] : null;
$deleteRequired = false;
if (isset($filePath['fileContent'])) {
// File from memory
$resource = fopen('php://temp', 'r+');
$filename = tempnam(sys_get_temp_dir(), "MAILGUN_TMP");
$resource = fopen($filename, 'r+');
fwrite($resource, $filePath['fileContent']);
rewind($resource);
$deleteRequired = true;
} elseif (isset($filePath['filePath'])) {
// File form path
$path = $filePath['filePath'];
@ -160,6 +163,7 @@ class Message extends HttpApi
'name' => $fieldName,
'content' => $resource,
'filename' => $filename,
'deleteRequired' => $deleteRequired,
];
}
@ -194,6 +198,13 @@ class Message extends HttpApi
if (is_array($param) && array_key_exists('content', $param) && is_resource($param['content'])) {
fclose($param['content']);
}
if (is_array($param)) {
$isFile = array_key_exists('filename', $param) && is_file($param['filename']);
$deleteRequired = $param['deleteRequired'] ?? false;
if ($isFile && $deleteRequired) {
unlink($param['filename']);
}
}
}
}
}