| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #!/usr/local/bin/php
- <?php
- # Debug
- $DEBUG = false;
- if ($_ENV['DEBUG']) {
- $DEBUG = true;
- }
- # Includes
- require_once 'opendrive.php';
- # Command-line parameters
- global $argc;
- global $argv;
- $BYTES = 1 * 1024 * 1024 * 1024;
- if (($argc > 1) && (strlen($argv[1]) > 0)) {
- $BYTES = intval($argv[1]) * 1024 * 1024;
- }
- # Run serially
- $PID_FILE = sys_get_temp_dir() . '/upload.pid';
- if (file_exists($PID_FILE)) {
- $OLD_PID = trim(@file_get_contents($PID_FILE));
- if ($OLD_PID && posix_kill($OLD_PID, 0)) {
- # Already running
- if ($DEBUG) {
- echo 'Already running: ' . $OLD_PID . "\n";
- }
- exit(0);
- }
- }
- file_put_contents($PID_FILE, posix_getpid());
- unset($PID_FILE);
- # Open the DB connection
- $dbh = dbOpen();
- # Open the OpenDrive session
- $session = login();
- if (!$session) {
- die("Unable to connect to OpenDrive\n");
- }
- # Prepare statements
- $folders = $dbh->prepare('SELECT base, path FROM files WHERE ' .
- "type = 'folder' AND " .
- '(remote_mtime IS NULL OR remote_mtime < mtime) ' .
- 'ORDER BY base, path');
- $files = $dbh->prepare('SELECT base, path FROM files WHERE ' .
- "(type != 'ignored' AND type != 'folder') AND " .
- '(remote_mtime IS NULL OR remote_mtime < mtime) ' .
- 'ORDER BY priority DESC, path'
- );
- $update = $dbh->prepare('UPDATE files SET remote_mtime = now() WHERE base = :base AND path = :path');
- # Create all the folders
- if ($DEBUG) {
- echo "Creating folders\n";
- }
- $folders->execute();
- while ($row = $folders->fetch()) {
- # Ensure the local reference is valid
- $file = $row['base'] . '/' . $row['path'];
- if (!is_dir($file)) {
- echo 'Invalid directory: ' . $file . "\n";
- continue;
- }
- # Create the folder
- if ($DEBUG) {
- echo 'Creating folder: ' . $row['path'] . "\n";
- }
- if (folderID($session, $row['path']) || mkFolder($session, $row['path'])) {
- $update->execute(array(':base' => $row['base'], ':path' => $row['path']));
- } else {
- echo 'Unable to create folder: ' . $row['path'] . "\n";
- }
- }
- # Loop through all the files
- if ($DEBUG) {
- echo 'Uploading files with byte limit: ' . $BYTES . "\n";
- }
- $uploaded = 0;
- $files->execute();
- while ($row = $files->fetch()) {
- # Ensure the local reference is valid
- $file = $row['base'] . '/' . $row['path'];
- if (!is_readable($file)) {
- echo 'Invalid file: ' . $file . "\n";
- continue;
- }
- $stat = @stat($file);
- if ($stat === false) {
- echo 'Unable to stat: ' . $file . "\n";
- continue;
- }
- # Delete the file if it already exists
- if (fileID($session, $row['path'])) {
- echo 'Replacing file: ' . $row['path'] . "\n";
- rmFile($session, $row['path']);
- }
- # Upload the file
- if ($DEBUG) {
- echo 'Uploading file (' . $stat['size'] . ' bytes): ' . $row['path'] . "\n";
- }
- $result = fileUpload($session, $row['path'], $file);
- if ($result !== false && $result == $stat['size']) {
- $uploaded += $result;
- $update->execute(array(':base' => $row['base'], ':path' => $row['path']));
- } else {
- echo 'Unable to upload file: ' . $row['path'] . "\n";
- }
- # Bail if we exceed the byte limit
- if ($DEBUG) {
- echo 'Uploaded ' . $uploaded . ' of ' . $BYTES . " bytes\n";
- }
- if ($BYTES && $uploaded >= $BYTES) {
- if ($DEBUG) {
- echo "Exiting due to byte limit\n";
- }
- break;
- }
- }
- # Logout from OpenDrive
- logout($session);
- ?>
|