Verified Commit b98d5b91 authored by Cyril Vazquez's avatar Cyril Vazquez
Browse files

Allow multipart with files

parent 80f9510e
Pipeline #18870 failed with stages
in 38 seconds
......@@ -50,10 +50,6 @@ class Multiparts
*/
public function receive($package, $params, $messageDirectory)
{
if (!isset($package->data) || empty($package->data)) {
throw new \core\Exception\BadRequestException("Package data is mandatory", 400);
}
if (isset($messageDirectory) && !empty($messageDirectory)) {
if (!is_dir($messageDirectory)) {
throw new \core\Exception\BadRequestException("MessageDirectory is not a directory", 400);
......@@ -61,14 +57,18 @@ class Multiparts
$this->messageDirectory = $messageDirectory;
}
if (is_resource($package->data)) {
$data = stream_get_contents(\core\Encoding\Base64::decode($package->data));
} elseif (filter_var($package->data, FILTER_VALIDATE_URL)) {
// TODO verify
} elseif (preg_match('%^[a-zA-Z0-9\\\\/+]*={0,2}$%', $package->data)) {
$data = \core\Encoding\Base64::decode($package->data);
} elseif (is_file($package->data)) {
$data = file_get_contents($package->data);
if (is_object($package)) {
if (is_resource($package->data)) {
$data = stream_get_contents(\core\Encoding\Base64::decode($package));
} elseif (filter_var($package->data, FILTER_VALIDATE_URL)) {
// TODO verify
} elseif (preg_match('%^[a-zA-Z0-9\\\\/+]*={0,2}$%', $package->data)) {
$data = \core\Encoding\Base64::decode($package->data);
} elseif (is_file($package->data)) {
$data = file_get_contents($package->data);
}
} elseif(is_array($package)) {
$data = stream_get_contents($package['handler']);
}
// to use to modify xml, replace namespace for seda2
......@@ -88,45 +88,50 @@ class Multiparts
if (!empty($params['attachments'])) {
foreach ($params['attachments'] as $key => $attachment) {
if (empty($attachment->name)) {
throw new \core\Exception\BadRequestException("Attachment name is mandatory", 400);
}
$attachmentFileName = $messageDirectory . DIRECTORY_SEPARATOR . $attachment->name;
if ($attachment->encoding == 'base64') {
switch (true) {
case is_resource($attachment->data):
case is_string($attachment->data) &&
(
filter_var(substr($attachment->data, 0, 10), FILTER_VALIDATE_URL) ||
is_file($attachment->data)
):
$handler = \core\Encoding\Base64::decode($attachment->data);
file_put_contents($attachmentFileName, stream_get_contents($handler));
break;
case is_string($attachment->data):
file_put_contents($attachmentFileName, \core\Encoding\Base64::decode($attachment->data));
break;
default:
throw new \core\Exception\BadRequestException("Data attachment format is not valid", 400);
break;
if (is_object($attachment)) {
if (empty($attachment->name)) {
throw new \core\Exception\BadRequestException("Attachment name is mandatory", 400);
}
} else {
switch (true) {
case is_resource($attachment->data):
case is_string($attachment->data) &&
(
filter_var(substr($attachment->data, 0, 10), FILTER_VALIDATE_URL) ||
is_file($attachment->data)
):
file_put_contents($attachmentFileName, file_get_contents($attachment->data));
break;
case is_string($attachment->data):
file_put_contents($attachmentFileName, $attachment->data);
break;
default:
throw new \core\Exception\BadRequestException("Data attachment format is not valid", 400);
break;
$attachmentFileName = $messageDirectory . DIRECTORY_SEPARATOR . $attachment->name;
if ($attachment->encoding == 'base64') {
switch (true) {
case is_resource($attachment->data):
case is_string($attachment->data) &&
(
filter_var(substr($attachment->data, 0, 10), FILTER_VALIDATE_URL) ||
is_file($attachment->data)
):
$handler = \core\Encoding\Base64::decode($attachment->data);
file_put_contents($attachmentFileName, stream_get_contents($handler));
break;
case is_string($attachment->data):
file_put_contents($attachmentFileName, \core\Encoding\Base64::decode($attachment->data));
break;
default:
throw new \core\Exception\BadRequestException("Data attachment format is not valid", 400);
break;
}
} else {
switch (true) {
case is_resource($attachment->data):
case is_string($attachment->data) &&
(
filter_var(substr($attachment->data, 0, 10), FILTER_VALIDATE_URL) ||
is_file($attachment->data)
):
file_put_contents($attachmentFileName, file_get_contents($attachment->data));
break;
case is_string($attachment->data):
file_put_contents($attachmentFileName, $attachment->data);
break;
default:
throw new \core\Exception\BadRequestException("Data attachment format is not valid", 400);
break;
}
}
} elseif (is_array($attachment)) {
$attachmentFileName = $messageDirectory . DIRECTORY_SEPARATOR . $attachment['name'];
file_put_contents($attachmentFileName, stream_get_contents($attachment['handler']));
}
$this->attachments[] = $attachmentFileName;
......
......@@ -26,19 +26,6 @@ class Zip
{
protected $messageDirectory;
/**
*
*
* @param string $messageDirectory [description]
*/
public function __construct($messageDirectory)
{
$this->messageDirectory = \laabs::configuration("medona")['messageDirectory'];
if (!is_dir($this->messageDirectory)) {
mkdir($this->messageDirectory, 0777, true);
}
}
/**
* Get archive transfer transformed by connector
*
......@@ -49,21 +36,26 @@ class Zip
*/
public function receive($package, $params, $messageDirectory)
{
if (!isset($package->data) || empty($package->data)) {
if (is_object($package) && (!isset($package->data) || empty($package->data))) {
throw new \core\Exception\BadRequestException("Package data is mandatory", 400);
}
if (isset($messageDirectory) && !empty($messageDirectory)) {
if (!is_dir($messageDirectory)) {
throw new \core\Exception\BadRequestException("MessageDirectory is not a directory", 400);
}
$this->messageDirectory = $messageDirectory;
if (!is_dir($messageDirectory)) {
throw new \core\Exception\BadRequestException("MessageDirectory is not a directory", 400);
}
$this->messageDirectory = $messageDirectory;
if (is_object($package)) {
$data = $package->data;
$encoding = $package->encoding;
} elseif (is_array($package)) {
$data = $package['handler'];
$encoding = null;
}
$packageName = $package->name ?? (string) \laabs::newId();
$zipTmpFile = \laabs\tempnam();
$this->receiveFile($package->data, $zipTmpFile, $package->encoding);
$this->receiveFile($data, $zipTmpFile, $encoding);
$tmpDir = \laabs\tempdir();
......@@ -108,6 +100,9 @@ class Zip
} else {
switch (true) {
case is_resource($data):
file_put_contents($zipTmpFile, stream_get_contents($data));
break;
case is_string($data) &&
(
filter_var(substr($data, 0, 10), FILTER_VALIDATE_URL) ||
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment