From 642e09f282d77097317747e1dbde6804010144cc Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Wed, 3 Jul 2013 17:44:16 +0200 Subject: [PATCH] Added attachment import --- .../AbstractAttachmentImporter.class.php | 78 +++++++++++++++++++ .../AbstractCommentImporter.class.php | 1 - .../AbstractCommentResponseImporter.class.php | 1 - .../system/importer/ImportHandler.class.php | 2 +- 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/importer/AbstractAttachmentImporter.class.php diff --git a/wcfsetup/install/files/lib/system/importer/AbstractAttachmentImporter.class.php b/wcfsetup/install/files/lib/system/importer/AbstractAttachmentImporter.class.php new file mode 100644 index 0000000000..fbf65f922d --- /dev/null +++ b/wcfsetup/install/files/lib/system/importer/AbstractAttachmentImporter.class.php @@ -0,0 +1,78 @@ + + * @package com.woltlab.wcf + * @subpackage system.importer + * @category Community Framework + */ +class AbstractAttachmentImporter implements IImporter { + /** + * object type id for attachment + * @var integer + */ + protected $objectTypeID = 0; + + /** + * @see wcf\system\importer\IImporter::import() + */ + public function import($oldID, array $data) { + $fileLocation = $data['fileLocation']; + unset($data['fileLocation']); + + // check file location + if (!@file_exists($fileLocation)) return 0; + + // get file hash + if (empty($data['fileHash'])) $data['fileHash'] = sha1_file($fileLocation); + + // get user id + if ($data['userID']) $data['userID'] = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.user', $data['userID']); + + // save attachment + $action = new AttachmentAction(array(), 'create', array( + 'data' => array_merge($data, array('objectTypeID' => $this->objectTypeID)) + )); + $returnValues = $action->executeAction(); + $attachment = $returnValues['returnValues']; + + // check attachment directory + // and create subdirectory if necessary + $dir = dirname($attachment->getLocation()); + if (!@file_exists($dir)) { + @mkdir($dir, 0777); + } + + // copy file + try { + if (!copy($fileLocation, $attachment->getLocation())) { + throw new SystemException(); + } + + // create thumbnails + if (ATTACHMENT_ENABLE_THUMBNAILS) { + if ($attachment->isImage) { + $action = new AttachmentAction(array($attachment), 'generateThumbnails'); + $action->executeAction(); + } + } + + return $attachment->attachmentID; + } + catch (SystemException $e) { + // copy failed; delete attachment + $editor = new AttachmentEditor($attachment); + $editor->delete(); + } + + return 0; + } +} diff --git a/wcfsetup/install/files/lib/system/importer/AbstractCommentImporter.class.php b/wcfsetup/install/files/lib/system/importer/AbstractCommentImporter.class.php index 07c1e31014..607ece88da 100644 --- a/wcfsetup/install/files/lib/system/importer/AbstractCommentImporter.class.php +++ b/wcfsetup/install/files/lib/system/importer/AbstractCommentImporter.class.php @@ -30,7 +30,6 @@ class AbstractCommentImporter implements IImporter { */ public function import($oldID, array $data) { if ($data['userID']) $data['userID'] = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.user', $data['userID']); - if (!$data['userID']) $data['userID'] = null; $action = new CommentAction(array(), 'create', array( 'data' => array_merge($data, array('objectTypeID' => $this->objectTypeID)) diff --git a/wcfsetup/install/files/lib/system/importer/AbstractCommentResponseImporter.class.php b/wcfsetup/install/files/lib/system/importer/AbstractCommentResponseImporter.class.php index b0d0fe7266..54f6420936 100644 --- a/wcfsetup/install/files/lib/system/importer/AbstractCommentResponseImporter.class.php +++ b/wcfsetup/install/files/lib/system/importer/AbstractCommentResponseImporter.class.php @@ -24,7 +24,6 @@ class AbstractCommentResponseImporter implements IImporter { */ public function import($oldID, array $data) { if ($data['userID']) $data['userID'] = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.user', $data['userID']); - if (!$data['userID']) $data['userID'] = null; $data['commentID'] = ImportHandler::getInstance()->getNewID($this->objectTypeName, $data['commentID']); if (!$data['commentID']) return 0; diff --git a/wcfsetup/install/files/lib/system/importer/ImportHandler.class.php b/wcfsetup/install/files/lib/system/importer/ImportHandler.class.php index e461484950..8cd75a4059 100644 --- a/wcfsetup/install/files/lib/system/importer/ImportHandler.class.php +++ b/wcfsetup/install/files/lib/system/importer/ImportHandler.class.php @@ -70,7 +70,7 @@ class ImportHandler extends SingletonFactory { $objectTypeID = $this->objectTypes[$type]->objectTypeID; if (!isset($this->idMappingCache[$objectTypeID][$oldID])) { - $this->idMappingCache[$objectTypeID][$oldID] = 0; + $this->idMappingCache[$objectTypeID][$oldID] = null; $sql = "SELECT newID FROM wcf".WCF_N."_import_mapping -- 2.20.1