From c81f6d3058d733e7f5657e0947627924baebf2d0 Mon Sep 17 00:00:00 2001
From: VladimirCherkasov <vcherckasov@yandex.ru>
Date: Thu, 9 Jul 2015 20:37:12 +0300
Subject: [PATCH] add Controller_Support_Image to receive support images

---
 ~dev_rating/application/bootstrap.php         |  7 ++++
 .../classes/Controller/Environment/Admin.php  |  2 +-
 .../classes/Controller/Support/Image.php      | 39 +++++++++++++++++++
 .../application/classes/SupportImageSaver.php | 11 ++++--
 4 files changed, 54 insertions(+), 5 deletions(-)
 create mode 100644 ~dev_rating/application/classes/Controller/Support/Image.php

diff --git a/~dev_rating/application/bootstrap.php b/~dev_rating/application/bootstrap.php
index 19dcd3ba3..86da4ae13 100644
--- a/~dev_rating/application/bootstrap.php
+++ b/~dev_rating/application/bootstrap.php
@@ -275,6 +275,13 @@ Route::set('admin:common', 'admin(/<controller>(/<action>(/<param1>(:<param2>)))
         'action'     => 'index'
     ]);
 
+Route::set('support:image', 'support/image(/<action>)/<id>')//, ['id' => '[0-9]+'])
+    ->defaults([
+        'directory'  => 'Support',
+        'controller' => 'Image',
+        'action'     => 'view'
+    ]);
+
 // --------------- Java session provider --------------
 Route::set('javaSessionProvider', 'java_authentication')
     ->defaults([
diff --git a/~dev_rating/application/classes/Controller/Environment/Admin.php b/~dev_rating/application/classes/Controller/Environment/Admin.php
index 627ae08c9..82da3feb6 100644
--- a/~dev_rating/application/classes/Controller/Environment/Admin.php
+++ b/~dev_rating/application/classes/Controller/Environment/Admin.php
@@ -10,4 +10,4 @@ class Controller_Environment_Admin extends Controller_Environment_User
 
         $this->user->checkAccess(User::RIGHTS_ADMIN);
     }
-}
\ No newline at end of file
+}
diff --git a/~dev_rating/application/classes/Controller/Support/Image.php b/~dev_rating/application/classes/Controller/Support/Image.php
new file mode 100644
index 000000000..bd4740463
--- /dev/null
+++ b/~dev_rating/application/classes/Controller/Support/Image.php
@@ -0,0 +1,39 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+class Controller_Support_Image extends Controller_Environment_Admin
+{
+    public function before() {
+        parent::before();
+    }
+
+    public function after() {
+
+    }
+
+    public function action_view() {
+        $filename = DOCROOT . SupportImageSaver::directoryToSave .
+            $this->request->param('id') . SupportImageSaver::imageExtension;
+        $this->responseImage($filename);
+    }
+
+    public function action_preview() {
+        $filename = DOCROOT . SupportImageSaver::directoryToSave .
+            SupportImageSaver::thumbPref . $this->request->param('id') . SupportImageSaver::imageExtension;
+        $this->responseImage($filename);
+    }
+
+    private function responseImage($filename) {
+
+        if (!file_exists($filename) OR !is_file($filename)) {
+            throw new HTTP_Exception_404('Picture not found');
+        }
+
+        $image = file_get_contents($filename);
+
+        $info = getimagesize($filename);
+        $mime = $info['mime'];
+        $this->response->headers('Content-Type', $mime)
+            ->body($image);
+    }
+
+}
diff --git a/~dev_rating/application/classes/SupportImageSaver.php b/~dev_rating/application/classes/SupportImageSaver.php
index 52dd53489..3fa925fb9 100644
--- a/~dev_rating/application/classes/SupportImageSaver.php
+++ b/~dev_rating/application/classes/SupportImageSaver.php
@@ -3,11 +3,14 @@
 class SupportImageSaver
 {
     const savingStatusOk = true;
+    const directoryToSave = 'support/img/';
+    const imageExtension = '.jpg';
+    const thumbPref = 'thumb_';
 
     private $_rootDir = DOCROOT;
-    private $_subDir = 'support/img/';
-    private $_fileExt = '.jpg';
-    private $_thumbSuf = 'thumb_';
+    private $_subDir = self::directoryToSave;
+    private $_fileExt = self::imageExtension;
+    private $_thumbPref = self::thumbPref;
 
     private $_srcFilePath = null;
     private $_targetFilePath = null;
@@ -23,7 +26,7 @@ class SupportImageSaver
 
         $this->_srcFilePath = $srcFilePath;
         $this->_targetFilePath = $dir . $file;
-        $this->_targetPreviewPath = $dir . $this->_thumbSuf . $file;
+        $this->_targetPreviewPath = $dir . $this->_thumbPref . $file;
     }
 
     public function saveImage() {
-- 
GitLab