/** * Upload file and process it for mapping. */ public function actionUpload() { // Get import post $import = craft()->request->getRequiredPost('import'); // Get file $file = \CUploadedFile::getInstanceByName('file'); // Is file valid? if (!is_null($file)) { // Determine folder $folder = craft()->path->getStoragePath() . 'import/'; // Ensure folder exists IOHelper::ensureFolderExists($folder); // Get filepath - save in storage folder $path = $folder . $file->getName(); // Save file to Craft's temp folder for later use $file->saveAs($path); // Put vars in model $model = new ImportModel(); $model->filetype = $file->getType(); // Validate filetype if ($model->validate()) { // Get columns $columns = craft()->import->columns($path); // Send variables to template and display $this->renderTemplate('import/_map', array('import' => $import, 'file' => $path, 'columns' => $columns)); } else { // Not validated, show error craft()->userSession->setError(Craft::t('This filetype is not valid') . ': ' . $model->filetype); } } else { // No file uploaded craft()->userSession->setError(Craft::t('Please upload a file.')); } }
/** * Upload file and process it for mapping. */ public function actionUpload() { // Get import post $import = craft()->request->getRequiredPost('import'); // Get file $file = \CUploadedFile::getInstanceByName('file'); // Is file valid? if (!is_null($file)) { // Is asset source valid? if (isset($import['assetsource']) && !empty($import['assetsource'])) { // Get source $source = craft()->assetSources->getSourceTypeById($import['assetsource']); // Get folder to save to $folderId = craft()->assets->getRootFolderBySourceId($import['assetsource']); // Save file to Craft's temp folder for later use $fileName = AssetsHelper::cleanAssetName($file->name); $filePath = AssetsHelper::getTempFilePath($file->extensionName); $file->saveAs($filePath); // Move the file by source type implementation $response = $source->insertFileByPath($filePath, $folderId, $fileName, true); // Prevent sensitive information leak. Just in case. $response->deleteDataItem('filePath'); // Get file id $fileId = $response->getDataItem('fileId'); // Put vars in model $model = new ImportModel(); $model->filetype = $file->getType(); // Validate filetype if ($model->validate()) { // Get columns $columns = craft()->import->columns($fileId); // Send variables to template and display $this->renderTemplate('import/_map', array('import' => $import, 'file' => $fileId, 'columns' => $columns)); } else { // Not validated, show error craft()->userSession->setError(Craft::t('This filetype is not valid') . ': ' . $model->filetype); } } else { // No asset source selected craft()->userSession->setError(Craft::t('Please select an asset source.')); } } else { // No file uploaded craft()->userSession->setError(Craft::t('Please upload a file.')); } }