public function scanUploadFolder($strPath = null, $pid = null)
{
if ($strPath === null) {
$strPath = \Config::get('uploadPath');
}
$arrMeta = array();
$arrMapper = array();
$arrFolders = array();
$arrFiles = array();
$arrScan = scan(TL_ROOT . '/' . $strPath);
foreach ($arrScan as $strFile) {
if (strncmp($strFile, '.', 1) === 0) {
continue;
}
if (is_dir(TL_ROOT . '/' . $strPath . '/' . $strFile)) {
$arrFolders[] = $strPath . '/' . $strFile;
} else {
$arrFiles[] = $strPath . '/' . $strFile;
}
}
// Folders
foreach ($arrFolders as $strFolder) {
$objFolder = new \Folder($strFolder);
$strUuid = $this->Database->getUuid();
$this->Database->prepare("INSERT INTO tl_files (pid, tstamp, uuid, name, type, path, hash) VALUES (?, ?, ?, ?, 'folder', ?, ?)")->execute($pid, time(), $strUuid, basename($strFolder), $strFolder, $objFolder->hash);
$this->scanUploadFolder($strFolder, $strUuid);
}
// Files
foreach ($arrFiles as $strFile) {
$matches = array();
// Handle meta.txt files
if (preg_match('/^meta(_([a-z]{2}))?\\.txt$/', basename($strFile), $matches)) {
$key = $matches[2] ?: 'en';
$arrData = file(TL_ROOT . '/' . $strFile, FILE_IGNORE_NEW_LINES);
foreach ($arrData as $line) {
list($name, $info) = explode('=', $line, 2);
list($title, $link, $caption) = explode('|', $info);
$arrMeta[trim($name)][$key] = array('title' => trim($title), 'link' => trim($link), 'caption' => trim($caption));
}
}
$objFile = new \File($strFile);
$strUuid = $this->Database->getUuid();
$this->Database->prepare("INSERT INTO tl_files (pid, tstamp, uuid, name, type, path, extension, hash) VALUES (?, ?, ?, ?, 'file', ?, ?, ?)")->execute($pid, time(), $strUuid, basename($strFile), $strFile, $objFile->extension, $objFile->hash);
$arrMapper[basename($strFile)] = $strUuid;
}
// Insert the meta data AFTER the file entries have been created
if (!empty($arrMeta)) {
foreach ($arrMeta as $file => $meta) {
if (isset($arrMapper[$file])) {
$this->Database->prepare("UPDATE tl_files SET meta=? WHERE uuid=?")->execute(serialize($meta), $arrMapper[$file]);
}
}
}
}