/** * Creates a Csv file consisting of the request body and then returns it. * * Called when this component receives an HTTP POST request to * /Csv/$orientation/temporary/$filename. * The request body should contain a JSON object representing the Csv file. * * @param string $orientation The orientation of the Csv file, e.g. portrait or landscape. * @param string $filename A freely chosen filename of the Csv file which should be stored. * @see http://wiki.spipu.net/doku.php */ public function addCsvPermanent($callName, $input, $params = array()) { $name = ''; if ($input->getRows() != null) { foreach ($input->getRows() as $row) { $name .= implode(',', $row); } } $name = sha1($name); // generate Csv $filePath = FSCsv::generateFilePath($params['folder'], $name); unset($name); if (!file_exists($this->config['DIR']['files'] . '/' . $filePath)) { FSCsv::generatepath($this->config['DIR']['files'] . '/' . dirname($filePath)); $result = $this->createCsv($input); // writes the file to filesystem $file = fopen($this->config['DIR']['files'] . '/' . $filePath, 'w'); if ($file) { fwrite($file, $result); fclose($file); } else { $fileObject = new File(); $fileObject->addMessage("Datei konnte nicht im Dateisystem angelegt werden."); $fileObject->setStatus(409); Logger::Log('POST postCsv failed', LogLevel::ERROR); return Model::isProblem($fileObject); } } if (isset($params['filename'])) { Model::header('Content-Type', 'text/csv'); Model::header('Content-Disposition', "attachment; filename=\"" . $params['filename'] . "\""); Model::header('Accept-Ranges', 'none'); if (isset($result)) { Model::header('Content-Length', strlen($result)); return isCreated($result); } else { readfile($this->config['DIR']['files'] . '/' . $filePath); Model::header('Content-Length', filesize($this->config['DIR']['files'] . '/' . $filePath)); return isCreated(); } } else { $CsvFile = new File(); $CsvFile->setStatus(201); $CsvFile->setAddress($filePath); $CsvFile->setMimeType("application/Csv"); if (file_exists($this->config['DIR']['files'] . '/' . $filePath)) { $CsvFile->setFileSize(filesize($this->config['DIR']['files'] . '/' . $filePath)); $hash = sha1(file_get_contents($this->config['DIR']['files'] . '/' . $filePath)); $CsvFile->setHash($hash); } return Model::isCreated($CsvFile); } }
public function addPdfFromFile3($callName, $files, $params = array()) { // merge all file objects to one pdf $hashArray = array(); foreach ($files as $part) { if ($part->getBody() !== null) { $hashArray[] = $part->getBody(); } else { $hashArray[] = $part->getAddress() . $part->getDisplayName(); } } $name = sha1(implode("\n", $hashArray)); $targetPath = FSPdf::generateFilePath($params['folder'], $name); if (!file_exists($this->config['DIR']['files'] . '/' . $targetPath)) { $body = ""; foreach ($files as $part) { if ($part->getBody() !== null) { // use body $body .= $part->getBody(true) . '<br>'; } else { $file = $this->config['DIR']['files'] . '/' . $part->getAddress(); if (file_exists($file)) { $text = file_get_contents($file); if (mb_detect_encoding($text, 'UTF-8', true) === false) { $text = utf8_encode($text); } $text = htmlentities(htmlentities($text)); $body .= $text . '<br>'; } else { // failure } } } ///echo $body; FSPdf::generatepath($this->config['DIR']['files'] . '/' . dirname($targetPath)); $data = new Pdf(); $data->setText($body); $result = FSPdf::createPdf($data); // writes the file to filesystem $file = fopen($this->config['DIR']['files'] . '/' . $targetPath, 'w'); if ($file) { fwrite($file, $result); fclose($file); } else { $fileObject = new File(); $fileObject->addMessage("Datei konnte nicht im Dateisystem angelegt werden."); $fileObject->setStatus(409); Logger::Log('POST postPdf failed', LogLevel::ERROR); return Model::isProblem($fileObject); } } $pdfFile = new File(); $pdfFile->setStatus(201); $pdfFile->setAddress($targetPath); $pdfFile->setMimeType("application/pdf"); if (file_exists($this->config['DIR']['files'] . '/' . $targetPath)) { $pdfFile->setFileSize(filesize($this->config['DIR']['files'] . '/' . $targetPath)); $hash = sha1(file_get_contents($this->config['DIR']['files'] . '/' . $targetPath)); $pdfFile->setHash($hash); } return Model::isCreated($pdfFile); }