Powerful and easy to use cms to build websites, blogs or ecommerce stores.
HTML backendblogblog-engineblog-platformbloggingcmscontent-managementcontent-management-systemecommerceecommerce-platformno-codepage-builderphpphp-cmsphp7php8self-hostedshopping-cartweb-applicationwebsite-builder
Digital asset media library and upload support
givanzd23107ca73ab
diff --git a/admin/controller/product/digital-asset.php b/admin/controller/product/digital-asset.phpindex 9aa7f03..7c49d06 100644--- a/admin/controller/product/digital-asset.php+++ b/admin/controller/product/digital-asset.php@@ -22,7 +22,9 @@namespace Vvveb\Controller\Product;+use function Vvveb\__;use Vvveb\Controller\Crud;+use function Vvveb\sanitizeFileName;use Vvveb\System\Images;class DigitalAsset extends Crud {@@ -32,9 +34,154 @@ class DigitalAsset extends Crud {protected $module = 'product';+ function delete() {+ $file = sanitizeFileName($this->request->post['file']);+ $dir = DIR_STORAGE . 'digital_assets';++ header('Content-type: application/json; charset=utf-8');++ if ($file && @unlink($dir . DS . $file)) {+ $message = ['success' => true, 'message' => __('File deleted!')];+ } else {+ $message = ['success' => false, 'message' => __('Error deleting file!')];+ }++ $this->response->setType('json');+ $this->response->output($message);+ }++ function upload() {+ $path = sanitizeFileName($this->request->post['mediaPath']);+ $file = $this->request->files['file'] ?? [];+ $fileName = sanitizeFileName($file['name']);+ $path = preg_replace('@^[\\\/]public[\\\/]media|^[\\\/]media|^[\\\/]public@', '', $path);+ $extension = strtolower(substr($fileName, strrpos($fileName, '.') + 1));+ $success = false;+ $return = '';+ $message = '';+ $dirMedia = DIR_STORAGE . 'digital_assets';++ if ($file) {+ if ($file['error'] == UPLOAD_ERR_OK) {+ $success = true;+ } else {+ $message = fileUploadErrMessage($file['error']);+ }++ /*+ if (in_array($extension, $this->uploadDenyExtensions)) {+ $message = __('File type not allowed!');+ $success = false;+ }+ */++ $origFilename = $fileName;+ $i = 1;++ if ($success) {+ while (file_exists($destination = $dirMedia . $path . DS . $fileName) && ($i++ < 5)) {+ $fileName = rand(0, 10000) . '-' . $origFilename;+ }++ if (move_uploaded_file($file['tmp_name'], $destination)) {+ if (isset($this->request->post['onlyFilename'])) {+ $return = $fileName;+ } else {+ $return = $destination;+ }+ $message = __('File uploaded successfully!');+ } else {+ $destination = $dirMedia . $path . DS;+ $success = false;++ if (! is_writable($destination)) {+ $message = sprintf(__('%s not writable!'), $destination);+ } else {+ $message = __('Error moving uploaded file!');+ }+ }+ }+ } else {+ $message = __('Invalid upload!');+ }++ $message = ['success' => $success, 'message' => $message, 'file' => $return];++ $this->response->setType('json');+ $this->response->output($message);+ }++ function scan() {+ $type = $this->request->get['type'] ?? 'public';+ $scandir = DIR_STORAGE . 'digital_assets';++ if (! $scandir) {+ return [];+ }++ // This function scans the files folder recursively, and builds a large array+ $scan = function ($dir) use ($scandir, &$scan) {+ $files = [];++ // Is there actually such a folder/file?++ if (file_exists($dir)) {+ $listdir = @scandir($dir);++ if ($listdir) {+ foreach ($listdir as $f) {+ if (! $f || $f[0] == '.' || $f == 'node_modules' || $f == 'vendor') {+ continue; // Ignore hidden files+ }++ if (is_dir($dir . DS . $f)) {+ // The path is a folder++ $files[] = [+ 'name' => $f,+ 'type' => 'folder',+ 'path' => str_replace($scandir, '', $dir) . DS . $f,+ 'items' => $scan($dir . DS . $f), // Recursively get the contents of the folder+ ];+ } else {+ // It is a file++ $files[] = [+ 'name' => $f,+ 'type' => 'file',+ 'path' => str_replace($scandir, '', $dir) . DS . $f,+ 'size' => filesize($dir . DS . $f), // Gets the size of this file+ ];+ }+ }+ }+ }++ return $files;+ };++ $response = $scan($scandir);++ // Output the directory listing as JSON+ $this->response->setType('json');+ $this->response->output([+ 'name' => '',+ 'type' => 'folder',+ 'path' => '',+ 'items' => $response,+ ]);+ }+function index() {parent::index();+ $admin_path = \Vvveb\adminPath();++ $controllerPath = $admin_path . 'index.php?module=product/digital-asset';+ $this->view->scanUrl = "$controllerPath&action=scan";+ $this->view->uploadUrl = "$controllerPath&action=upload";+ $this->view->deleteUrl = "$controllerPath&action=delete";+if ($this->view->digital_asset) {$this->view->digital_asset['image'] = $this->view->digital_asset['file'];$this->view->digital_asset['image_url'] = Images::image($this->view->digital_asset['image'], 'digital_asset', 'thumb');diff --git a/admin/template/product/product/attribute.tpl b/admin/template/product/product/attribute.tplindex d5ee62f..62930c0 100644--- a/admin/template/product/product/attribute.tpl+++ b/admin/template/product/product/attribute.tpl@@ -18,22 +18,6 @@ foreach ($this->product['product_attribute'] as $product_attribute_id => $attrib}?>-//product digital_asset-@product-digital_asset = [data-v-product] [data-v-product-digital_asset] [data-v-digital_asset]-@product-digital_asset|deleteAllButFirst--@product-digital_asset|before = <?php-if(isset($this->product['product_to_digital_asset']) && is_array($this->product['product_to_digital_asset']))-foreach ($this->product['product_to_digital_asset'] as $product_digital_asset_id => $digital_asset) {-?>- @product-digital_asset input[data-v-digital_asset-*]|value = $digital_asset['@@__data-v-digital_asset-(*)__@@']- @product-digital_asset [data-v-digital_asset-*] = $digital_asset['@@__data-v-digital_asset-(*)__@@']--@product-digital_asset|after = <?php- }-?>--[data-v-product] [data-v-attributes] [data-v-group]|deleteAllButFirst[data-v-product] [data-v-attributes] [data-v-attribute]|deleteAllButFirst