Beispiel #1
0
 public function handleQueue()
 {
     global $mtlda, $config, $audit, $mbus;
     if ($config->isCreatePreviewImageOnImport()) {
         try {
             $imagectrl = new \Mtlda\Controllers\ImageController();
         } catch (\Exception $e) {
             static::raiseError(__METHOD__ . '(), unable to load ImageController!');
             return false;
         }
     }
     if ($config->isPdfSigningEnabled() && ($sign_pos = $config->getPdfSigningIconPosition()) === false) {
         static::raiseError(__METHOD__ . '(), PDF-Signing is enabled but no signing-icon-position is defined!');
         return false;
     }
     try {
         $storage = new StorageController();
     } catch (\Exception $e) {
         static::raiseError(__METHOD__ . ', failed to load StorageController!');
         return false;
     }
     $files = array();
     if ($this->scanDirectory(self::INCOMING_DIRECTORY, $files) === false) {
         static::raiseError(__CLASS__ . '::scanDirectory() returned false!');
         return false;
     }
     if (!isset($files) || empty($files)) {
         return true;
     }
     $start = 10;
     $end = 90;
     $cnt = 0;
     $total = count($files);
     $steps = ($end - $start) / $total;
     foreach ($files as $file) {
         $cnt += 1;
         $lockfile = "{$file['fqpn']}.lock";
         $mbus->sendMessageToClient('import-reply', "Importing file {$cnt} of {$total}.", floor($start + $cnt * $steps) . '%');
         // if file has vanished in the meantime.
         if (!file_exists($file['fqpn'])) {
             continue;
         }
         if (file_exists($lockfile)) {
             if (($timestamp = file_get_contents($lockfile)) === false) {
                 static::raiseError(__METHOD__ . "(), failed to read {$lockfile}!");
                 return false;
             }
             if (!isset($timestamp) || empty($timestamp) || !is_numeric($timestamp)) {
                 static::raiseError(__METHOD__ . "(), {$lockfile} does not contain a timestamp!");
                 return false;
             }
             if (time() < $timestamp + 5 * 60) {
                 continue;
             }
         }
         if (file_put_contents($lockfile, time()) === false) {
             static::raiseError(__METHOD__ . "(), failed to write timestamp into {$lockfile}!");
             return false;
         }
         if (($guid = $mtlda->createGuid()) === false) {
             static::raiseError(get_class($mtlda) . '::createGuid() returned false!');
             return false;
         }
         try {
             $queueitem = new \Mtlda\Models\QueueItemModel();
         } catch (\Exception $e) {
             static::raiseError(__METHOD__ . '(), failed to load QueueItemModel!');
             return false;
         }
         try {
             $queueitem->setGuid($guid);
             $queueitem->setFileName($file['filename']);
             $queueitem->setFileSize($file['size']);
             $queueitem->setFileHash($file['hash']);
             $queueitem->setState('new');
             $queueitem->setTime(microtime(true));
             $queueitem->setProcessingFlag(false);
             if ($config->isPdfSigningEnabled()) {
                 $queueitem->setSigningIconPosition($sign_pos);
             }
         } catch (\Exception $e) {
             static::raiseError('Failed to prepare QueueItemModel! ', false, $e);
             return false;
         }
         $in_file = $file['fqpn'];
         $in_dir = dirname($in_file);
         if (!file_exists($in_file)) {
             static::raiseError(__METHOD__ . "(), file {$in_file} does not exist!");
             return false;
         }
         if (($dsc_file = preg_replace('/\\.pdf$/i', '.dsc', $in_file)) === false) {
             static::raiseError(__METHOD__ . '(), preg_replace() returned false!');
             return false;
         }
         if (($work_file = $queueitem->getFilePath()) === false) {
             static::raiseError(get_class($queueitem) . '::getFilePath() returned false!');
             return false;
         }
         if (isset($dsc_file) && !empty($dsc_file) && file_exists($dsc_file)) {
             if (($description = file_get_contents($dsc_file)) === false) {
                 static::raiseError(__METHOD__ . "(), file_get_contents({$dsc_file}) returned false!");
                 return false;
             }
             if (!$queueitem->setDescription($description)) {
                 static::raiseError(get_class($queueitem) . '::setDescription() returned false!');
                 return false;
             }
         }
         // create the target directory structure
         if (!$storage->createDirectoryStructure(dirname($work_file))) {
             static::raiseError(get_class($storage) . '::createDirectoryStructure() returned false!');
             return false;
         }
         if (copy($in_file, $work_file) === false) {
             static::raiseError(__METHOD__ . "(), copy({$in_file}, {$work_file}) returned false!");
             return false;
         }
         if (!$queueitem->save()) {
             $queueitem->delete();
             static::raiseError(get_class($queueitem) . '::save() returned false!');
             return false;
         }
         if (!unlink($in_file)) {
             static::raiseError(__METHOD__ . "(), unlink({$in_file}) failed!");
             return false;
         }
         if (isset($dsc_file) && !empty($dsc_file) && file_exists($dsc_file)) {
             if (!unlink($dsc_file)) {
                 static::raiseError(__METHOD__ . "(), unlink({$dsc_file}) failed!");
                 return false;
             }
         }
         if ($in_dir != self::INCOMING_DIRECTORY) {
             if (!$this->unlinkDirectory($in_dir)) {
                 static::raiseError(__CLASS__ . '::unlinkDirectory() returned false!');
                 return false;
             }
         }
         $json_str = json_encode(array('file_name' => $file['filename'], 'file_size' => $file['size'], 'file_hash' => $file['hash'], 'state' => 'new'));
         if (!$json_str) {
             $queueitem->delete();
             static::raiseError(__METHOD__ . '(), json_encode() returned false!');
             return false;
         }
         try {
             $audit->log($json_str, "import", "queue", $guid);
         } catch (\Exception $e) {
             $queueitem->delete();
             static::raiseError(get_class($audit) . '::log() returned false!');
             return false;
         }
         if ($config->isCreatePreviewImageOnImport()) {
             if (!$imagectrl->createPreviewImage($queueitem, false)) {
                 static::raiseError(get_class($imagectrl) . '::savePreviewImage() returned false!');
                 return false;
             }
         }
         if (file_exists($lockfile) && !unlink($lockfile)) {
             static::raiseError(__METHOD__ . "(), unlink({$lockfile}) failed!");
             return false;
         }
     }
     return true;
 }