Verified Commit 5281dfed authored by Cyril Vazquez's avatar Cyril Vazquez
Browse files

Receive package from filename, base64 encoded, plain contents or zip

parent 77483ca4
......@@ -132,56 +132,23 @@ class ArchiveTransfer extends abstractMessage
$message->messageId = $messageId;
$message->type = "ArchiveTransfer";
$message->receptionDate = \laabs::newTimestamp();
$message->schema = $schema;
$messageDir = $this->messageDirectory.DIRECTORY_SEPARATOR.(string) $message->messageId;
if (!is_dir($messageDir)) {
mkdir($messageDir, 0777, true);
}
$messageFile = base64_decode($messageFile);
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$contentType = $finfo->buffer($messageFile);
if ($contentType == 'application/zip' || $contentType == 'application/octet-stream') {
$attachments = $this->receiveZip($message, $messageFile, $filename);
} else {
if (!$filename) {
$filename = (string)$message->messageId;
}
file_put_contents($messageDir.DIRECTORY_SEPARATOR.$filename, $messageFile);
$message->path = $messageDir.DIRECTORY_SEPARATOR.$filename;
if (count($attachments)) {
foreach ($attachments as $attachment) {
$attachment->data = base64_decode($attachment->data);
file_put_contents($messageDir.DIRECTORY_SEPARATOR.$attachment->filename, $attachment->data);
}
}
}
$this->receivePackage($message, $messageFile, $attachments, $filename);
if (empty($schema)) {
$contentType = $finfo->file($message->path);
if ($contentType == 'application/xml') {
$xml = new \DOMDocument();
$xml->load($message->path);
$messageNamespace = $xml->documentElement->namespaceURI;
if (!$messageSchema = \laabs::resolveXmlNamespace($messageNamespace)) {
throw \laabs::newException('medona/invalidMessageException', "Unknown message namespace'.$messageNamespace", 400);
}
$message->schema = $messageSchema;
} else {
$message->schema = 'recordsManagement';
}
} else {
$message->schema = $schema;
$this->detectSchema($message);
}
try {
$archiveTransferController = \laabs::newController($message->schema. '/ArchiveTransfer');
$archiveTransferController->receive($message, $attachments);
$archiveTransferController->receive($message);
try {
$message->senderOrg = $this->orgController->getOrgByRegNumber($message->senderOrgRegNumber);
$message->senderOrgName = $message->senderOrg->orgName;
......@@ -248,6 +215,106 @@ class ArchiveTransfer extends abstractMessage
return $acknowledgement;
}
protected function receivePackage($message, $messageFile, $attachments, $filename)
{
// Valid URL file:// http:// data://
if (filter_var($messageFile, FILTER_VALIDATE_URL)) {
$data = stream_get_contents($handler);
} elseif (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $messageFile)) {
$data = base64_decode($messageFile);
} elseif (is_file($messageFile)) {
$data = file_get_contents($messageFile);
}
$mediatype = $this->finfo->buffer($data);
if ($mediatype == 'application/zip' || $mediatype == 'application/octet-stream') {
$this->receiveZip($message, $data, $attachments, $filename);
} else {
$this->receiveFiles($message, $data, $attachments, $filename);
}
}
protected function receiveZip($message, $data, $filename)
{
$messageDir = $this->messageDirectory.DIRECTORY_SEPARATOR.(string) $message->messageId;
$zipfile = \laabs\tempnam();
file_put_contents($zipfile, $data);
$tmpdir = \laabs::getTmpDir();
$zip = \laabs::newService('dependency/fileSystem/plugins/zip');
$zip->extract($zipfile, $tmpdir);
$zipContents = scandir($tmpdir);
if (!isset($zipContents[2])) {
throw \laabs::newException('medona/invalidMessageException', "Invalid message", 400);
}
$message->path = $messageDir.DIRECTORY_SEPARATOR.$filename;
$message->attachments = [];
if (is_dir($tmpdir.DIRECTORY_SEPARATOR.$zipContents[2])) {
$zipFolder = $tmpdir.DIRECTORY_SEPARATOR.$zipContents[2];
foreach (scandir($zipFolder) as $file) {
if ($file != "." && $file != "..") {
rename($zipFolder.DIRECTORY_SEPARATOR.$file, $messageDir.DIRECTORY_SEPARATOR.$file);
if ($file != $filename) {
$message->attachments[] = $file;
}
}
}
rmdir($zipFolder);
}
}
protected function receiveFiles($message, $data, $attachments, $filename=false)
{
$messageDir = $this->messageDirectory.DIRECTORY_SEPARATOR.(string) $message->messageId;
if (!$filename) {
$filename = (string) $message->messageId;
}
file_put_contents($messageDir.DIRECTORY_SEPARATOR.$filename, $data);
$message->path = $messageDir.DIRECTORY_SEPARATOR.$filename;
$message->attachments = [];
if (count($attachments)) {
foreach ($attachments as $attachment) {
if (filter_var($attachment->data, FILTER_VALIDATE_URL)) {
$data = stream_get_contents($attachment->data);
} elseif (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $attachment->data)) {
$data = base64_decode($attachment->data);
} elseif (is_file($attachment->data)) {
$data = file_get_contents($attachment->data);
}
file_put_contents($messageDir.DIRECTORY_SEPARATOR.$attachment->filename, $data);
$message->attachments[] = $attachment->filename;
}
}
}
protected function detectSchema($message)
{
$mediatype = $this->finfo->file($message->path);
if ($mediatype == 'application/xml') {
$xml = new \DOMDocument();
$xml->load($message->path);
$messageNamespace = $xml->documentElement->namespaceURI;
if (!$messageSchema = \laabs::resolveXmlNamespace($messageNamespace)) {
throw \laabs::newException('medona/invalidMessageException', "Unknown message namespace'.$messageNamespace", 400);
}
$message->schema = $messageSchema;
} else {
$message->schema = 'recordsManagement';
}
}
protected function receiveXmlFile($message, $messageFile, $attachments = array())
{
$messageDir = $this->messageDirectory.DIRECTORY_SEPARATOR.(string) $message->messageId;
......@@ -299,46 +366,6 @@ class ArchiveTransfer extends abstractMessage
}
}
protected function receiveZip($message, $zipstream, $filename)
{
$messageDir = $this->messageDirectory.DIRECTORY_SEPARATOR.(string) $message->messageId;
$zipfile = \laabs\tempnam();
file_put_contents($zipfile, $zipstream);
$tmpdir = \laabs::getTmpDir();
$zip = \laabs::newService('dependency/fileSystem/plugins/zip');
$zip->extract($zipfile, $tmpdir);
$zipContents = scandir($tmpdir);
if (!isset($zipContents[2])) {
throw \laabs::newException('medona/invalidMessageException', "Invalid message", 400);
}
$attachments = [];
if (is_dir($tmpdir.DIRECTORY_SEPARATOR.$zipContents[2])) {
$zipFolder = $tmpdir.DIRECTORY_SEPARATOR.$zipContents[2];
foreach (scandir($zipFolder) as $file) {
if ($file != "." && $file != "..") {
rename($zipFolder.DIRECTORY_SEPARATOR.$file, $messageDir.DIRECTORY_SEPARATOR.$file);
if ($file != $filename) {
$attachment = new \stdClass();
$attachment->data = file_get_contents($messageDir.DIRECTORY_SEPARATOR.$file);
$attachment->filename = $file;
$attachments[] = $attachment;
}
}
}
rmdir($zipFolder);
}
$message->path = $messageDir.DIRECTORY_SEPARATOR.$filename;
return $attachments;
}
/**
* Validate the messages
......
......@@ -29,8 +29,9 @@ interface ArchiveTransferInterface
{
/**
* Receive message with all contents embedded
* @param string $message The message object
*
* @param string $message The message object
*
* @return medona/message The acknowledgement
*/
public function receive($message);
......
......@@ -79,6 +79,8 @@ class message
protected $parentsDerogation;
protected $finfo;
/**
* Constructor
* @param string $messageDirectory The message directory
......@@ -123,6 +125,8 @@ class message
$this->autoProcessSize = $autoProcessSize;
$this->errors = array();
$this->finfo = new \finfo(\FILEINFO_MIME_TYPE);
}
/**
......
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