9
9
use Chamilo \CoreBundle \Component \Utils \CreateUploadedFile ;
10
10
use Chamilo \CoreBundle \Entity \AbstractResource ;
11
11
use Chamilo \CoreBundle \Entity \Course ;
12
+ use Chamilo \CoreBundle \Entity \ResourceFile ;
12
13
use Chamilo \CoreBundle \Entity \ResourceLink ;
13
14
use Chamilo \CoreBundle \Entity \ResourceNode ;
14
15
use Chamilo \CoreBundle \Entity \ResourceRight ;
27
28
use Symfony \Component \HttpFoundation \Request ;
28
29
use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
29
30
use Symfony \Component \HttpKernel \KernelInterface ;
31
+ use Symfony \Contracts \Translation \TranslatorInterface ;
30
32
use ZipArchive ;
31
33
32
34
class BaseResourceFileAction
@@ -189,7 +191,8 @@ public function handleCreateFileRequest(
189
191
ResourceRepository $ resourceRepository ,
190
192
Request $ request ,
191
193
EntityManager $ em ,
192
- string $ fileExistsOption = ''
194
+ string $ fileExistsOption = '' ,
195
+ TranslatorInterface $ translator = null
193
196
): array {
194
197
$ contentData = $ request ->getContent ();
195
198
@@ -253,11 +256,23 @@ public function handleCreateFileRequest(
253
256
// Check if a document with the same title and parent resource node already exists
254
257
$ existingDocument = $ resourceRepository ->findByTitleAndParentResourceNode ($ title , $ parentResourceNodeId );
255
258
if ($ existingDocument ) {
256
- if ('overwrite ' == $ fileExistsOption ) {
257
- // Perform actions when file exists and 'overwrite' option is selected
258
- $ resource ->setResourceName ($ title );
259
+ if ('overwrite ' === $ fileExistsOption ) {
259
260
$ existingDocument ->setTitle ($ title );
260
261
$ existingDocument ->setComment ($ comment );
262
+
263
+ $ resourceNode = $ existingDocument ->getResourceNode ();
264
+
265
+ $ resourceFile = $ resourceNode ->getFirstResourceFile ();
266
+ if ($ resourceFile instanceof ResourceFile) {
267
+ $ resourceFile ->setFile ($ uploadedFile );
268
+ $ em ->persist ($ resourceFile );
269
+ } else {
270
+ $ existingDocument ->setUploadFile ($ uploadedFile );
271
+ }
272
+
273
+ $ resourceNode ->setUpdatedAt (new \DateTime ());
274
+ $ existingDocument ->setResourceNode ($ resourceNode );
275
+
261
276
$ em ->persist ($ existingDocument );
262
277
$ em ->flush ();
263
278
@@ -282,7 +297,7 @@ public function handleCreateFileRequest(
282
297
283
298
// Return any data you need for further processing
284
299
return [
285
- 'title ' => $ title ,
300
+ 'title ' => $ newTitle ,
286
301
'filetype ' => 'file ' ,
287
302
'comment ' => $ comment ,
288
303
];
@@ -294,13 +309,10 @@ public function handleCreateFileRequest(
294
309
// or perform any other desired actions based on your application's requirements
295
310
$ resource ->setResourceName ($ title );
296
311
$ flashBag = $ request ->getSession ()->getFlashBag ();
297
- $ flashBag ->add ('warning ' , 'Upload Already Exists ' );
312
+ $ message = $ translator ? $ translator ->trans ('upload.already_exists ' ) : 'Upload Already Exists ' ;
313
+ $ flashBag ->add ('warning ' , $ message );
298
314
299
- return [
300
- 'title ' => $ title ,
301
- 'filetype ' => 'file ' ,
302
- 'comment ' => $ comment ,
303
- ];
315
+ throw new BadRequestHttpException ($ translator ? $ translator ->trans ('file.already_exists ' ) : 'The file already exists and was not uploaded. ' );
304
316
}
305
317
306
318
throw new InvalidArgumentException ('Invalid fileExistsOption ' );
@@ -614,8 +626,15 @@ private function formatFolderStructure(array $folderStructure): array
614
626
return $ result ;
615
627
}
616
628
629
+ /**
630
+ * Generates a unique filename by appending a random suffix.
631
+ */
617
632
private function generateUniqueTitle (string $ title ): string
618
633
{
619
- return $ title .'_ ' .uniqid ();
634
+ $ info = pathinfo ($ title );
635
+ $ filename = $ info ['filename ' ];
636
+ $ extension = isset ($ info ['extension ' ]) ? '. ' .$ info ['extension ' ] : '' ;
637
+
638
+ return $ filename .'_ ' .uniqid ().$ extension ;
620
639
}
621
640
}
0 commit comments