public function submitFilesAPI() { $data = file_get_contents("php://input"); $dataArr = (array) json_decode($data); if (empty($dataArr)) { ZDebug::my_echo('Error: Json format error!'); //header('HTTP/1.1 500 Json format error'); $this->submitFileHelper(); exit; } // $dataArr['folderFullName'] cannot be null if (!isset($dataArr['folderFullName']) || empty($dataArr['folderFullName'])) { ZDebug::my_echo('Error: folderFullName cannot be null!'); $this->submitFileHelper(); exit; } if (!isset($dataArr['files']) || count($dataArr['files']) == 0) { ZDebug::my_echo('Error: no file in the submit!'); $this->submitFileHelper(); exit; } // Build folder and sub folders in file root if not exist mkdirInFileRoot($dataArr['folderFullName']); // Adding files foreach ($dataArr['files'] as $fileItem) { $myFile['url'] = $fileItem->url; $myFile = array_merge($myFile, (array) json_decode($fileItem->fields)); // Get folder real full name - absolute path $folderABSName = FILE_ROOT . DIRECTORY_SEPARATOR . $dataArr['folderFullName']; // Download file to folder $fileFullName = $this->downloadFile($myFile['url'], $folderABSName); if (!$fileFullName) { ZDebug::my_echo('Download failed! URL: ' . $myFile['url']); continue; } // Import this file to DB import('SyncFiles'); SyncFiles::importFiles($dataArr['folderFullName']); // Update the file info with data in json import('dao.File'); $file = new File(); $fileToSave = $file->getFile($fileFullName); if ($fileToSave) { $fileToSave['page_title'] = $myFile['Text']; $fileToSave['page_meta'] = $fileItem->fields; $file->saveFile($fileToSave); } else { ZDebug::my_echo('Import to DB failed! ' . $fileFullName); } // Build thumbnails here? //$myThumb = imageCache::cacheImage($fileFullName, 160, 120); //if(!$myThumb) ZDebug::my_echo ('error build thumbnail for ' . $fileFullName); ZDebug::my_print($myFile, 'Adding file'); } ZDebug::my_echo('Success to add ' . count($dataArr['files']) . ' files to folder: ' . $dataArr['folderFullName']); exit; }
public function deleteFolder($id) { if (!isId($id)) { ZDebug::my_echo('Param error in deleteFolder!'); return FALSE; } $res = DB::$dbInstance->query("DELETE FROM folders WHERE fd_id = " . $id . ""); $res = DB::$dbInstance->query("DELETE FROM folders WHERE parent_id = " . $id . ""); $res = DB::$dbInstance->query("DELETE FROM files WHERE fd_id = " . $id . ""); return $res; }
public static function syncFolders($folderToSync) { $debug = self::$debug; $mode = self::$mode; import('dao.Folder'); $folder = new Folder(); import('dao.File'); $file = new File(); $folderFullName = $folderToSync['path']; $folderId = $folderToSync['fd_id']; // If folder is locked, ignore. Unless set mode greater than 0. if ($folderToSync['locked'] && $mode == 0) { if ($debug) { ZDebug::my_echo('Ignore locked folder: ' . $folderFullName . '(' . $folderId . ')'); } return TRUE; } // Step 1: If folder is not physically exist, set to deleted in DB. if (!is_dir($folderFullName)) { $folder->deleteFolder($folderId); if ($debug) { ZDebug::my_echo('Delete folder in DB: ' . $folderFullName . '(' . $folderId . ')'); } return TRUE; } // Step 2: Get the result set of files under this folder $filesInFolder = $folder->getFilesInFolder($folderId); $fileNameArr = array(); foreach ($filesInFolder as $theFile) { // Step 3: If a file is not physically exist, delete it in table files. if (!file_exists($theFile['path'])) { $file->deleteFile($theFile['fid']); if ($debug) { ZDebug::my_echo('Delete file in DB: ' . $theFile['path'] . '(' . $theFile['fid'] . ')'); } } elseif ($theFile['last_modified'] != my_filemtime($theFile['path'])) { $theFile['last_modified'] = my_filemtime($theFile['path']); $file->saveFile($theFile); if ($debug) { ZDebug::my_echo('Update file in DB: ' . $theFile['path'] . '(' . $theFile['fid'] . ')'); } } elseif ($mode > 1) { $file->saveFile($theFile); if ($debug) { ZDebug::my_echo('Update file in DB: ' . $theFile['path'] . '(' . $theFile['fid'] . ')'); } } else { } $fileNameArr[] = $theFile['name']; } // Step 5: Get result set of sub-folders under this folder $subFoldera = $folder->getSubFolder($folderId); $folderNameArr = array(); foreach ($subFoldera as $theFolder) { // Step 6: If a folder is not physically exist, set deleted flag in table folders. if (!file_exists($theFolder['path'])) { $folder->deleteFolder($theFolder['fd_id']); if ($debug) { ZDebug::my_echo('Delete folder in DB: ' . $theFolder['path'] . '(' . $theFolder['fd_id'] . ')'); } } elseif ($theFolder['last_modified'] != my_filemtime($theFolder['path'])) { $theFolder['last_modified'] = my_filemtime($theFolder['path']); $folder->saveFolder($theFolder); if ($debug) { ZDebug::my_echo('Update folder in DB: ' . $theFolder['path'] . '(' . $theFolder['fd_id'] . ')'); } self::syncFolders($theFolder); } else { self::syncFolders($theFolder); } $folderNameArr[] = $theFolder['name']; } $hdl = opendir($folderFullName); while ($item = readdir($hdl)) { $itemFullName = $folderFullName . DIRECTORY_SEPARATOR . $item; // Step 8: If physical file is not in DB file result set, then add a file if ($item != "." && $item != ".." && is_file($itemFullName) && !in_array($item, $fileNameArr)) { $file->saveFile(array('path' => $itemFullName, 'fd_id' => $folderId)); if ($debug > 1) { ZDebug::my_echo('Adding new file in DB: ' . $itemFullName); } } // Step 9: if physical folder is not in DB folder result set, then add a folder if ($item != "." && $item != ".." && is_dir($itemFullName) && !in_array($item, $folderNameArr)) { self::addFolder($itemFullName, $folderId); } } closedir($hdl); }
public function start() { ZDebug::my_echo("I'm pageNotFound controller, I'm gonna call model and view."); }
} elseif ($_SERVER['argc'] == 3) { $start_fid = trim($_SERVER['argv'][1]); $end_fid = trim($_SERVER['argv'][2]); } else { show_error(); } function show_error() { global $enclose; echo "Wrong Parameter! " . $enclose; echo "Usage via command line: " . $enclose; echo " ---- buildThumbnails.php <start_fid> <end_fid>" . $enclose; echo "Usage via browser: " . $enclose; echo " ---- http://foo.com/buildThumbnails.php?start_fid=123&end_fid=12345" . $enclose; exit; } include_once '../bootstrap.inc'; set_time_limit(0); $files = array(); $files = DB::$dbInstance->getRows('SELECT * FROM `files` where image_type>0 and fid>' . $start_fid . ' and fid<' . $end_fid); foreach ($files as $file) { $fileFullName = $file['path']; $myThumb = imageCache::cacheImage($fileFullName, 160, 120, TRUE); if (!$myThumb) { ZDebug::my_echo('error build thumbnail for ' . $fileFullName); } $myThumb = imageCache::cacheImage($fileFullName, 120, 90, TRUE); if (!$myThumb) { ZDebug::my_echo('error build thumbnail for ' . $fileFullName); } }