Ejemplo n.º 1
0
 /**
  * Writes the file with the is $fileId to the legacy import folder. The file name will used from
  * argument $fileName and the file was successfully created or an identical file was already found,
  * $fileName will held the uid of the new created file record.
  *
  * @param string $fileName The file name for the new file. Value would be changed to the uid of the new created file record.
  * @param int $fileId The id of the file in data array
  * @return bool
  */
 protected function writeSysFileResourceForLegacyImport(&$fileName, $fileId)
 {
     if ($this->legacyImportFolder === null) {
         return false;
     }
     if (!isset($this->dat['files'][$fileId])) {
         $this->error('ERROR: File ID "' . $fileId . '" could not be found');
         return false;
     }
     $temporaryFile = $this->writeTemporaryFileFromData($fileId, 'files');
     if ($temporaryFile === null) {
         // error on writing the file. Error message was already added
         return false;
     }
     $importFolder = $this->legacyImportFolder;
     if (isset($this->dat['files'][$fileId]['relFileName'])) {
         $relativeFilePath = PathUtility::dirname($this->dat['files'][$fileId]['relFileName']);
         if (!$this->legacyImportFolder->hasFolder($relativeFilePath)) {
             $this->legacyImportFolder->createFolder($relativeFilePath);
         }
         $importFolder = $this->legacyImportFolder->getSubfolder($relativeFilePath);
     }
     $fileObject = null;
     try {
         // check, if there is alreay the same file in the folder
         if ($importFolder->hasFile($fileName)) {
             $fileStorage = $importFolder->getStorage();
             $file = $fileStorage->getFile($importFolder->getIdentifier() . $fileName);
             if ($file->getSha1() === sha1_file($temporaryFile)) {
                 $fileObject = $file;
             }
         }
     } catch (Exception $e) {
     }
     if ($fileObject === null) {
         try {
             $fileObject = $importFolder->addFile($temporaryFile, $fileName, DuplicationBehavior::RENAME);
         } catch (Exception $e) {
             $this->error('Error: no file could be added to the storage for file name ' . $this->alternativeFileName[$temporaryFile]);
         }
     }
     if (md5_file(PATH_site . $fileObject->getPublicUrl()) == $this->dat['files'][$fileId]['content_md5']) {
         $fileName = $fileObject->getUid();
         $this->fileIDMap[$fileId] = $fileName;
         $this->filePathMap[$fileId] = $fileObject->getPublicUrl();
         return true;
     } else {
         $this->error('ERROR: File content "' . $this->dat['files'][$fileId]['relFileName'] . '" was corrupted');
     }
     return false;
 }