Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-upload files if they've changed since the last upload #3

Merged
merged 1 commit into from Jul 19, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 37 additions & 14 deletions scp-cloudfiles.sh.php
@@ -1,4 +1,4 @@
#!/usr/bin/php -q #!/usr/bin/php
<?php <?php


/** /**
Expand All @@ -15,7 +15,7 @@


// initialize // initialize
set_time_limit(0); set_time_limit(0);
ini_set('register_globals', 'on'); // ini_set('register_globals', 'on');
error_reporting(E_ALL & ~E_NOTICE); error_reporting(E_ALL & ~E_NOTICE);
require_once dirname(__FILE__) . '/php-cloudfiles/cloudfiles.php'; require_once dirname(__FILE__) . '/php-cloudfiles/cloudfiles.php';


Expand Down Expand Up @@ -47,7 +47,7 @@
*/ */
function out($s = '', $lb = TRUE) { function out($s = '', $lb = TRUE) {
echo $s . ($lb? "\n" : ''); echo $s . ($lb? "\n" : '');
ob_flush(); flush();
} }


# Authenticate to Cloud Files. The default is to automatically try # Authenticate to Cloud Files. The default is to automatically try
Expand Down Expand Up @@ -104,13 +104,26 @@ function out($s = '', $lb = TRUE) {
else { else {
$file = $_path; $file = $_path;
$object_name = ltrim(str_replace($path, '', $file), '/'); $object_name = ltrim(str_replace($path, '', $file), '/');

$exists = false;
$changed = false;


try { try {
$exists = $container->get_object($object_name); $object = $container->get_object($object_name);
out(sprintf('File "%s" exists',$object_name)); // var_dump($object);
$exists = true;

// check if it's the same file or it's been changed.
// a better solution would be to hash the file (eg: md5)
// however I'm unaware if rackspace provides you with
// hashes of the objects, and this works fine for me now.
if ($object->content_length!=filesize($file) || strtotime($object->last_modified)<filemtime($file)) {
$changed = true;
}
} catch (Exception $e) { } catch (Exception $e) {
//out(sprintf('File "%s" DOES NOT exist ... ',$object_name), FALSE); $exists = false;
}

if (!$exists || $changed) {
out(sprintf('Uploading file "%s"...', $object_name), FALSE); out(sprintf('Uploading file "%s"...', $object_name), FALSE);
try { try {
$object = $container->create_object($object_name); $object = $container->create_object($object_name);
Expand All @@ -123,14 +136,24 @@ function out($s = '', $lb = TRUE) {
out('object->load_from_filename $file Exception: '.$e); out('object->load_from_filename $file Exception: '.$e);
} }
out('Done.'); out('Done.');
} } // end uploading a file

else {
out(sprintf('File "%s" exists and unchanged',$object_name));
}


unset($object); unset($object);
}
} } // end handling of a file

} // end looping through files in this directory

closedir($dh); closedir($dh);
}
} } // end successfully opening the directory for reading
}
} // end while there are still directories left to scan

} // end check for directory


exit(0); exit(0);