diff options
author | shadlaws <shad@shadlaws.com> | 2013-02-17 23:19:35 +0100 |
---|---|---|
committer | shadlaws <shad@shadlaws.com> | 2013-02-17 23:19:35 +0100 |
commit | fd0051dab7258c817ff01877bd4324530ce79398 (patch) | |
tree | 6436553d04c789c36cc9eeafe87a35d47bce9098 | |
parent | 87d20c63af02ae573853da2cb82d7323a56677b5 (diff) |
#2006 - Add system::mark_file_for_delete API to delete files at shutdown.
- added system::mark_file_for_delete to be called to mark a file
- added system::delete_marked_files to be called at shutdown to delete the list
- amended system::temp_filename to, by default, add the temp name to the list
- updated a few other places in code where this should be used
-rw-r--r-- | modules/gallery/controllers/quick.php | 1 | ||||
-rw-r--r-- | modules/gallery/controllers/uploader.php | 2 | ||||
-rw-r--r-- | modules/gallery/helpers/gallery_event.php | 4 | ||||
-rw-r--r-- | modules/gallery/helpers/system.php | 32 | ||||
-rw-r--r-- | modules/rest/controllers/rest.php | 2 | ||||
-rw-r--r-- | modules/watermark/controllers/admin_watermarks.php | 6 |
6 files changed, 38 insertions, 9 deletions
diff --git a/modules/gallery/controllers/quick.php b/modules/gallery/controllers/quick.php index 2ddf2a4b..4b21d9ee 100644 --- a/modules/gallery/controllers/quick.php +++ b/modules/gallery/controllers/quick.php @@ -41,7 +41,6 @@ class Quick_Controller extends Controller { gallery_graphics::rotate($item->file_path(), $tmpfile, array("degrees" => $degrees), $item); $item->set_data_file($tmpfile); $item->save(); - unlink($tmpfile); } if (Input::instance()->get("page_type") == "collection") { diff --git a/modules/gallery/controllers/uploader.php b/modules/gallery/controllers/uploader.php index 78437071..8e09dbed 100644 --- a/modules/gallery/controllers/uploader.php +++ b/modules/gallery/controllers/uploader.php @@ -55,7 +55,7 @@ class Uploader_Controller extends Controller { if ($form->validate() && $file_validation->validate()) { $temp_filename = upload::save("Filedata"); - Event::add("system.shutdown", create_function("", "unlink(\"$temp_filename\");")); + system::delete_later($temp_filename); try { $item = ORM::factory("item"); $item->name = substr(basename($temp_filename), 10); // Skip unique identifier Kohana adds diff --git a/modules/gallery/helpers/gallery_event.php b/modules/gallery/helpers/gallery_event.php index 26432ef5..a319b9c6 100644 --- a/modules/gallery/helpers/gallery_event.php +++ b/modules/gallery/helpers/gallery_event.php @@ -44,7 +44,7 @@ class gallery_event_Core { // var/tmp might be stickier because theoretically we could wind up spamming that // dir with a lot of files. But let's start with this and refine as we go. if (!(rand() % 500)) { - // Note that this code is roughly duplicated in gallery_event::gallery_shutdown + // Note that this code is roughly duplicated in gallery_task::file_cleanup $threshold = time() - 1209600; // older than 2 weeks foreach(array("logs", "tmp") as $dir) { $dir = VARPATH . $dir; @@ -67,6 +67,8 @@ class gallery_event_Core { } } } + // Delete all files marked using system::delete_later. + system::delete_marked_files(); } static function user_deleted($user) { diff --git a/modules/gallery/helpers/system.php b/modules/gallery/helpers/system.php index e1398103..7d56466e 100644 --- a/modules/gallery/helpers/system.php +++ b/modules/gallery/helpers/system.php @@ -18,6 +18,8 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class system_Core { + private static $files_marked_for_deletion = array(); + /** * Return the path to an executable version of the named binary, or null. * The paths are traversed in the following order: @@ -66,8 +68,10 @@ class system_Core { * This helper is similar to the built-in tempnam. * It allows the caller to specify a prefix and an extension. * It always places the file in TMPPATH. + * Unless specified with the $delete_later argument, it will be marked + * for deletion at shutdown using system::delete_later. */ - static function temp_filename($prefix="", $extension="") { + static function temp_filename($prefix="", $extension="", $delete_later=true) { do { $basename = tempnam(TMPPATH, $prefix); if (!$basename) { @@ -79,6 +83,30 @@ class system_Core { @unlink($basename); } } while (!$success); + + if ($delete_later) { + system::delete_later($filename); + } + return $filename; } -}
\ No newline at end of file + + /** + * Mark a file for deletion at shutdown time. This is useful for temp files, where we can delay + * the deletion time until shutdown to keep page load time quick. + */ + static function delete_later($filename) { + self::$files_marked_for_deletion[] = $filename; + } + + /** + * Delete all files marked using system::delete_later. This is called at gallery shutdown. + */ + static function delete_marked_files() { + foreach (self::$files_marked_for_deletion as $filename) { + // We want to suppress all errors, as it's possible that some of these + // files may have been deleted/moved before we got here. + @unlink($filename); + } + } +} diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php index bd03b334..54ca6fe9 100644 --- a/modules/rest/controllers/rest.php +++ b/modules/rest/controllers/rest.php @@ -69,7 +69,7 @@ class Rest_Controller extends Controller { $request->params = (object) $input->post(); if (isset($_FILES["file"])) { $request->file = upload::save("file"); - Event::add("system.shutdown", create_function("", "unlink(\"{$request->file}\");")); + system::delete_later($request->file); } break; } diff --git a/modules/watermark/controllers/admin_watermarks.php b/modules/watermark/controllers/admin_watermarks.php index b058d6a5..222279e8 100644 --- a/modules/watermark/controllers/admin_watermarks.php +++ b/modules/watermark/controllers/admin_watermarks.php @@ -67,7 +67,7 @@ class Admin_Watermarks_Controller extends Admin_Controller { $form = watermark::get_delete_form(); if ($form->validate()) { if ($name = basename(module::get_var("watermark", "name"))) { - @unlink(VARPATH . "modules/watermark/$name"); + system::delete_later(VARPATH . "modules/watermark/$name"); module::clear_var("watermark", "name"); module::clear_var("watermark", "width"); @@ -108,7 +108,7 @@ class Admin_Watermarks_Controller extends Admin_Controller { $name = legal_file::sanitize_filename($name, $extension, "photo"); } catch (Exception $e) { message::error(t("Invalid or unidentifiable image file")); - @unlink($file); + system::delete_later($file); return; } @@ -120,7 +120,7 @@ class Admin_Watermarks_Controller extends Admin_Controller { module::set_var("watermark", "position", $form->add_watermark->position->value); module::set_var("watermark", "transparency", $form->add_watermark->transparency->value); $this->_update_graphics_rules(); - @unlink($file); + system::delete_later($file); message::success(t("Watermark saved")); log::success("watermark", t("Watermark saved")); |