From e01630006051bd91641d11f132846f7e0d845da1 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Mon, 4 Jul 2016 07:51:19 +0200 Subject: WIP: Add preview of text pastes to history - Performance needs to be improved by putting the preview text into the database instead of fetching it on each call. Signed-off-by: Florian Pritz --- application/controllers/api/v2/file.php | 3 +++ application/service/files.php | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/application/controllers/api/v2/file.php b/application/controllers/api/v2/file.php index 3d4103f1c..34e472d74 100644 --- a/application/controllers/api/v2/file.php +++ b/application/controllers/api/v2/file.php @@ -60,6 +60,9 @@ class file extends \controllers\api\api_controller { { $this->CI->muser->require_access("apikey"); $history = \service\files::history($this->CI->muser->get_userid()); + foreach ($history['items'] as $key => $item) { + unset($history['items'][$key]['preview_text']); + } foreach ($history['multipaste_items'] as $key => $item) { foreach ($item['items'] as $inner_key => $item) { unset($history['multipaste_items'][$key]['items'][$inner_key]['sort_order']); diff --git a/application/service/files.php b/application/service/files.php index a98e0873f..a0db41b1f 100644 --- a/application/service/files.php +++ b/application/service/files.php @@ -26,6 +26,29 @@ class files { if (\libraries\Image::type_supported($item["mimetype"])) { $item['thumbnail'] = site_url("file/thumbnail/".$item['id']); } + + // FIXME performance is baaaaad here. put this in the db and populate when uploading + $filedata = $CI->mfile->get_filedata($item['id']); + $file = $CI->mfile->file($filedata["data_id"]); + $pygments = new \libraries\Pygments($file, $item['mimetype'], $item['filename']); + if ($pygments->should_highlight()) { + $max_preview_len = 1024; + $max_lines = 15; + + try { + $f = fopen($file, "r"); + } catch (\ErrorException $e) { + $f = null; + } + + if ($f) { + $text = fread($f, $max_preview_len); + fclose($f); + + $item['preview_text'] = self::ellipsize($text, $max_lines, filesize($file)); + } + } + $items[$item["id"]] = $item; } -- cgit v1.2.3-24-g4f1b