summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2018-05-03 21:25:49 +0200
committerFlorian Pritz <bluewind@xinu.at>2018-05-03 21:27:31 +0200
commitdd9351c023f57c011cf72bb113c9003ae5aeb2c5 (patch)
treef2016ab6578e1a3a947a7fb07edbf308de150746
parent55c421addac2f814e1a41cc729288276d306f03f (diff)
Move cleanup code from controller to \service\files
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r--application/controllers/Main.php98
-rw-r--r--application/service/files.php98
2 files changed, 101 insertions, 95 deletions
diff --git a/application/controllers/Main.php b/application/controllers/Main.php
index 94d025678..c7bfb2f73 100644
--- a/application/controllers/Main.php
+++ b/application/controllers/Main.php
@@ -869,106 +869,14 @@ class Main extends MY_Controller {
}
}
- private function clean_multipaste_tarballs()
- {
- $tarball_dir = $this->config->item("upload_path")."/special/multipaste-tarballs";
- if (is_dir($tarball_dir)) {
- $tarball_cache_time = $this->config->item("tarball_cache_time");
- $it = new RecursiveIteratorIterator(
- new RecursiveDirectoryIterator($tarball_dir), RecursiveIteratorIterator::SELF_FIRST);
-
- foreach ($it as $file) {
- if ($file->isFile()) {
- if ($file->getMTime() < time() - $tarball_cache_time) {
- $lock = fopen($file, "r+");
- flock($lock, LOCK_EX);
- unlink($file);
- flock($lock, LOCK_UN);
- }
- }
- }
- }
- }
-
/* remove files without database entries */
function clean_stale_files()
{
$this->_require_cli_request();
- $this->remove_files_missing_in_db();
- $this->remove_files_missing_on_disk();
- $this->clean_multipaste_tarballs();
- }
-
- private function remove_files_missing_in_db()
- {
- $upload_path = $this->config->item("upload_path");
- $outer_dh = opendir($upload_path);
-
- while (($dir = readdir($outer_dh)) !== false) {
- if (!is_dir($upload_path."/".$dir) || $dir == ".." || $dir == "." || $dir == "special") {
- continue;
- }
-
- $dh = opendir($upload_path."/".$dir);
-
- $empty = true;
-
- while (($file = readdir($dh)) !== false) {
- if ($file == ".." || $file == ".") {
- continue;
- }
-
- try {
- list($hash, $storage_id) = explode("-", $file);
- } catch (\ErrorException $e) {
- unlink($upload_path."/".$dir."/".$file);
- continue;
- }
-
- $query = $this->db->select('hash, id')
- ->from('file_storage')
- ->where('hash', $hash)
- ->where('id', $storage_id)
- ->limit(1)
- ->get()->row_array();
-
- if (empty($query)) {
- $this->mfile->delete_data_id($file);
- } else {
- $empty = false;
- }
- }
-
- closedir($dh);
-
- if ($empty && file_exists($upload_path."/".$dir)) {
- rmdir($upload_path."/".$dir);
- }
- }
- closedir($outer_dh);
- }
-
- private function remove_files_missing_on_disk()
- {
- $chunk = 500;
- $total = $this->db->count_all("file_storage");
-
- for ($limit = 0; $limit < $total; $limit += $chunk) {
- $query = $this->db->select('hash, id')
- ->from('file_storage')
- ->limit($chunk, $limit)
- ->get()->result_array();
-
- foreach ($query as $key => $item) {
- $data_id = $item["hash"].'-'.$item['id'];
- $file = $this->mfile->file($data_id);
-
- if (!$this->mfile->file_exists($file)) {
- $this->mfile->delete_data_id($data_id);
- }
- }
- }
+ \service\files::remove_files_missing_in_db();
+ \service\files::remove_files_missing_on_disk();
+ \service\files::clean_multipaste_tarballs();
}
function nuke_id()
diff --git a/application/service/files.php b/application/service/files.php
index 85efdfff8..062d17bf8 100644
--- a/application/service/files.php
+++ b/application/service/files.php
@@ -450,4 +450,102 @@ class files {
return $tooltip;
}
+ static public function clean_multipaste_tarballs()
+ {
+ $CI =& get_instance();
+
+ $tarball_dir = $CI->config->item("upload_path")."/special/multipaste-tarballs";
+ if (is_dir($tarball_dir)) {
+ $tarball_cache_time = $CI->config->item("tarball_cache_time");
+ $it = new RecursiveIteratorIterator(
+ new RecursiveDirectoryIterator($tarball_dir), RecursiveIteratorIterator::SELF_FIRST);
+
+ foreach ($it as $file) {
+ if ($file->isFile()) {
+ if ($file->getMTime() < time() - $tarball_cache_time) {
+ $lock = fopen($file, "r+");
+ flock($lock, LOCK_EX);
+ unlink($file);
+ flock($lock, LOCK_UN);
+ }
+ }
+ }
+ }
+ }
+
+ static public function remove_files_missing_in_db()
+ {
+ $CI =& get_instance();
+
+ $upload_path = $CI->config->item("upload_path");
+ $outer_dh = opendir($upload_path);
+
+ while (($dir = readdir($outer_dh)) !== false) {
+ if (!is_dir($upload_path."/".$dir) || $dir == ".." || $dir == "." || $dir == "special") {
+ continue;
+ }
+
+ $dh = opendir($upload_path."/".$dir);
+
+ $empty = true;
+
+ while (($file = readdir($dh)) !== false) {
+ if ($file == ".." || $file == ".") {
+ continue;
+ }
+
+ try {
+ list($hash, $storage_id) = explode("-", $file);
+ } catch (\ErrorException $e) {
+ unlink($upload_path."/".$dir."/".$file);
+ continue;
+ }
+
+ $query = $CI->db->select('hash, id')
+ ->from('file_storage')
+ ->where('hash', $hash)
+ ->where('id', $storage_id)
+ ->limit(1)
+ ->get()->row_array();
+
+ if (empty($query)) {
+ $CI->mfile->delete_data_id($file);
+ } else {
+ $empty = false;
+ }
+ }
+
+ closedir($dh);
+
+ if ($empty && file_exists($upload_path."/".$dir)) {
+ rmdir($upload_path."/".$dir);
+ }
+ }
+ closedir($outer_dh);
+ }
+
+ static public function remove_files_missing_on_disk()
+ {
+ $CI =& get_instance();
+
+ $chunk = 500;
+ $total = $CI->db->count_all("file_storage");
+
+ for ($limit = 0; $limit < $total; $limit += $chunk) {
+ $query = $CI->db->select('hash, id')
+ ->from('file_storage')
+ ->limit($chunk, $limit)
+ ->get()->result_array();
+
+ foreach ($query as $key => $item) {
+ $data_id = $item["hash"].'-'.$item['id'];
+ $file = $CI->mfile->file($data_id);
+
+ if (!$CI->mfile->file_exists($file)) {
+ $CI->mfile->delete_data_id($data_id);
+ }
+ }
+ }
+ }
+
}