Пример #1
0
 /**
  * Prepares the saving process by generating the hash and the place where the file is stored.
  *
  * Called when this component receives an HTTP POST request to
  * /$a/$b/$c/$file.
  * The request body should contain a JSON object representing the file's
  * attributes.
  */
 public function addFile($callName, $input, $params = array())
 {
     $fileObject = $input;
     $fileContent = $fileObject->getBody(true);
     $fileObject->setBody(null);
     $fileObject->setHash(sha1($fileContent));
     $filePath = FSFile::generateFilePath(FSFile::getBaseDir(), $fileObject->getHash());
     $fileObject->setAddress(FSFile::getBaseDir() . '/' . $fileObject->getHash());
     if (!file_exists($this->config['DIR']['files'] . '/' . $filePath)) {
         FSFile::generatepath($this->config['DIR']['files'] . '/' . dirname($filePath));
         // writes the file to filesystem
         $file = fopen($this->config['DIR']['files'] . '/' . $filePath, 'w');
         if ($file) {
             fwrite($file, $fileContent);
             fclose($file);
             $fileObject->setStatus(201);
         } else {
             $fileObject->addMessage("Datei konnte nicht im Dateisystem angelegt werden.");
             $fileObject->setStatus(409);
             Logger::Log('POST postFile failed', LogLevel::ERROR);
             return Model::isCreated($fileObject);
         }
     }
     // resets the file content
     $fileObject->setBody(null);
     // generate new file address, file size and file hash
     $fileObject->setAddress($filePath);
     $fileObject->setFileSize(filesize($this->config['DIR']['files'] . '/' . $filePath));
     $fileObject->setHash(sha1_file($this->config['DIR']['files'] . '/' . $filePath));
     $fileObject->setMimeType(MimeReader::get_mime($this->config['DIR']['files'] . '/' . $filePath));
     return Model::isCreated($fileObject);
 }