Skip to content

Commit d23107c

Browse files
committed
Digital asset media library and upload support
1 parent 41fc0b7 commit d23107c

File tree

2 files changed

+147
-16
lines changed

2 files changed

+147
-16
lines changed

admin/controller/product/digital-asset.php

+147
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
namespace Vvveb\Controller\Product;
2424

25+
use function Vvveb\__;
2526
use Vvveb\Controller\Crud;
27+
use function Vvveb\sanitizeFileName;
2628
use Vvveb\System\Images;
2729

2830
class DigitalAsset extends Crud {
@@ -32,9 +34,154 @@ class DigitalAsset extends Crud {
3234

3335
protected $module = 'product';
3436

37+
function delete() {
38+
$file = sanitizeFileName($this->request->post['file']);
39+
$dir = DIR_STORAGE . 'digital_assets';
40+
41+
header('Content-type: application/json; charset=utf-8');
42+
43+
if ($file && @unlink($dir . DS . $file)) {
44+
$message = ['success' => true, 'message' => __('File deleted!')];
45+
} else {
46+
$message = ['success' => false, 'message' => __('Error deleting file!')];
47+
}
48+
49+
$this->response->setType('json');
50+
$this->response->output($message);
51+
}
52+
53+
function upload() {
54+
$path = sanitizeFileName($this->request->post['mediaPath']);
55+
$file = $this->request->files['file'] ?? [];
56+
$fileName = sanitizeFileName($file['name']);
57+
$path = preg_replace('@^[\\\/]public[\\\/]media|^[\\\/]media|^[\\\/]public@', '', $path);
58+
$extension = strtolower(substr($fileName, strrpos($fileName, '.') + 1));
59+
$success = false;
60+
$return = '';
61+
$message = '';
62+
$dirMedia = DIR_STORAGE . 'digital_assets';
63+
64+
if ($file) {
65+
if ($file['error'] == UPLOAD_ERR_OK) {
66+
$success = true;
67+
} else {
68+
$message = fileUploadErrMessage($file['error']);
69+
}
70+
71+
/*
72+
if (in_array($extension, $this->uploadDenyExtensions)) {
73+
$message = __('File type not allowed!');
74+
$success = false;
75+
}
76+
*/
77+
78+
$origFilename = $fileName;
79+
$i = 1;
80+
81+
if ($success) {
82+
while (file_exists($destination = $dirMedia . $path . DS . $fileName) && ($i++ < 5)) {
83+
$fileName = rand(0, 10000) . '-' . $origFilename;
84+
}
85+
86+
if (move_uploaded_file($file['tmp_name'], $destination)) {
87+
if (isset($this->request->post['onlyFilename'])) {
88+
$return = $fileName;
89+
} else {
90+
$return = $destination;
91+
}
92+
$message = __('File uploaded successfully!');
93+
} else {
94+
$destination = $dirMedia . $path . DS;
95+
$success = false;
96+
97+
if (! is_writable($destination)) {
98+
$message = sprintf(__('%s not writable!'), $destination);
99+
} else {
100+
$message = __('Error moving uploaded file!');
101+
}
102+
}
103+
}
104+
} else {
105+
$message = __('Invalid upload!');
106+
}
107+
108+
$message = ['success' => $success, 'message' => $message, 'file' => $return];
109+
110+
$this->response->setType('json');
111+
$this->response->output($message);
112+
}
113+
114+
function scan() {
115+
$type = $this->request->get['type'] ?? 'public';
116+
$scandir = DIR_STORAGE . 'digital_assets';
117+
118+
if (! $scandir) {
119+
return [];
120+
}
121+
122+
// This function scans the files folder recursively, and builds a large array
123+
$scan = function ($dir) use ($scandir, &$scan) {
124+
$files = [];
125+
126+
// Is there actually such a folder/file?
127+
128+
if (file_exists($dir)) {
129+
$listdir = @scandir($dir);
130+
131+
if ($listdir) {
132+
foreach ($listdir as $f) {
133+
if (! $f || $f[0] == '.' || $f == 'node_modules' || $f == 'vendor') {
134+
continue; // Ignore hidden files
135+
}
136+
137+
if (is_dir($dir . DS . $f)) {
138+
// The path is a folder
139+
140+
$files[] = [
141+
'name' => $f,
142+
'type' => 'folder',
143+
'path' => str_replace($scandir, '', $dir) . DS . $f,
144+
'items' => $scan($dir . DS . $f), // Recursively get the contents of the folder
145+
];
146+
} else {
147+
// It is a file
148+
149+
$files[] = [
150+
'name' => $f,
151+
'type' => 'file',
152+
'path' => str_replace($scandir, '', $dir) . DS . $f,
153+
'size' => filesize($dir . DS . $f), // Gets the size of this file
154+
];
155+
}
156+
}
157+
}
158+
}
159+
160+
return $files;
161+
};
162+
163+
$response = $scan($scandir);
164+
165+
// Output the directory listing as JSON
166+
$this->response->setType('json');
167+
$this->response->output([
168+
'name' => '',
169+
'type' => 'folder',
170+
'path' => '',
171+
'items' => $response,
172+
]);
173+
}
174+
35175
function index() {
36176
parent::index();
37177

178+
$admin_path = \Vvveb\adminPath();
179+
180+
$controllerPath = $admin_path . 'index.php?module=product/digital-asset';
181+
$this->view->scanUrl = "$controllerPath&action=scan";
182+
$this->view->uploadUrl = "$controllerPath&action=upload";
183+
$this->view->deleteUrl = "$controllerPath&action=delete";
184+
38185
if ($this->view->digital_asset) {
39186
$this->view->digital_asset['image'] = $this->view->digital_asset['file'];
40187
$this->view->digital_asset['image_url'] = Images::image($this->view->digital_asset['image'], 'digital_asset', 'thumb');

admin/template/product/product/attribute.tpl

-16
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,6 @@ foreach ($this->product['product_attribute'] as $product_attribute_id => $attrib
1818
}
1919
?>
2020

21-
//product digital_asset
22-
@product-digital_asset = [data-v-product] [data-v-product-digital_asset] [data-v-digital_asset]
23-
@product-digital_asset|deleteAllButFirst
24-
25-
@product-digital_asset|before = <?php
26-
if(isset($this->product['product_to_digital_asset']) && is_array($this->product['product_to_digital_asset']))
27-
foreach ($this->product['product_to_digital_asset'] as $product_digital_asset_id => $digital_asset) {
28-
?>
29-
@product-digital_asset input[data-v-digital_asset-*]|value = $digital_asset['@@__data-v-digital_asset-(*)__@@']
30-
@product-digital_asset [data-v-digital_asset-*] = $digital_asset['@@__data-v-digital_asset-(*)__@@']
31-
32-
@product-digital_asset|after = <?php
33-
}
34-
?>
35-
36-
3721
[data-v-product] [data-v-attributes] [data-v-group]|deleteAllButFirst
3822
[data-v-product] [data-v-attributes] [data-v-attribute]|deleteAllButFirst
3923

0 commit comments

Comments
 (0)