Skip to content
This repository has been archived by the owner on Apr 5, 2018. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Olde Hampsink committed May 17, 2016
1 parent 26789b1 commit 4c6c575
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Changelog
=================
###0.8.31###
- Make sure we have a unique filename to prevent conflict
- Cache opening of file to prevent a download on every step

###0.8.30###
- Added the ability to upload the import file to an asset source for better persistency
- Save user id to settings so we can run the task without user session
Expand Down
2 changes: 1 addition & 1 deletion ImportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getName()
*/
public function getVersion()
{
return '0.8.30';
return '0.8.31';
}

/**
Expand Down
19 changes: 6 additions & 13 deletions controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,19 @@ public function actionUpload()
// 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);

// 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($path, $folderId, $file->getName(), true);
$response = $source->insertFileByPath($filePath, $folderId, $fileName, true);

// Prevent sensitive information leak. Just in case.
$response->deleteDataItem('filePath');
Expand Down
68 changes: 38 additions & 30 deletions services/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class ImportService extends BaseApplicationComponent
*/
private $_loadedOptionPaths = false;

/**
* Cache import file data
*
* @var array
*/
private $_data = array();

/**
* Read CSV columns.
*
Expand Down Expand Up @@ -539,50 +546,51 @@ public function slugify($slug)
*/
protected function _open($file)
{
$data = array();
if(!count($this->_data)) {

// Turn asset into a file
$asset = craft()->assets->getFileById($file);
$source = $asset->getSource();
$sourceType = $source->getSourceType();
$file = $sourceType->getLocalCopy($asset);
// Turn asset into a file
$asset = craft()->assets->getFileById($file);
$source = $asset->getSource();
$sourceType = $source->getSourceType();
$file = $sourceType->getLocalCopy($asset);

// Check if file exists in the first place
if (file_exists($file)) {
// Check if file exists in the first place
if (file_exists($file)) {

// Automatically detect line endings
@ini_set('auto_detect_line_endings', true);
// Automatically detect line endings
@ini_set('auto_detect_line_endings', true);

// Open file into rows
$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
// Open file into rows
$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// Detect delimiter from first row
$delimiters = array();
$delimiters[ImportModel::DelimiterSemicolon] = substr_count($lines[0], ImportModel::DelimiterSemicolon);
$delimiters[ImportModel::DelimiterComma] = substr_count($lines[0], ImportModel::DelimiterComma);
$delimiters[ImportModel::DelimiterPipe] = substr_count($lines[0], ImportModel::DelimiterPipe);
// Detect delimiter from first row
$delimiters = array();
$delimiters[ImportModel::DelimiterSemicolon] = substr_count($lines[0], ImportModel::DelimiterSemicolon);
$delimiters[ImportModel::DelimiterComma] = substr_count($lines[0], ImportModel::DelimiterComma);
$delimiters[ImportModel::DelimiterPipe] = substr_count($lines[0], ImportModel::DelimiterPipe);

// Sort by delimiter with most occurences
arsort($delimiters, SORT_NUMERIC);
// Sort by delimiter with most occurences
arsort($delimiters, SORT_NUMERIC);

// Give me the keys
$delimiters = array_keys($delimiters);
// Give me the keys
$delimiters = array_keys($delimiters);

// Use first key -> this is the one with most occurences
$delimiter = array_shift($delimiters);
// Use first key -> this is the one with most occurences
$delimiter = array_shift($delimiters);

// Open file and parse csv rows
$handle = fopen($file, 'r');
while (($row = fgetcsv($handle, 0, $delimiter)) !== false) {
// Open file and parse csv rows
$handle = fopen($file, 'r');
while (($row = fgetcsv($handle, 0, $delimiter)) !== false) {

// Add row to data array
$data[] = $row;
// Add row to data array
$this->_data[] = $row;
}
fclose($handle);
}
fclose($handle);
}

// Return data array
return $data;
return $this->_data;
}

/**
Expand Down

0 comments on commit 4c6c575

Please sign in to comment.