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;
 }
Beispiel #2
0
//else echo 'a = ' . $a . ", is NOT a interger.\n";
//exit;
$enclose = "\n";
if (isset($_SERVER['HTTP_USER_AGENT'])) {
    $enclose = "<br>\n";
    $folderStr = isset($_REQUEST['folder']) ? $_REQUEST['folder'] : show_error();
    $mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : 0;
    $debug = isset($_REQUEST['debug']) ? $_REQUEST['debug'] : 0;
} elseif ($_SERVER['argc'] >= 2) {
    $folderStr = trim($_SERVER['argv'][1]);
    $mode = isset($_SERVER['argv']['2']) ? trim($_SERVER['argv']['2']) : 0;
    $debug = isset($_SERVER['argv']['3']) ? trim($_SERVER['argv']['3']) : 0;
} else {
    show_error();
}
function show_error()
{
    global $enclose;
    echo "Wrong Parameter! " . $enclose;
    echo "Usage via command line: " . $enclose;
    echo "  ---- importFiles.php <folerId/folderFullPath> <mode> <debug>" . $enclose;
    echo "Usage via browser: " . $enclose;
    echo "  ---- http://foo.com/importFiles.php?folder=123&mode=0&debug=0" . $enclose;
    echo "Note: mode is optional" . $enclose;
    exit;
}
//////////////////////////////////////////
include_once '../bootstrap.inc';
import('SyncFiles');
SyncFiles::importFiles($folderStr, $mode, $debug);