/**
  *
  */
 public function importAttachmentsAsFiles($directory = null)
 {
     // todo(Jake): make $directory use relative to the SS/basedir rather than full filepath
     $this->logFunctionStart(__FUNCTION__);
     if ($directory === null) {
         throw new Exception('Must provide a $directory parameter');
     }
     $directorIsCLI = Director::is_cli();
     $folderMap = array();
     $existingWpIDs = singleton('File')->WordpressIDsMap();
     $fileResolver = new WordpressAttachmentFileResolver($directory, getTempFolder());
     if (!$fileResolver->getFilesRecursive()) {
         $this->log('No files found recursively in ' . $fileResolver->directory);
         $this->logFunctionEnd(__FUNCTION__);
         return;
     }
     $basePath = Director::baseFolder() . DIRECTORY_SEPARATOR;
     $baseAssetPath = $basePath . ASSETS_DIR . DIRECTORY_SEPARATOR;
     $this->setupDefaultDatabaseIfNull();
     $attachments = $this->_db->getAttachments();
     foreach ($attachments as $wpData) {
         $wpID = $wpData['ID'];
         if (isset($existingWpIDs[$wpID])) {
             //$this->log('File (Wordpress ID: '.$wpID.') already imported.');
             continue;
         }
         $wpMeta = $this->_db->attachAndGetAttachmentMeta($wpData);
         if (isset($wpMeta['_wp_attached_file'])) {
             $filepaths = $fileResolver->getFilepathsFromRecord($wpData);
             if (!$filepaths) {
                 $this->log('Unable to find matching file for "' . $wpMeta['_wp_attached_file'] . '"" database entry (Wordpress ID: ' . $wpData['ID'] . ')', 'error');
                 continue;
             }
             // Check each filepath and see what year/month pattern matches the current Wordpress attachment
             $yearMonthFile = $fileResolver->extractYearAndMonth($wpMeta['_wp_attached_file']);
             if (!$yearMonthFile) {
                 throw new Exception('Doubled up basename and unable to determine year/month from _wp_attached_file postmeta.');
             }
             $chosenFilepath = null;
             foreach ($filepaths as $filepath) {
                 $checkYearMonthAgainst = $fileResolver->extractYearAndMonth($filepath);
                 if ($yearMonthFile === $checkYearMonthAgainst) {
                     $chosenFilepath = $filepath;
                 }
             }
             if ($chosenFilepath === null) {
                 $errorString = "\n    - " . implode("\n    - ", $filepaths);
                 $errorString = 'Unable to find EXACT matching file for "' . $wpMeta['_wp_attached_file'] . '"" database entry (Wordpress ID: ' . $wpData['ID'] . ') -- Possible paths were: ' . $errorString;
                 if (!$directorIsCLI) {
                     $errorString = nl2br($errorString);
                 }
                 $this->log($errorString, 'notice');
                 continue;
             }
             // $chosenFilepath = Full filename (ie. C:/wamp/www/MySSSite)
             $relativeFilepath = str_replace($basePath, '', $chosenFilepath);
             if ($relativeFilepath === $chosenFilepath) {
                 throw new Exception('Wordpress assets must be moved underneath your Silverstripe assets folder.');
             }
             // Convert from Windows backslash to *nix forwardslash
             $relativeFilepath = str_replace("\\", '/', $relativeFilepath);
             // Add S3 CDN path to the record
             $cdnFile = '';
             if (isset($wpMeta['amazonS3_info']) && isset($wpMeta['amazonS3_info']['key'])) {
                 $cdnFile = 'Cdn:||' . $wpMeta['amazonS3_info']['key'];
             }
             // Feed record data into the object directly
             $recordData = array('Title' => $wpData['post_title'], 'Created' => $wpData['post_date'], 'LastEdited' => $wpData['post_modified'], 'Content' => $wpData['post_content'], 'Description' => $wpData['post_content'], 'CDNFile' => $cdnFile);
             if ($fileResolver->isFileExtensionImage($relativeFilepath)) {
                 $record = Image::create($recordData);
             } else {
                 $record = File::create($recordData);
             }
             $record->Filename = $relativeFilepath;
             // Determine folder to save to
             $relativeDirectoryInsideAssets = str_replace(array($baseAssetPath, '\\'), array('', '/'), $chosenFilepath);
             $relativeDirectoryInsideAssets = dirname($relativeDirectoryInsideAssets);
             if (!isset($folderMap[$relativeDirectoryInsideAssets])) {
                 // Using this to speed-up the lookup on already found or made directories
                 $folderMap[$relativeDirectoryInsideAssets] = Folder::find_or_make($relativeDirectoryInsideAssets);
             }
             $folder = $folderMap[$relativeDirectoryInsideAssets];
             if (!$folder || !$folder->ID) {
                 throw new Exception('Unable to determine or create Folder at: ' . $relativeDirectoryInsideAssets);
             }
             $record->ParentID = $folder->ID;
             try {
                 $record->WordpressData = $wpData;
                 $record->write();
                 $this->log('Added "' . $record->Title . '" (' . $record->class . ') to #' . $record->ID, 'created');
             } catch (Exception $e) {
                 //Debug::dump($relativeFilepath);
                 //Debug::dump($e->getMessage()); exit;
                 $this->log('Failed to write #' . $record->ID . ' (' . $record->class . ', Wordpress ID: ' . $wpData['ID'] . ') -- ' . $e->getMessage(), 'error');
             }
         }
     }
     $this->logFunctionEnd(__FUNCTION__);
 }