static function item_created($item) { if ($item->is_album()) { return true; } aws_s3::log("Item created - " . $item->id); aws_s3::upload_item($item); }
static function make_album_cover($item, $sync = false) { if (!$sync) { parent::make_album_cover($item); } $parent = $item->parent(); if ($parent->id > 1) { aws_s3::upload_item($parent); } }
static function sync($task) { aws_s3::log("Amazon S3 Re-sync task started.."); batch::start(); $items = ORM::factory("item")->find_all(); $task->set("total_count", count($items)); $task->set("completed", 0); if (!module::get_var("aws_s3", "synced", false)) { aws_s3::log("Emptying contents of bucket"); $task->status = "Emptying contents of bucket"; $task->save(); require_once MODPATH . "aws_s3/lib/s3.php"; $s3 = new S3(module::get_var("aws_s3", "access_key"), module::get_var("aws_s3", "secret_key")); $bucket = module::get_var("aws_s3", "bucket_name"); $resource = aws_s3::get_resource_url(""); $stuff = array_reverse(S3::getBucket($bucket, $resource)); $i = 0; foreach ($stuff as $uri => $item) { $i++; aws_s3::log("Removing " . $uri . " from S3"); S3::deleteObject($bucket, $uri); $task->percent_complete = round(20 * ($i / count($stuff))); $task->save(); } } $task->percent_complete = 20; aws_s3::log("Commencing upload tasks"); $task->state = "Commencing upload..."; $task->save(); $completed = $task->get("completed", 0); $items = ORM::factory("item")->find_all(); foreach ($items as $item) { try { if ($item->id > 1) { aws_s3::upload_item($item, aws_s3::get_upload_flags()); } } catch (Exception $err) { } $completed++; $task->set("completed", $completed); $task->percent_complete = round(80 * ($completed / $task->get("total_count"))) + 20; $task->status = $completed . " of " . $task->get("total_count") . " uploaded."; $task->save(); } $task->percent_complete = 100; $task->state = "success"; $task->done = true; aws_s3::log("Sync task completed successfully"); $task->status = "Sync task completed successfully"; module::set_var("aws_s3", "synced", true); site_status::clear("aws_s3_not_synced"); batch::stop(); $task->save(); }
static function move_item($old_item, $new_item) { self::get_s3(); $old_filename = urldecode($old_item->relative_path()); $new_filename = urldecode($new_item->relative_path()); aws_s3::log("old filename: " . self::get_resource_url("fs/" . $old_filename) . ", " . "new filename: " . self::get_resource_url("fs/" . $new_filename)); //aws_s3::log($old_item->get_aws_s3_meta()); if ($old_item->s3_fullsize_uploaded) { aws_s3::log("Copying fullsize " . $old_filename . " to " . $new_filename); S3::copyObject(module::get_var("aws_s3", "bucket_name"), self::get_resource_url("fs/" . $old_filename), module::get_var("aws_s3", "bucket_name"), self::get_resource_url("fs/" . $new_filename), $new_item->view_1 ? S3::ACL_PUBLIC_READ : S3::ACL_PRIVATE); S3::deleteObject(module::get_var("aws_s3", "bucket_name"), self::get_resource_url("fs/" . $old_filename)); } else { aws_s3::upload_item($new_item, aws_s3::UPLOAD_FULLSIZE); } if ($old_item->s3_resize_uploaded) { aws_s3::log("Copying resized " . $old_filename . " to " . $new_filename); S3::copyObject(module::get_var("aws_s3", "bucket_name"), self::get_resource_url("rs/" . $old_filename), module::get_var("aws_s3", "bucket_name"), self::get_resource_url("rs/" . $new_filename), $new_item->view_1 ? S3::ACL_PUBLIC_READ : S3::ACL_PRIVATE); S3::deleteObject(module::get_var("aws_s3", "bucket_name"), self::get_resource_url("rs/" . $old_filename)); } else { aws_s3::upload_item($new_item, aws_s3::UPLOAD_RESIZE); } if ($old_item->s3_thumb_uploaded) { aws_s3::log("Copying thumbnail " . $old_filename . " to " . $new_filename); S3::copyObject(module::get_var("aws_s3", "bucket_name"), self::get_resource_url("th/" . $old_filename), module::get_var("aws_s3", "bucket_name"), self::get_resource_url("th/" . $new_filename), $new_item->view_1 ? S3::ACL_PUBLIC_READ : S3::ACL_PRIVATE); S3::deleteObject(module::get_var("aws_s3", "bucket_name"), self::get_resource_url("th/" . $old_filename)); } else { aws_s3::upload_item($new_item, aws_s3::UPLOAD_THUMB); } }
static function sync($task) { require_once MODPATH . "aws_s3/lib/s3.php"; $s3 = new S3(module::get_var("aws_s3", "access_key"), module::get_var("aws_s3", "secret_key")); $mode = $task->get("mode", "init"); switch ($mode) { case "init": aws_s3::log("re-sync task started.."); batch::start(); $items = ORM::factory("item")->find_all(); aws_s3::log("items to sync: " . count($items)); $task->set("total_count", count($items)); $task->set("completed", 0); $task->set("mode", "empty"); $task->status = "Emptying contents of bucket"; break; case "empty": // 0 - 10% aws_s3::log("emptying bucket contents (any files that may already exist in the bucket/prefix path)"); $bucket = module::get_var("aws_s3", "bucket_name"); $resource = aws_s3::get_resource_url(""); $stuff = array_reverse(S3::getBucket($bucket, $resource)); foreach ($stuff as $uri => $item) { aws_s3::log("removing: " . $uri); S3::deleteObject($bucket, $uri); } $task->percent_complete = 10; $task->set("mode", "upload"); $task->state = "Commencing upload..."; break; case "upload": // 10 - 100% $completed = $task->get("completed", 0); $items = ORM::factory("item")->find_all(1, $completed); foreach ($items as $item) { if ($item->id > 1) { aws_s3::log("uploading item " . $item->id . " (" . ($completed + 1) . "/" . $task->get("total_count") . ")"); if ($item->is_album()) { aws_s3::upload_album_cover($item); } else { aws_s3::upload_item($item); } } $completed++; } $task->set("completed", $completed); $task->percent_complete = round(90 * ($completed / $task->get("total_count"))) + 10; $task->status = $completed . " of " . $task->get("total_count") . " uploaded."; if ($completed == $task->get("total_count")) { $task->set("mode", "finish"); } break; case "finish": aws_s3::log("completing upload task.."); $task->percent_complete = 100; $task->state = "success"; $task->done = true; $task->status = "Sync task completed successfully"; batch::stop(); module::set_var("aws_s3", "synced", true); site_status::clear("aws_s3_not_synced"); break; } }