From ce6162603ec08565f9ef9ff406e321b4bae2f038 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Wed, 11 Apr 2012 12:13:28 +0200 Subject: Add filesize to history view Signed-off-by: Florian Pritz --- application/config/migration.php | 2 +- application/controllers/file.php | 33 +++++++++++++++++++++- application/migrations/004_add_filesize.php | 22 +++++++++++++++ application/models/file_mod.php | 7 +++-- application/views/file/upload_history.php | 2 ++ .../views/file_plaintext/upload_history.php | 6 ++-- 6 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 application/migrations/004_add_filesize.php diff --git a/application/config/migration.php b/application/config/migration.php index 3e824520a..0bd1d7c44 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -21,7 +21,7 @@ $config['migration_enabled'] = true; | be upgraded / downgraded to. | */ -$config['migration_version'] = 3; +$config['migration_version'] = 4; /* diff --git a/application/controllers/file.php b/application/controllers/file.php index e5e984b21..b8b22f12b 100644 --- a/application/controllers/file.php +++ b/application/controllers/file.php @@ -127,7 +127,7 @@ class File extends CI_Controller { if (! $cached = $this->memcachelibrary->get("history_".$this->var->view_dir."_".$user)) { $query = array(); $lengths = array(); - $fields = array("id", "filename", "mimetype", "date", "hash"); + $fields = array("id", "filename", "mimetype", "date", "hash", "filesize"); $this->data['title'] .= ' - Upload history'; foreach($fields as $length_key) { $lengths[$length_key] = 0; @@ -142,6 +142,7 @@ class File extends CI_Controller { foreach($query as $key => $item) { $query[$key]["date"] = date("r", $item["date"]); + $query[$key]["filesize"] = format_bytes($item["filesize"]); if ($this->var->cli_client) { // Keep track of longest string to pad plaintext output correctly foreach($fields as $length_key) { @@ -343,6 +344,36 @@ class File extends CI_Controller { } closedir($outer_dh); } + + function update_file_sizes() + { + if (!$this->input->is_cli_request()) return; + + $chunk = 500; + + $total = $this->db->count_all("files"); + + for ($limit = 0; $limit < $total; $limit += $chunk) { + $query = $this->db->query(" + SELECT hash + FROM files + GROUP BY hash + LIMIT $limit, $chunk + ")->result_array(); + + foreach ($query as $key => $item) { + $hash = $item["hash"]; + $filesize = intval(filesize($this->file_mod->file($hash))); + $this->db->query(" + UPDATE files + SET filesize = ? + WHERE hash = ? + ", array($filesize, $hash)); + } + } + + + } } # vim: set noet: diff --git a/application/migrations/004_add_filesize.php b/application/migrations/004_add_filesize.php new file mode 100644 index 000000000..d7a70223d --- /dev/null +++ b/application/migrations/004_add_filesize.php @@ -0,0 +1,22 @@ +db->query(" + ALTER TABLE `files` + ADD `filesize` INT UNSIGNED NOT NULL + "); + } + + public function down() + { + $this->db->query(" + ALTER TABLE `files` + DROP `filesize` + "); + + } +} diff --git a/application/models/file_mod.php b/application/models/file_mod.php index b6194a39e..ffd031ace 100644 --- a/application/models/file_mod.php +++ b/application/models/file_mod.php @@ -88,10 +88,11 @@ class File_mod extends CI_Model { $userid = $this->muser->get_userid(); $mimetype = exec("perl ".FCPATH.'scripts/mimetype '.escapeshellarg($filename).' '.escapeshellarg($this->file($hash))); + $filesize = filesize($this->file($hash)); $query = $this->db->query(' - INSERT INTO `files` (`hash`, `id`, `filename`, `user`, `date`, `mimetype`) - VALUES (?, ?, ?, ?, ?, ?)', - array($hash, $id, $filename, $userid, time(), $mimetype)); + INSERT INTO `files` (`hash`, `id`, `filename`, `user`, `date`, `mimetype`, `filesize`) + VALUES (?, ?, ?, ?, ?, ?, ?)', + array($hash, $id, $filename, $userid, time(), $mimetype, $filesize)); } function show_url($id, $mode) diff --git a/application/views/file/upload_history.php b/application/views/file/upload_history.php index 1dcaa8053..8f0f8e3d7 100644 --- a/application/views/file/upload_history.php +++ b/application/views/file/upload_history.php @@ -6,6 +6,7 @@ Mimetype Date Hash + Size $item): ?> @@ -16,6 +17,7 @@ + diff --git a/application/views/file_plaintext/upload_history.php b/application/views/file_plaintext/upload_history.php index e03311464..4e9308d91 100644 --- a/application/views/file_plaintext/upload_history.php +++ b/application/views/file_plaintext/upload_history.php @@ -4,7 +4,8 @@ echo .mb_str_pad("Filename", $lengths["filename"])." | " .mb_str_pad("Mimetype", $lengths["mimetype"])." | " .mb_str_pad("Date", $lengths["date"])." | " - .mb_str_pad("Hash", $lengths["hash"])."\n"; + .mb_str_pad("Hash", $lengths["hash"])." | " + .mb_str_pad("Size", $lengths["filesize"])."\n"; foreach($query as $key => $item) { echo @@ -12,6 +13,7 @@ foreach($query as $key => $item) { .mb_str_pad($item["filename"], $lengths["filename"])." | " .mb_str_pad($item["mimetype"], $lengths["mimetype"])." | " .$item["date"]." | " - .$item["hash"]."\n"; + .$item["hash"]." | " + .$item["filesize"]."\n"; } -- cgit v1.2.3-24-g4f1b