22
22
23
23
namespace Vvveb \Controller \Product ;
24
24
25
+ use function Vvveb \__ ;
25
26
use Vvveb \Controller \Crud ;
27
+ use function Vvveb \sanitizeFileName ;
26
28
use Vvveb \System \Images ;
27
29
28
30
class DigitalAsset extends Crud {
@@ -32,9 +34,154 @@ class DigitalAsset extends Crud {
32
34
33
35
protected $ module = 'product ' ;
34
36
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
+
35
175
function index () {
36
176
parent ::index ();
37
177
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
+
38
185
if ($ this ->view ->digital_asset ) {
39
186
$ this ->view ->digital_asset ['image ' ] = $ this ->view ->digital_asset ['file ' ];
40
187
$ this ->view ->digital_asset ['image_url ' ] = Images::image ($ this ->view ->digital_asset ['image ' ], 'digital_asset ' , 'thumb ' );
0 commit comments