]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-231] media урл для показа картинок и карточек
authorAlexander Smirnov <fredeom@mail.ru>
Wed, 6 Nov 2024 14:08:20 +0000 (17:08 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Wed, 6 Nov 2024 14:08:20 +0000 (17:08 +0300)
docker-compose.yml
docker/nginx_media/Dockerfile [new file with mode: 0644]
docker/nginx_media/conf/fastcgi_params [new file with mode: 0644]
docker/nginx_media/conf/mime.types [new file with mode: 0644]
docker/nginx_media/conf/nginx.conf [new file with mode: 0644]
erp24/media/.gitignore [new file with mode: 0644]
erp24/media/config/media.config.php [new file with mode: 0644]
erp24/media/controllers/MediaController.php [new file with mode: 0644]
erp24/media/index.php [new file with mode: 0644]

index 42072072defe1f519a76d0167ee723b4b83ca392..aeef4ad140cab4f00358aa09757daf8901b239e6 100644 (file)
@@ -43,6 +43,17 @@ services:
       - ./docker/nginx_api3/conf:/etc/nginx
     environment:
       TZ: Europe/Moscow
+  nginx_media-yii_erp24:
+    build: ./docker/nginx_media
+    restart: always
+    ports:
+      - "9999:80"
+      - "8443:443"
+    volumes:
+      - ./erp24:/www
+      - ./docker/nginx_media/conf:/etc/nginx
+    environment:
+      TZ: Europe/Moscow
   php-yii_erp24:
     build: ./docker/php
     restart: always
diff --git a/docker/nginx_media/Dockerfile b/docker/nginx_media/Dockerfile
new file mode 100644 (file)
index 0000000..8cda9df
--- /dev/null
@@ -0,0 +1,7 @@
+FROM nginx:1.21.6-alpine
+RUN apk add openssl \
+    && mkdir -p /etc/ssl/private && mkdir -p /etc/ssl/certs \
+    && openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
+    -subj "/CN=media.erp-flowers.ru/C=RU/L=Nizhny Novgorod" \
+    -keyout /etc/ssl/private/nginx.key \
+    -out /etc/ssl/certs/nginx.crt
diff --git a/docker/nginx_media/conf/fastcgi_params b/docker/nginx_media/conf/fastcgi_params
new file mode 100644 (file)
index 0000000..28decb9
--- /dev/null
@@ -0,0 +1,25 @@
+
+fastcgi_param  QUERY_STRING       $query_string;
+fastcgi_param  REQUEST_METHOD     $request_method;
+fastcgi_param  CONTENT_TYPE       $content_type;
+fastcgi_param  CONTENT_LENGTH     $content_length;
+
+fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
+fastcgi_param  REQUEST_URI        $request_uri;
+fastcgi_param  DOCUMENT_URI       $document_uri;
+fastcgi_param  DOCUMENT_ROOT      $document_root;
+fastcgi_param  SERVER_PROTOCOL    $server_protocol;
+fastcgi_param  REQUEST_SCHEME     $scheme;
+fastcgi_param  HTTPS              $https if_not_empty;
+
+fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
+fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
+
+fastcgi_param  REMOTE_ADDR        $remote_addr;
+fastcgi_param  REMOTE_PORT        $remote_port;
+fastcgi_param  SERVER_ADDR        $server_addr;
+fastcgi_param  SERVER_PORT        $server_port;
+fastcgi_param  SERVER_NAME        $server_name;
+
+# PHP only, required if PHP was built with --enable-force-cgi-redirect
+fastcgi_param  REDIRECT_STATUS    200;
diff --git a/docker/nginx_media/conf/mime.types b/docker/nginx_media/conf/mime.types
new file mode 100644 (file)
index 0000000..1c00d70
--- /dev/null
@@ -0,0 +1,99 @@
+
+types {
+    text/html                                        html htm shtml;
+    text/css                                         css;
+    text/xml                                         xml;
+    image/gif                                        gif;
+    image/jpeg                                       jpeg jpg;
+    application/javascript                           js;
+    application/atom+xml                             atom;
+    application/rss+xml                              rss;
+
+    text/mathml                                      mml;
+    text/plain                                       txt;
+    text/vnd.sun.j2me.app-descriptor                 jad;
+    text/vnd.wap.wml                                 wml;
+    text/x-component                                 htc;
+
+    image/avif                                       avif;
+    image/png                                        png;
+    image/svg+xml                                    svg svgz;
+    image/tiff                                       tif tiff;
+    image/vnd.wap.wbmp                               wbmp;
+    image/webp                                       webp;
+    image/x-icon                                     ico;
+    image/x-jng                                      jng;
+    image/x-ms-bmp                                   bmp;
+
+    font/woff                                        woff;
+    font/woff2                                       woff2;
+
+    application/java-archive                         jar war ear;
+    application/json                                 json;
+    application/mac-binhex40                         hqx;
+    application/msword                               doc;
+    application/pdf                                  pdf;
+    application/postscript                           ps eps ai;
+    application/rtf                                  rtf;
+    application/vnd.apple.mpegurl                    m3u8;
+    application/vnd.google-earth.kml+xml             kml;
+    application/vnd.google-earth.kmz                 kmz;
+    application/vnd.ms-excel                         xls;
+    application/vnd.ms-fontobject                    eot;
+    application/vnd.ms-powerpoint                    ppt;
+    application/vnd.oasis.opendocument.graphics      odg;
+    application/vnd.oasis.opendocument.presentation  odp;
+    application/vnd.oasis.opendocument.spreadsheet   ods;
+    application/vnd.oasis.opendocument.text          odt;
+    application/vnd.openxmlformats-officedocument.presentationml.presentation
+                                                     pptx;
+    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
+                                                     xlsx;
+    application/vnd.openxmlformats-officedocument.wordprocessingml.document
+                                                     docx;
+    application/vnd.wap.wmlc                         wmlc;
+    application/wasm                                 wasm;
+    application/x-7z-compressed                      7z;
+    application/x-cocoa                              cco;
+    application/x-java-archive-diff                  jardiff;
+    application/x-java-jnlp-file                     jnlp;
+    application/x-makeself                           run;
+    application/x-perl                               pl pm;
+    application/x-pilot                              prc pdb;
+    application/x-rar-compressed                     rar;
+    application/x-redhat-package-manager             rpm;
+    application/x-sea                                sea;
+    application/x-shockwave-flash                    swf;
+    application/x-stuffit                            sit;
+    application/x-tcl                                tcl tk;
+    application/x-x509-ca-cert                       der pem crt;
+    application/x-xpinstall                          xpi;
+    application/xhtml+xml                            xhtml;
+    application/xspf+xml                             xspf;
+    application/zip                                  zip;
+
+    application/octet-stream                         bin exe dll;
+    application/octet-stream                         deb;
+    application/octet-stream                         dmg;
+    application/octet-stream                         iso img;
+    application/octet-stream                         msi msp msm;
+
+    audio/midi                                       mid midi kar;
+    audio/mpeg                                       mp3;
+    audio/ogg                                        ogg;
+    audio/x-m4a                                      m4a;
+    audio/x-realaudio                                ra;
+
+    video/3gpp                                       3gpp 3gp;
+    video/mp2t                                       ts;
+    video/mp4                                        mp4;
+    video/mpeg                                       mpeg mpg;
+    video/quicktime                                  mov;
+    video/webm                                       webm;
+    video/x-flv                                      flv;
+    video/x-m4v                                      m4v;
+    video/x-mng                                      mng;
+    video/x-ms-asf                                   asx asf;
+    video/x-ms-wmv                                   wmv;
+    video/x-msvideo                                  avi;
+}
diff --git a/docker/nginx_media/conf/nginx.conf b/docker/nginx_media/conf/nginx.conf
new file mode 100644 (file)
index 0000000..2abc34c
--- /dev/null
@@ -0,0 +1,73 @@
+user  nginx;
+worker_processes  auto;
+
+error_log  /proc/self/fd/2 notice;
+pid        /var/run/nginx.pid;
+
+
+events {
+    worker_connections  1024;
+}
+
+
+http {
+    include       /etc/nginx/mime.types;
+    default_type  application/octet-stream;
+
+    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
+                      '$status $body_bytes_sent "$http_referer" '
+                      '"$http_user_agent" "$http_x_forwarded_for"';
+
+    access_log  /proc/self/fd/1  main;
+
+    sendfile        on;
+    #tcp_nopush     on;
+
+    keepalive_timeout  65;
+
+    gzip  on;
+
+    server {
+        listen       80;
+        listen  [::]:80;
+        server_name  api2;
+
+        location ~ /uploads/\.*\.(js|css|jpg|png|ico) {
+          root        /www/uploads;
+        }
+        location / {
+            fastcgi_pass   php-yii_erp24:9000;
+            fastcgi_param  SCRIPT_FILENAME  /www/media/index.php;
+            include        fastcgi_params;
+            fastcgi_read_timeout 300;
+        }
+
+        error_page   500 502 503 504  /50x.html;
+        location = /50x.html {
+            root   /usr/share/nginx/html;
+        }
+    }
+    server {
+        listen 443 ssl;
+        listen [::]:443 ssl;
+
+        ssl_certificate /etc/ssl/certs/nginx.crt;
+        ssl_certificate_key /etc/ssl/private/nginx.key;
+
+        server_name  erp24;
+
+        location ~ /uploads/\.*\.(js|css|jpg|png|ico) {
+            root    /www/uploads;
+        }
+        location / {
+            fastcgi_pass   php-yii_erp24:9000;
+            fastcgi_param  SCRIPT_FILENAME  /www/media/index.php;
+            include        fastcgi_params;
+        }
+
+        error_page   500 502 503 504  /50x.html;
+        location = /50x.html {
+            root   /usr/share/nginx/html;
+        }
+    }
+}
diff --git a/erp24/media/.gitignore b/erp24/media/.gitignore
new file mode 100644 (file)
index 0000000..69f5430
--- /dev/null
@@ -0,0 +1,3 @@
+runtime
+web/assets/*
+json
\ No newline at end of file
diff --git a/erp24/media/config/media.config.php b/erp24/media/config/media.config.php
new file mode 100644 (file)
index 0000000..439d3e5
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+
+return [
+    'language' => 'ru',
+    'id' => 'app',
+    'basePath' => dirname(__DIR__),
+    'bootstrap' => ['log'],
+    'components' => [
+        'assetManager' => [
+            'appendTimestamp' => true,
+            'basePath' => '@webroot/cache/assets',
+            'baseUrl' => '@web/cache/assets',
+        ],
+        'formatter' => [    // fallback if no ext-intl php extension
+            'timeZone' => 'Europe/Moscow',
+            'defaultTimeZone' => 'Europe/Moscow',
+            'dateFormat' => 'php:d.m.Y',
+            'datetimeFormat' => 'php:d.m.Y H:i:s',
+            'timeFormat' => 'php:H:i',
+            'decimalSeparator' => ',',
+            'thousandSeparator' => ',',
+        ],
+        'urlManager' => [
+            'enablePrettyUrl' => true,
+            'enableStrictParsing' => false,
+            'showScriptName' => false,
+            'rules' => [],
+        ],
+        'request' => [
+            'scriptUrl' => '/',
+            'cookieValidationKey' => 'erp24_DLVFJRBvmttertrrt_key',
+            'parsers' => [
+                'application/json' => 'yii\web\JsonParser'
+            ]
+        ],
+        'response' => [
+            'format' => \yii\web\Response::FORMAT_JSON,
+            'charset' => 'UTF-8',
+            'formatters' => [
+                \yii\web\Response::FORMAT_JSON => [
+                    'class' => '\yii\web\JsonResponseFormatter',
+                    'prettyPrint' => YII_DEBUG,
+                    'encodeOptions' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
+                ],
+            ],
+        ],
+        'user' => [
+            'identityClass' => \yii\web\IdentityInterface::class,
+        ],
+        'db' => require __DIR__ . '/../../config/db.php',
+        'log' => [
+            'traceLevel' => 3,
+            'targets' => [
+                [
+                    'class' => \yii\log\FileTarget::class,
+                    'levels' => ['error', 'warning'],
+                    'except' => [
+                        'yii\web\HttpException:405',
+                        'yii\web\HttpException:404',
+                        'yii\web\HttpException:403',
+                        'yii\web\HttpException:401',
+                    ],
+                ]
+            ],
+        ],
+        'cache' => [
+            'class' => \yii\caching\FileCache::class,
+        ],
+    ],
+    'aliases' => [
+        '@bower' => '@vendor/bower-asset',
+        '@npm' => '@vendor/npm-asset',
+        '@dist' => '@app/dist',
+        '@uploads' => "@app/uploads",
+    ],
+    'params' => require dirname(__DIR__, 2) . '/config/params.php',
+    'timeZone' => 'Europe/Moscow',
+];
diff --git a/erp24/media/controllers/MediaController.php b/erp24/media/controllers/MediaController.php
new file mode 100644 (file)
index 0000000..dec38f7
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+namespace app\controllers;
+
+use Yii;
+use yii\filters\AccessControl;
+use yii\rest\Controller;
+use yii\web\NotFoundHttpException;
+use yii_app\api3\core\exceptions\ErrorException;
+use yii_app\helpers\File;
+use yii_app\helpers\ImageHelper;
+use yii_app\records\Files;
+use yii_app\records\Images;
+use yii_app\records\MatrixErp;
+use yii_app\records\MatrixErpProperty;
+use yii_app\services\FileService;
+
+class MediaController extends Controller
+{
+    public function actionTest() {
+        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
+        return ['ok'];
+    }
+
+    public function actionView($id) {
+        Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
+        $file = Files::findOne($id);
+        $filepath = \Yii::getalias('@yii_app') . FileService::padWithSlash($file->url);
+        return \Yii::$app->response->sendFile($filepath, $id, ['inline' => true]);
+    }
+
+    public function actionViewUrl($url) {
+        Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
+        $filepath = \Yii::getalias('@yii_app') . FileService::padWithSlash($url);
+        return \Yii::$app->response->sendFile($filepath, basename($url), ['inline' => true]);
+    }
+
+    public function actionViewCard($guid) {
+        Yii::$app->response->format = \yii\web\Response::FORMAT_HTML;
+        $matrixErp = MatrixErp::findOne(['guid' => $guid]);
+        $matrixErpProperty = MatrixErpProperty::findOne(['guid' => $guid]);
+
+        $image = Images::findOne($matrixErpProperty->image_id);
+        $fileName = '';
+        if ($image && File::src($image->filename, 'images') != null) {
+            $fileName = File::src($image->filename, 'images');
+        }
+
+        return '<!DOCTYPE html><html><head><title>' . $matrixErpProperty->display_name . '</title></head><body>' .
+            '<h1>' . $matrixErpProperty->display_name . '</h1>' .
+            '<p>' . $matrixErpProperty->description . '</p>' .
+            '<img src="/media/view-url?url=' . $fileName . '" alt=img />' .
+            '<div>Цена: ' . ($matrixErp->price->price ?? '0') . '₽</div>' .
+            '</body></html>';
+
+    }
+}
\ No newline at end of file
diff --git a/erp24/media/index.php b/erp24/media/index.php
new file mode 100644 (file)
index 0000000..dc83619
--- /dev/null
@@ -0,0 +1,15 @@
+<?php
+
+defined('YII_DEBUG') or define('YII_DEBUG', false);
+defined('YII_ENV') or define('YII_ENV', 'dev');
+
+require __DIR__ . '/../vendor/autoload.php';
+require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
+
+$config = require __DIR__.'/config/media.config.php';
+
+Yii::setAlias('@yii_app', dirname(__DIR__));
+
+$application = new yii\web\Application($config);
+$exitCode = $application->run();
+exit($exitCode);