Proyectos de Subversion LeadersLinked - Services

Rev

Rev 823 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
283 www 1
<?php
546 ariadna 2
 
283 www 3
namespace LeadersLinked\Library;
4
 
5
use LeadersLinked\Model\User;
6
use LeadersLinked\Model\UserProfile;
7
use LeadersLinked\Model\Company;
8
use LeadersLinked\Model\Group;
9
 
345 www 10
class Storage
283 www 11
{
345 www 12
 
13
    const FILE_TYPE_IMAGE = 'image';
14
 
15
    const FILE_TYPE_VIDEO = 'video';
16
 
17
    const FILE_TYPE_DOCUMENT = 'document';
18
 
19
    const TYPE_DEFAULT = 'default';
20
 
21
    const TYPE_CHAT = 'chat';
22
 
23
    const TYPE_GROUP = 'group';
24
 
25
    const TYPE_USER = 'user';
26
 
27
    const TYPE_IMAGE = 'image';
28
 
29
    const TYPE_JOB = 'job';
30
 
31
    const TYPE_MEDIA = 'media';
32
 
33
    const TYPE_COMPANY = 'company';
34
 
35
    const TYPE_FEED = 'feed';
36
 
37
    const TYPE_POST = 'post';
38
 
39
    const TYPE_MICROLEARNING_TOPIC = 'topic';
40
 
41
    const TYPE_MICROLEARNING_CAPSULE = 'capsule';
42
 
43
    const TYPE_MICROLEARNING_SLIDE = 'slide';
44
 
45
    const TYPE_JOB_DESCRIPTION = 'jobdesc';
46
 
47
    const TYPE_SELF_EVALUATION = 'selfval';
48
 
49
    const TYPE_PERFORMANCE_EVALUATION = 'perfeva';
50
 
51
    const TYPE_RECRUITMENT_SELECTION = 'recrsel';
52
 
53
    const TYPE_PLANNING_OBJECTIVES_AND_GOALS = 'plannig';
54
 
55
    const TYPE_MESSAGE = 'message';
56
 
57
    const TYPE_SURVEY = 'survey';
58
 
59
    const TYPE_NETWORK = 'network';
60
 
61
    const TYPE_DAILY_PULSE = 'dailpuls';
62
 
63
    const TYPE_ENGAGEMENT_REWARD = 'engarewr';
64
 
65
    const TYPE_KNOWLEDGE_AREA = 'knowarea';
66
 
67
    const TYPE_MY_COACH = 'mycoach';
68
 
69
    const TYPE_HABIT_EMOJI = 'habit-emoji';
70
 
71
    const TYPE_HABIT_CONTENT = 'habit-content';
72
 
283 www 73
    /**
345 www 74
     *
75
     * @var\LeadersLinked\Library\Storage
283 www 76
     */
77
    private static $_instance;
345 www 78
 
283 www 79
    /**
345 www 80
     *
283 www 81
     * @var array
82
     */
83
    private $config;
345 www 84
 
283 www 85
    /**
345 www 86
     *
87
     * @var \Laminas\Db\Adapter\AdapterInterface
283 www 88
     */
333 www 89
    private $adapter;
345 www 90
 
91
    /**
92
     *
93
     * @var string
94
     */
95
    private $tempPath;
96
 
97
    /**
98
     *
99
     * @var string
100
     */
101
    private $storagePath;
102
 
103
    /**
104
     *
823 stevensc 105
     * @var string
106
     */
107
    private $fullStoragePath;
108
 
109
    /**
110
     *
345 www 111
     * @var array
112
     */
823 stevensc 113
    private $storageConfig;
114
 
115
    /**
116
     *
117
     * @var array
118
     */
345 www 119
    private $currentFile;
546 ariadna 120
 
121
 
345 www 122
    /**
123
     *
124
     * @var array
125
     */
126
    private $files;
333 www 127
 
283 www 128
    /**
345 www 129
     *
283 www 130
     * @param array $config
333 www 131
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
283 www 132
     */
345 www 133
    private function __construct($config, $adapter)
283 www 134
    {
345 www 135
        $this->config = $config;
136
        $this->adapter = $adapter;
137
        $this->currentFile = [];
138
 
823 stevensc 139
        // Ruta relativa para BD
824 stevensc 140
        $this->storagePath = 'data' . DIRECTORY_SEPARATOR . 'storage';
823 stevensc 141
 
142
        // Ruta completa para operaciones de archivo
824 stevensc 143
        $this->fullStoragePath = $this->config['leaderslinked.storage.path'] . DIRECTORY_SEPARATOR . $this->storagePath;
823 stevensc 144
 
145
        if (!file_exists($this->fullStoragePath)) {
146
            mkdir($this->fullStoragePath, 0775, true);
345 www 147
        }
148
 
824 stevensc 149
        $this->tempPath = $this->storagePath . DIRECTORY_SEPARATOR . 'tmp';
345 www 150
 
816 stevensc 151
        if (! file_exists($this->tempPath)) {
549 stevensc 152
            mkdir($this->tempPath, 0775, true);
345 www 153
        }
283 www 154
    }
345 www 155
 
283 www 156
    /**
345 www 157
     *
283 www 158
     * @param array $config
333 www 159
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
283 www 160
     * @return \LeadersLinked\Library\Storage
161
     */
333 www 162
    public static function getInstance($config, $adapter)
283 www 163
    {
345 www 164
        if (self::$_instance == null) {
333 www 165
            self::$_instance = new Storage($config, $adapter);
283 www 166
        }
345 www 167
 
283 www 168
        return self::$_instance;
169
    }
345 www 170
 
171
    /**
172
     *
173
     * @return string
174
     */
175
    public function getFullPathImageDefault()
176
    {
823 stevensc 177
        return $this->fullStoragePath . DIRECTORY_SEPARATOR . 'no-image.jpg';
345 www 178
    }
179
 
180
    /**
181
     *
182
     * @return string
183
     */
184
    public function getFullPathImageUserDefault()
185
    {
823 stevensc 186
        return $this->fullStoragePath . DIRECTORY_SEPARATOR . 'no-image-user.jpg';
345 www 187
    }
188
 
189
    /**
190
     *
191
     * @return string
192
     */
193
    public function getFullPathImageUserPofileDefault()
194
    {
823 stevensc 195
        return $this->fullStoragePath . DIRECTORY_SEPARATOR . 'no-image-user-profile.png';
345 www 196
    }
197
 
198
    /**
199
     *
200
     * @return string
201
     */
202
    public function getFullPathImageUserCoverDefault()
203
    {
823 stevensc 204
        return $this->fullStoragePath . DIRECTORY_SEPARATOR . 'no-image-cover.jpg';
345 www 205
    }
206
 
207
    /**
208
     *
209
     * @return string
210
     */
211
    public function getFullPathImageCompanyDefault()
212
    {
823 stevensc 213
        return $this->fullStoragePath . DIRECTORY_SEPARATOR . 'no-image-company.jpg';
345 www 214
    }
215
 
216
    /**
217
     *
218
     * @return string
219
     */
220
    public function getFullPathImageCompanyCoverDefault()
221
    {
823 stevensc 222
        return $this->fullStoragePath . DIRECTORY_SEPARATOR . 'no-image-cover.jpg';
345 www 223
    }
224
 
225
    /**
226
     *
227
     * @return string
228
     */
229
    public function getFullPathImageGroupDefault()
230
    {
823 stevensc 231
        return $this->fullStoragePath . DIRECTORY_SEPARATOR . 'no-image-group.jpg';
345 www 232
    }
233
 
234
    /**
235
     *
236
     * @return string
237
     */
238
    public function getFullPathImageGroupCoverDefault()
239
    {
823 stevensc 240
        return $this->fullStoragePath . DIRECTORY_SEPARATOR . 'no-image-cover.jpg';
345 www 241
    }
242
 
243
    /**
244
     *
245
     * @return string
246
     */
247
    public function getFullPathImageCompanyHeaderPdfDefault()
248
    {
823 stevensc 249
        return $this->fullStoragePath . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'company-header.png';
345 www 250
    }
251
 
252
    /**
253
     *
254
     * @return string
255
     */
256
    public function getFullPathImageCompanyFooterPdfDefault()
257
    {
823 stevensc 258
        return $this->fullStoragePath . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'company-footer.png';
345 www 259
    }
260
 
261
    /**
823 stevensc 262
     * Convierte una ruta relativa a ruta completa para operaciones de archivo
263
     * @param string $relativePath
264
     * @return string
265
     */
266
    private function getFullPathFromRelative($relativePath)
267
    {
268
        // Si la ruta ya es absoluta, la devolvemos tal como está
269
        if (strpos($relativePath, $this->fullStoragePath) === 0) {
270
            return $relativePath;
271
        }
272
 
273
        // Reemplazar la ruta base relativa con la ruta completa
274
        return str_replace($this->storagePath, $this->fullStoragePath, $relativePath);
275
    }
276
 
277
    /**
278
     * Convierte una ruta completa a ruta relativa para almacenar en BD
279
     * @param string $fullPath
280
     * @return string
281
     */
282
    private function getRelativePathFromFull($fullPath)
283
    {
284
        // Si ya es una ruta relativa, la devolvemos tal como está
285
        if (strpos($fullPath, $this->fullStoragePath) !== 0) {
286
            return $fullPath;
287
        }
288
 
289
        // Reemplazar la ruta completa con la ruta relativa
290
        return str_replace($this->fullStoragePath, $this->storagePath, $fullPath);
291
    }
292
 
293
    /**
345 www 294
     *
295
     * @return string
296
     */
297
    public function getStoagePath()
298
    {
299
        return $this->storagePath;
300
    }
301
 
302
    /**
823 stevensc 303
     * Obtiene la ruta completa para operaciones de archivo
304
     * @return string
305
     */
306
    public function getFullStoragePath()
307
    {
308
        return $this->fullStoragePath;
309
    }
310
 
311
    /**
345 www 312
     *
313
     * @return string
314
     */
315
    public function getTempPath()
316
    {
317
        return $this->tempPath;
318
    }
319
 
320
    /**
321
     *
322
     * @return array
323
     */
324
    public function getCurrentFile()
325
    {
326
        return $this->currentFile;
327
    }
328
 
329
    /**
330
     *
331
     * @param array $currentFile
332
     */
333
    public function setCurrentFile($currentFile)
334
    {
335
        $this->currentFile = $currentFile;
336
    }
337
 
338
    /**
339
     *
340
     * @param array $filename
341
     */
342
    public function setCurrentFilename($filename)
343
    {
546 ariadna 344
        if (empty($this->files)) {
345 www 345
            if (isset($_FILES[$filename]) && empty($_FILES[$filename]['error'])) {
346
                $this->currentFile = $_FILES[$filename];
347
            } else {
348
                $this->currentFile = [];
349
            }
350
        } else {
351
            if (isset($this->files[$filename]) && empty($this->files[$filename]['error'])) {
352
                $this->currentFile = $this->files[$filename];
353
            } else {
354
                $this->currentFile = [];
355
            }
356
        }
546 ariadna 357
 
345 www 358
        return !empty($this->currentFile);
359
    }
546 ariadna 360
 
283 www 361
    /**
362
     *
345 www 363
     * @return boolean
364
     */
365
    public function hasFile()
366
    {
367
        return !empty($this->currentFile);
368
    }
546 ariadna 369
 
345 www 370
    /**
371
     *
372
     * @param array $files
373
     */
374
    public function setFiles($files)
375
    {
376
        $this->files = $files;
377
    }
378
 
379
    /**
380
     *
381
     * @param string $type
382
     * @param string $code
383
     * @param string $filename
384
     * @return string
385
     */
386
    public function composePathToFilename($type, $code, $filename)
387
    {
388
        $path = $this->getPathByType($type);
389
        $path = $path . DIRECTORY_SEPARATOR . $code;
823 stevensc 390
        if (!file_exists($path)) {
391
            mkdir($path, 0775, true);
345 www 392
        }
393
        return $path . DIRECTORY_SEPARATOR . $filename;
394
    }
395
 
396
    /**
397
     *
398
     * @param string $type
399
     * @param string $code
400
     * @return string
401
     */
402
    public function composePathToDirectory($type, $code)
403
    {
404
        $path = $this->getPathByType($type);
405
        $path = $path . DIRECTORY_SEPARATOR . $code;
406
        if (! file_exists($path)) {
553 stevensc 407
            mkdir($path, 0775, true);
345 www 408
        }
409
        return $path;
410
    }
411
 
412
    /**
553 stevensc 413
     * Private helper to get a presigned URL for an entity's image/cover or its default.
345 www 414
     *
553 stevensc 415
     * @param string $entityType The type of the entity (e.g., self::TYPE_USER).
416
     * @param string $entityUuid The UUID of the entity.
417
     * @param string|null $imageFilename The filename of the image/cover, if it exists.
418
     * @param string $defaultImageMethodName The name of the method in this class to get the full path to the default image.
419
     * @return string The presigned URL.
283 www 420
     */
553 stevensc 421
    private function _getProcessedImageOrCoverUrl($entityType, $entityUuid, $imageFilename, $defaultImageMethodName)
283 www 422
    {
553 stevensc 423
        if ($imageFilename) {
424
            $remoto = $this->composePathToFilename($entityType, $entityUuid, $imageFilename);
345 www 425
            if (file_exists($remoto)) {
333 www 426
                return $this->getPresignedUrl($remoto);
283 www 427
            }
428
        }
553 stevensc 429
        // If imageFilename is null/empty, or the specific file doesn't exist, use the default.
430
        $defaultFullPath = $this->$defaultImageMethodName();
431
        return $this->getPresignedUrl($defaultFullPath);
432
    }
345 www 433
 
553 stevensc 434
    /**
435
     *
436
     * @param User $user
437
     * @return string
438
     */
439
    public function getUserImage($user)
440
    {
441
        return $this->_getProcessedImageOrCoverUrl(self::TYPE_USER, $user->uuid, $user->image, 'getFullPathImageUserDefault');
283 www 442
    }
345 www 443
 
283 www 444
    /**
445
     *
446
     * @param User $user
447
     * @return string
448
     */
449
    public function getUserImageForCodeAndFilename($code, $filename)
450
    {
553 stevensc 451
        return $this->_getProcessedImageOrCoverUrl(self::TYPE_USER, $code, $filename, 'getFullPathImageUserDefault');
283 www 452
    }
345 www 453
 
283 www 454
    /**
455
     *
456
     * @param User $user
457
     * @param UserProfile $userProfile
458
     * @return string
459
     */
460
    public function getUserProfileImage($user, $userProfile)
461
    {
553 stevensc 462
        return $this->_getProcessedImageOrCoverUrl(self::TYPE_USER, $user->uuid, $userProfile->image, 'getFullPathImageUserPofileDefault');
283 www 463
    }
345 www 464
 
283 www 465
    /**
466
     *
467
     * @param User $user
468
     * @param UserProfile $userProfile
469
     * @return string
470
     */
471
    public function getUserProfileCover($user, $userProfile)
472
    {
553 stevensc 473
        return $this->_getProcessedImageOrCoverUrl(self::TYPE_USER, $user->uuid, $userProfile->cover, 'getFullPathImageUserCoverDefault');
283 www 474
    }
345 www 475
 
283 www 476
    /**
477
     *
478
     * @param Company $company
479
     * @return string
480
     */
481
    public function getCompanyImage($company)
482
    {
553 stevensc 483
        return $this->_getProcessedImageOrCoverUrl(self::TYPE_COMPANY, $company->uuid, $company->image, 'getFullPathImageCompanyDefault');
283 www 484
    }
345 www 485
 
283 www 486
    /**
487
     *
488
     * @param string $code
489
     * @param string $filename
490
     * @return string
491
     */
492
    public function getCompanyImageForCodeAndFilename($code, $filename)
493
    {
553 stevensc 494
        return $this->_getProcessedImageOrCoverUrl(self::TYPE_COMPANY, $code, $filename, 'getFullPathImageCompanyDefault');
283 www 495
    }
345 www 496
 
283 www 497
    /**
498
     *
499
     * @param Company $company
500
     * @return string
501
     */
502
    public function getCompanyCover($company)
503
    {
553 stevensc 504
        return $this->_getProcessedImageOrCoverUrl(self::TYPE_COMPANY, $company->uuid, $company->cover, 'getFullPathImageCompanyCoverDefault');
283 www 505
    }
345 www 506
 
283 www 507
    /**
508
     *
509
     * @param Group $group
510
     * @return string
511
     */
512
    public function getGroupImage($group)
513
    {
553 stevensc 514
        return $this->_getProcessedImageOrCoverUrl(self::TYPE_GROUP, $group->uuid, $group->image, 'getFullPathImageGroupDefault');
283 www 515
    }
345 www 516
 
283 www 517
    /**
518
     *
519
     * @param string $code
520
     * @param string $filename
521
     * @return string
522
     */
523
    public function getGroupImageForCodeAndFilename($code, $filename)
524
    {
672 ariadna 525
        return $this->_getProcessedImageOrCoverUrl(self::TYPE_GROUP, $code, $filename, 'getFullPathImageGroupDefault');
283 www 526
    }
345 www 527
 
668 ariadna 528
 
283 www 529
    /**
530
     *
531
     * @param Group $group
532
     * @return string
533
     */
534
    public function getGroupCover($group)
535
    {
672 ariadna 536
        return $this->_getProcessedImageOrCoverUrl(self::TYPE_GROUP, $group->uuid, $group->cover, 'getFullPathImageGroupCoverDefault');
283 www 537
    }
345 www 538
 
668 ariadna 539
    public function getGroupCoverForCodeAndFilename($code, $filename)
540
    {
541
        return $this->_getProcessedImageOrCoverUrl(self::TYPE_COMPANY, $code, $filename, 'getFullPathCoverGroupDefault');
542
    }
543
 
283 www 544
    /**
545
     *
546
     * @param Group $group
547
     * @return string
548
     */
549
    public function getGenericImage($path, $code, $filename)
550
    {
345 www 551
        $remoto = $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;
552
 
553
        if (file_exists($remoto)) {
333 www 554
            return $this->getPresignedUrl($remoto);
283 www 555
        }
345 www 556
 
755 stevensc 557
        $remoto = $path . DIRECTORY_SEPARATOR . $filename;
558
 
559
        if (file_exists($remoto)) {
560
            return $this->getPresignedUrl($remoto);
561
        }
562
 
345 www 563
        $remoto = $this->getFullPathImageDefault();
564
        return $this->getPresignedUrl($remoto);
283 www 565
    }
334 www 566
 
567
    /**
568
     *
569
     * @param string $path
570
     * @param string $code,
571
     * @param string $filename
572
     * @return string
573
     */
345 www 574
    public function getGenericFile($path, $code, $filename)
334 www 575
    {
345 www 576
        $remoto = $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;
546 ariadna 577
 
345 www 578
        if (file_exists($remoto)) {
546 ariadna 579
            if ($code == 'd8e6c5de-52a6-4c28-bdce-4ba2823ba841') {
580
 
345 www 581
                error_log('getGenericFile = ' . $remoto);
582
            }
546 ariadna 583
 
584
 
333 www 585
            return $this->getPresignedUrl($remoto);
586
        }
760 stevensc 587
 
588
        $remoto = $path . DIRECTORY_SEPARATOR . $filename;
589
 
590
        if (file_exists($remoto)) {
591
            return $this->getPresignedUrl($remoto);
592
        }
593
 
594
        $remoto = $this->getFullPathImageDefault();
595
        return $this->getPresignedUrl($remoto);
333 www 596
    }
546 ariadna 597
 
345 www 598
    public function getPathDefault()
333 www 599
    {
345 www 600
        return $this->getPathByType(self::TYPE_DEFAULT);
333 www 601
    }
546 ariadna 602
 
345 www 603
    public function getPathChat()
320 www 604
    {
345 www 605
        return $this->getPathByType(self::TYPE_CHAT);
320 www 606
    }
546 ariadna 607
 
283 www 608
    public function getPathGroup()
609
    {
345 www 610
        return $this->getPathByType(self::TYPE_GROUP);
283 www 611
    }
546 ariadna 612
 
283 www 613
    public function getPathUser()
614
    {
345 www 615
        return $this->getPathByType(self::TYPE_USER);
283 www 616
    }
546 ariadna 617
 
283 www 618
    public function getPathImage()
619
    {
345 www 620
        return $this->getPathByType(self::TYPE_IMAGE);
283 www 621
    }
546 ariadna 622
 
283 www 623
    public function getPathJob()
624
    {
345 www 625
        return $this->getPathByType(self::TYPE_JOB);
283 www 626
    }
546 ariadna 627
 
283 www 628
    public function getPathCompany()
629
    {
345 www 630
        return $this->getPathByType(self::TYPE_COMPANY);
283 www 631
    }
546 ariadna 632
 
633
 
283 www 634
    public function getPathFeed()
635
    {
345 www 636
        return $this->getPathByType(self::TYPE_FEED);
283 www 637
    }
546 ariadna 638
 
345 www 639
    public function getPathMedia()
295 www 640
    {
345 www 641
        return $this->getPathByType(self::TYPE_MEDIA);
295 www 642
    }
546 ariadna 643
 
283 www 644
    public function getPathPost()
645
    {
345 www 646
        return $this->getPathByType(self::TYPE_POST);
283 www 647
    }
546 ariadna 648
 
649
 
283 www 650
    public function getPathMicrolearningTopic()
651
    {
345 www 652
        return $this->getPathByType(self::TYPE_MICROLEARNING_TOPIC);
283 www 653
    }
546 ariadna 654
 
283 www 655
    public function getPathMicrolearningCapsule()
656
    {
345 www 657
        return $this->getPathByType(self::TYPE_MICROLEARNING_CAPSULE);
283 www 658
    }
546 ariadna 659
 
283 www 660
    public function getPathMicrolearningSlide()
661
    {
345 www 662
        return $this->getPathByType(self::TYPE_MICROLEARNING_SLIDE);
283 www 663
    }
546 ariadna 664
 
283 www 665
    public function getPathJobDescription()
666
    {
345 www 667
        return $this->getPathByType(self::TYPE_JOB_DESCRIPTION);
283 www 668
    }
546 ariadna 669
 
295 www 670
    public function getPathSelfEvaluation()
283 www 671
    {
345 www 672
        return $this->getPathByType(self::TYPE_SELF_EVALUATION);
283 www 673
    }
546 ariadna 674
 
345 www 675
    public function getPathPerformanceEvaluation()
676
    {
677
        return $this->getPathByType(self::TYPE_PERFORMANCE_EVALUATION);
678
    }
546 ariadna 679
 
680
 
283 www 681
    public function getPathRecruitmentSelection()
682
    {
345 www 683
        return $this->getPathByType(self::TYPE_RECRUITMENT_SELECTION);
283 www 684
    }
546 ariadna 685
 
686
 
345 www 687
    public function getPathPlanningObjectivesAndGoals()
283 www 688
    {
345 www 689
        return $this->getPathByType(self::TYPE_PLANNING_OBJECTIVES_AND_GOALS);
283 www 690
    }
546 ariadna 691
 
345 www 692
    public function getPathMessage()
283 www 693
    {
345 www 694
        return $this->getPathByType(self::TYPE_MESSAGE);
283 www 695
    }
546 ariadna 696
 
697
 
283 www 698
    public function getPathSurvey()
699
    {
345 www 700
        return $this->getPathByType(self::TYPE_SURVEY);
283 www 701
    }
546 ariadna 702
 
703
 
283 www 704
    public function getPathNetwork()
705
    {
345 www 706
        return $this->getPathByType(self::TYPE_NETWORK);
283 www 707
    }
546 ariadna 708
 
709
 
345 www 710
    public function getPathDailyPulse()
283 www 711
    {
345 www 712
        return $this->getPathByType(self::TYPE_DAILY_PULSE);
283 www 713
    }
345 www 714
 
546 ariadna 715
 
345 www 716
    public function getPathEngagementReward()
283 www 717
    {
345 www 718
        return $this->getPathByType(self::TYPE_ENGAGEMENT_REWARD);
283 www 719
    }
546 ariadna 720
 
283 www 721
    public function getPathKnowledgeArea()
722
    {
345 www 723
        return $this->getPathByType(self::TYPE_KNOWLEDGE_AREA);
283 www 724
    }
546 ariadna 725
 
283 www 726
    public function getPathMyCoach()
727
    {
345 www 728
        return $this->getPathByType(self::TYPE_MY_COACH);
283 www 729
    }
546 ariadna 730
 
345 www 731
    public function getPathHabitEmoji()
283 www 732
    {
345 www 733
        return $this->getPathByType(self::TYPE_HABIT_EMOJI);
283 www 734
    }
345 www 735
 
736
    public function getPathHabitContent()
283 www 737
    {
345 www 738
        return $this->getPathByType(self::TYPE_HABIT_CONTENT);
739
    }
740
 
741
 
742
    public function getPathByType($type)
743
    {
744
        switch ($type) {
745
            case self::TYPE_DEFAULT:
823 stevensc 746
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'default';
345 www 747
                break;
748
 
749
            case self::TYPE_CHAT:
823 stevensc 750
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'chat';
345 www 751
                break;
752
 
753
            case self::TYPE_GROUP:
823 stevensc 754
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'group';
345 www 755
                break;
756
 
757
            case self::TYPE_USER:
823 stevensc 758
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'user';
345 www 759
                break;
760
 
761
            case self::TYPE_IMAGE:
823 stevensc 762
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'image';
345 www 763
                break;
764
 
765
            case self::TYPE_JOB:
823 stevensc 766
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'job';
345 www 767
                break;
768
 
769
            case self::TYPE_COMPANY:
823 stevensc 770
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'company';
345 www 771
                break;
772
 
773
            case self::TYPE_FEED:
823 stevensc 774
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'feed';
345 www 775
                break;
776
 
777
            case self::TYPE_MEDIA:
823 stevensc 778
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'media';
345 www 779
                break;
780
 
781
            case self::TYPE_POST:
823 stevensc 782
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'post';
345 www 783
                break;
784
 
785
            case self::TYPE_MICROLEARNING_TOPIC:
823 stevensc 786
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'topic';
345 www 787
                break;
788
 
789
            case self::TYPE_MICROLEARNING_CAPSULE:
823 stevensc 790
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'capsule';
345 www 791
                break;
792
 
793
            case self::TYPE_MICROLEARNING_SLIDE:
823 stevensc 794
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'slide';
345 www 795
                break;
796
 
797
            case self::TYPE_JOB_DESCRIPTION:
823 stevensc 798
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'job-description';
345 www 799
                break;
800
 
801
            case self::TYPE_SELF_EVALUATION:
823 stevensc 802
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'self-evaluation';
345 www 803
                break;
804
 
805
            case self::TYPE_PERFORMANCE_EVALUATION:
823 stevensc 806
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'performance-evaluation';
345 www 807
                break;
808
 
809
            case self::TYPE_RECRUITMENT_SELECTION:
823 stevensc 810
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'recruitment-selection';
345 www 811
                break;
812
 
813
            case self::TYPE_PLANNING_OBJECTIVES_AND_GOALS:
823 stevensc 814
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'planning-objectives-and-goals';
345 www 815
                break;
816
 
817
            case self::TYPE_MESSAGE:
823 stevensc 818
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'message';
345 www 819
                break;
820
 
821
            case self::TYPE_SURVEY:
823 stevensc 822
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'survey';
345 www 823
                break;
824
 
825
            case self::TYPE_NETWORK:
823 stevensc 826
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'network';
345 www 827
                break;
828
 
829
            case self::TYPE_DAILY_PULSE:
823 stevensc 830
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'daily-pulse';
345 www 831
                break;
832
 
833
            case self::TYPE_ENGAGEMENT_REWARD:
823 stevensc 834
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'engagement-reward';
345 www 835
                break;
836
 
837
            case self::TYPE_KNOWLEDGE_AREA:
823 stevensc 838
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'knowledge-area';
345 www 839
                break;
840
 
841
            case self::TYPE_MY_COACH:
823 stevensc 842
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'my-coach';
345 www 843
                break;
844
 
845
            case self::TYPE_HABIT_EMOJI:
823 stevensc 846
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'habit' . DIRECTORY_SEPARATOR . 'emoji';
345 www 847
                break;
848
 
849
            case self::TYPE_HABIT_CONTENT:
823 stevensc 850
                $path = $this->fullStoragePath . DIRECTORY_SEPARATOR . 'habit' . DIRECTORY_SEPARATOR . 'content';
345 www 851
                break;
852
 
853
            default:
823 stevensc 854
                $path = $this->fullStoragePath;
345 www 855
                break;
334 www 856
        }
345 www 857
 
858
        if (! file_exists($path)) {
859
            mkdir($path, 0775, true);
860
        }
861
 
862
        return $path;
283 www 863
    }
345 www 864
 
283 www 865
    /**
866
     *
867
     * @param string $path
868
     * @param string $code
869
     * @param string $filename
345 www 870
     * @return string
283 www 871
     */
872
    public function deleteFile($path, $code, $filename)
873
    {
345 www 874
        $remoto = $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;
553 stevensc 875
        if (file_exists($remoto)) {
876
            return unlink($remoto);
283 www 877
        } else {
878
            return true;
879
        }
880
    }
345 www 881
 
295 www 882
    /**
883
     *
549 stevensc 884
     * @param string $type
295 www 885
     * @param string $code
886
     * @return boolean
887
     */
549 stevensc 888
    public function deleteDirectory($type, $code)
295 www 889
    {
549 stevensc 890
        $path = $this->getPathByType($type);
891
        $remoto = $path . DIRECTORY_SEPARATOR . $code;
345 www 892
        if (file_exists($remoto)) {
334 www 893
            $this->deleteDirectoryRecursive($remoto);
894
            return true;
295 www 895
        } else {
896
            return true;
897
        }
898
    }
345 www 899
 
334 www 900
    /**
901
     *
902
     * @param string $dir
903
     */
345 www 904
    private function deleteDirectoryRecursive($dir)
905
    {
334 www 906
        if (is_dir($dir)) {
907
            $objects = scandir($dir);
908
            foreach ($objects as $object) {
345 www 909
                if ($object != '.' && $object != '..') {
910
                    $s = $dir . DIRECTORY_SEPARATOR . $object;
911
                    if (is_dir($s) && ! is_link($s)) {
549 stevensc 912
                        $this->deleteDirectoryRecursive($s);
345 www 913
                    } else {
553 stevensc 914
                        unlink($s);
334 www 915
                    }
916
                }
917
            }
553 stevensc 918
            rmdir($dir);
334 www 919
        }
920
    }
283 www 921
 
922
    /**
923
     *
924
     * @param string $path
925
     * @param string $code
290 www 926
     * @param string $local_filename
283 www 927
     * @return boolean
928
     */
345 www 929
    public function putFile($source_file, $target_file)
283 www 930
    {
553 stevensc 931
        return rename($source_file, $target_file);
345 www 932
    }
933
 
934
    /**
935
     *
936
     * @param string $tempfile
937
     * @param string $path
938
     * @param string $code
939
     * @param string $filename
940
     * @return boolean
941
     */
942
    public function moveUploadedFile($source_file, $target_file)
943
    {
553 stevensc 944
        return move_uploaded_file($source_file, $target_file);
345 www 945
    }
946
 
947
    /**
948
     *
346 www 949
     * @param string $tempfile
950
     * @param string $path
951
     * @param string $code
952
     * @param string $filename
953
     * @return boolean
954
     */
955
    public function copyFile($source_file, $target_file)
956
    {
553 stevensc 957
        return copy($source_file, $target_file);
346 www 958
    }
546 ariadna 959
 
960
 
346 www 961
    /**
962
     *
345 www 963
     * @param string $url
964
     * @return string
965
     */
966
    public function getPresignedUrl($url)
967
    {
823 stevensc 968
        // Convertir la ruta completa a ruta relativa para almacenar en BD
969
        $relativePath = $this->getRelativePathFromFull($url);
345 www 970
        $code = hash('sha256', $url);
546 ariadna 971
 
345 www 972
        $storageFileMapper = \LeadersLinked\Mapper\StorageFileMapper::getInstance($this->adapter);
973
        $storageFile = $storageFileMapper->fetchOneByCode($code);
974
        if (! $storageFile) {
975
            $storageFile = new \LeadersLinked\Model\StorageFile();
976
            $storageFile->code = $code;
823 stevensc 977
            // Guardar la ruta relativa en la base de datos
978
            $storageFile->path = $relativePath;
345 www 979
            $storageFile->salt = \LeadersLinked\Library\Functions::generatePassword(8);
980
 
981
            $storageFileMapper->insert($storageFile);
334 www 982
        }
345 www 983
 
984
        $url = 'https://' . $_SERVER['SERVER_NAME'] . '/storage/' . $code;
668 ariadna 985
 
345 www 986
        return $url;
987
    }
290 www 988
 
345 www 989
    /**
823 stevensc 990
     * Obtiene la ruta completa del archivo basado en el código almacenado
991
     * @param string $code
992
     * @return string|null
993
     */
994
    public function getFilePathByCode($code)
995
    {
996
        $storageFileMapper = \LeadersLinked\Mapper\StorageFileMapper::getInstance($this->adapter);
997
        $storageFile = $storageFileMapper->fetchOneByCode($code);
998
 
999
        if ($storageFile && $storageFile->path) {
1000
            // Convertir la ruta relativa almacenada en BD a ruta completa
1001
            return $this->getFullPathFromRelative($storageFile->path);
1002
        }
1003
 
1004
        return null;
1005
    }
1006
 
1007
    /**
1008
     * Verifica si un archivo existe usando el código almacenado
1009
     * @param string $code
1010
     * @return bool
1011
     */
1012
    public function fileExistsByCode($code)
1013
    {
1014
        $filePath = $this->getFilePathByCode($code);
1015
        return $filePath && file_exists($filePath);
1016
    }
1017
 
1018
    /**
345 www 1019
     *
1020
     * @return @return void|string
1021
     */
1022
    public function getFilenamePNG()
1023
    {
1024
        if ($this->currentFile) {
1025
            $filename = $this->normalizeStringFilename($this->currentFile['name']);
1026
            $path_parts = pathinfo($filename);
1027
            return $path_parts['filename'] . '.png';
1028
        } else {
1029
            return;
1030
        }
1031
    }
333 www 1032
 
345 www 1033
    /**
1034
     *
1035
     * @return @return void|string
1036
     */
1037
    public function getTmpFilename()
1038
    {
1039
        if ($this->currentFile) {
1040
            return $this->currentFile['tmp_name'];
1041
        } else {
1042
            return;
1043
        }
283 www 1044
    }
345 www 1045
 
283 www 1046
    /**
1047
     *
345 www 1048
     * @return @return void|string
283 www 1049
     */
345 www 1050
    public function getFilename()
283 www 1051
    {
345 www 1052
        if ($this->currentFile) {
1053
            return $this->normalizeStringFilename($this->currentFile['name']);
283 www 1054
        } else {
345 www 1055
            return;
283 www 1056
        }
345 www 1057
    }
334 www 1058
 
345 www 1059
    /**
1060
     *
1061
     * @return void|string
1062
     */
1063
    public function getFileType()
1064
    {
1065
        if ($this->currentFile) {
1066
            $tmp_name = $this->currentFile['tmp_name'];
1067
            $mime_type = mime_content_type($tmp_name);
1068
            if ($mime_type == 'image/jpg' || $mime_type == 'image/jpeg' || $mime_type == 'image/png') {
1069
                return self::FILE_TYPE_IMAGE;
1070
            } else if ($mime_type == 'video/quicktime' || $mime_type == 'video/webm' || $mime_type == 'video/mpeg' || $mime_type == 'video/mpg' || $mime_type == 'video/mp4') {
1071
                return self::FILE_TYPE_VIDEO;
1072
            } else if ($mime_type == 'application/pdf') {
1073
                return self::FILE_TYPE_DOCUMENT;
1074
            } else {
1075
                return;
1076
            }
1077
        } else {
1078
            return;
334 www 1079
        }
283 www 1080
    }
345 www 1081
 
1082
    /**
1083
     *
1084
     * @return void|string
1085
     */
1086
    public function getExtension()
1087
    {
1088
        if ($this->currentFile) {
1089
            $path_parts = pathinfo($this->currentFile['name']);
1090
            $ext = $path_parts['extension'];
1091
            return $ext;
1092
        } else {
1093
            return;
1094
        }
1095
    }
546 ariadna 1096
 
283 www 1097
    /**
553 stevensc 1098
     * Creates a new GD image resource with transparency settings for PNG.
1099
     * @param int $width The width of the new image.
1100
     * @param int $height The height of the new image.
1101
     * @return \GdImage|false The image resource or false on failure.
1102
     */
1103
    private function _createImageCanvas($width, $height)
1104
    {
1105
        $new_image = imageCreateTrueColor($width, $height);
1106
        if ($new_image === false) {
1107
            error_log("_createImageCanvas: imageCreateTrueColor failed for dimensions {$width}x{$height}.");
1108
            return false;
1109
        }
1110
        imageAlphaBlending($new_image, False);
1111
        imageSaveAlpha($new_image, True);
1112
        $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
1113
        if ($transparent === false) {
1114
            error_log("_createImageCanvas: imageColorAllocateAlpha failed.");
1115
            imagedestroy($new_image);
1116
            return false;
1117
        }
1118
        imagefill($new_image, 0, 0, $transparent);
1119
        return $new_image;
1120
    }
1121
 
1122
    /**
1123
     * Saves a GD image resource as PNG, unlinks source, and cleans up image resource.
1124
     * @param \GdImage $imageResource The GD image resource to save.
1125
     * @param string $targetFilename The path to save the PNG file to.
1126
     * @param string $sourceFilenameToUnlink The path to the source file to unlink.
1127
     * @return bool True on success, false on failure.
1128
     */
1129
    private function _savePngAndCleanup($imageResource, $targetFilename, $sourceFilenameToUnlink)
1130
    {
823 stevensc 1131
        $save_success = imagepng($imageResource, $targetFilename);
553 stevensc 1132
        imagedestroy($imageResource); // Clean up the image resource
1133
 
1134
        if ($save_success) {
1135
            if ($sourceFilenameToUnlink && file_exists($sourceFilenameToUnlink)) {
1136
                unlink($sourceFilenameToUnlink);
1137
            }
1138
            return true;
1139
        } else {
823 stevensc 1140
            error_log("_savePngAndCleanup: imagepng failed to save image to {$targetFilename}.");
553 stevensc 1141
            return false;
1142
        }
1143
    }
1144
 
1145
    /**
784 stevensc 1146
     * Redimensiona una imagen manteniendo su proporción original.
1147
     * La imagen resultante tendrá las dimensiones que mejor se ajusten
1148
     * dentro de los límites especificados sin recortar contenido.
283 www 1149
     *
784 stevensc 1150
     * @param string $source_filename Ruta del archivo de imagen fuente
1151
     * @param string $target_filename Ruta donde guardar la imagen redimensionada
1152
     * @param int $target_width Ancho máximo deseado
1153
     * @param int $target_height Alto máximo deseado
1154
     * @return boolean True si se redimensionó correctamente, false en caso de error
283 www 1155
     */
345 www 1156
    public function uploadImageResize($source_filename, $target_filename, $target_width, $target_height)
283 www 1157
    {
345 www 1158
        try {
1159
            $data = file_get_contents($source_filename);
1160
            $img = imagecreatefromstring($data);
546 ariadna 1161
 
1162
            if ($img) {
345 www 1163
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1164
 
345 www 1165
                $width_ratio = $target_width / $source_width;
1166
                $height_ratio = $target_height / $source_height;
546 ariadna 1167
 
1168
                if ($width_ratio > $height_ratio) {
345 www 1169
                    $resized_width = $target_width;
1170
                    $resized_height = $source_height * $width_ratio;
1171
                } else {
1172
                    $resized_height = $target_height;
1173
                    $resized_width = $source_width * $height_ratio;
1174
                }
546 ariadna 1175
 
345 www 1176
                $resized_width = round($resized_width);
1177
                $resized_height = round($resized_height);
546 ariadna 1178
 
784 stevensc 1179
                $new_image = $this->_createImageCanvas($resized_width, $resized_height);
553 stevensc 1180
                if ($new_image === false) {
1181
                    imagedestroy($img);
1182
                    unlink($source_filename);
1183
                    return false;
1184
                }
1185
 
546 ariadna 1186
                imageCopyResampled($new_image, $img, 0, 0, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
553 stevensc 1187
                imagedestroy($img); // Source image resource can be destroyed after resampling
546 ariadna 1188
 
553 stevensc 1189
                return $this->_savePngAndCleanup($new_image, $target_filename, $source_filename);
345 www 1190
            }
546 ariadna 1191
 
553 stevensc 1192
            unlink($source_filename);
345 www 1193
            return false;
1194
        } catch (\Throwable $e) {
546 ariadna 1195
            error_log($e->getTraceAsString());
553 stevensc 1196
            if (isset($img) && ($img instanceof \GdImage || is_resource($img))) {
1197
                imagedestroy($img);
1198
            }
1199
            if (isset($new_image) && ($new_image instanceof \GdImage || is_resource($new_image))) {
1200
                imagedestroy($new_image);
668 ariadna 1201
            }
553 stevensc 1202
            if (isset($source_filename) && file_exists($source_filename)) {
668 ariadna 1203
                unlink($source_filename);
553 stevensc 1204
            }
345 www 1205
            return false;
1206
        }
546 ariadna 1207
    }
345 www 1208
 
546 ariadna 1209
 
333 www 1210
    /**
345 www 1211
     *
1212
     * @param string $source
1213
     * @param string $target_filename
1214
     * @param number $target_width
1215
     * @param number $target_height
1216
 
1217
     * @return boolean
334 www 1218
     */
345 www 1219
    public function uploadImageCrop($source_filename, $target_filename, $target_width, $target_height)
1220
    {
1221
        try {
1222
            $data = file_get_contents($source_filename);
1223
            $img = imagecreatefromstring($data);
546 ariadna 1224
 
1225
            if ($img) {
345 www 1226
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1227
 
345 www 1228
                $width_ratio = $target_width / $source_width;
1229
                $height_ratio = $target_height / $source_height;
546 ariadna 1230
                if ($width_ratio > $height_ratio) {
345 www 1231
                    $resized_width = $target_width;
1232
                    $resized_height = $source_height * $width_ratio;
1233
                } else {
1234
                    $resized_height = $target_height;
1235
                    $resized_width = $source_width * $height_ratio;
1236
                }
546 ariadna 1237
 
345 www 1238
                $resized_width = round($resized_width);
1239
                $resized_height = round($resized_height);
546 ariadna 1240
 
345 www 1241
                $offset_width = round(($target_width - $resized_width) / 2);
1242
                $offset_height = round(($target_height - $resized_height) / 2);
546 ariadna 1243
 
553 stevensc 1244
                $new_image = $this->_createImageCanvas($target_width, $target_height);
1245
                if ($new_image === false) {
1246
                    imagedestroy($img);
1247
                    unlink($source_filename);
1248
                    return false;
1249
                }
546 ariadna 1250
 
1251
                imageCopyResampled($new_image, $img, $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
553 stevensc 1252
                imagedestroy($img);
546 ariadna 1253
 
553 stevensc 1254
                return $this->_savePngAndCleanup($new_image, $target_filename, $source_filename);
546 ariadna 1255
            }
1256
 
553 stevensc 1257
            unlink($source_filename);
345 www 1258
            return false;
546 ariadna 1259
        } catch (\Throwable $e) {
1260
            error_log($e->getTraceAsString());
553 stevensc 1261
            if (isset($img) && ($img instanceof \GdImage || is_resource($img))) {
1262
                imagedestroy($img);
1263
            }
1264
            if (isset($new_image) && ($new_image instanceof \GdImage || is_resource($new_image))) {
1265
                imagedestroy($new_image);
1266
            }
1267
            if (isset($source_filename) && file_exists($source_filename)) {
1268
                unlink($source_filename);
1269
            }
345 www 1270
            return false;
1271
        }
334 www 1272
    }
546 ariadna 1273
 
334 www 1274
    /**
345 www 1275
     *
1276
     * @param string $source
1277
     * @param string $target_filename
1278
     * @return boolean
333 www 1279
     */
345 www 1280
    public function uploadImageWithOutChangeSize($source_filename, $target_filename)
333 www 1281
    {
345 www 1282
        try {
1283
            $data = file_get_contents($source_filename);
1284
            $img = imagecreatefromstring($data);
546 ariadna 1285
 
1286
            if ($img) {
345 www 1287
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1288
 
553 stevensc 1289
                $new_image = $this->_createImageCanvas($source_width, $source_height);
1290
                if ($new_image === false) {
1291
                    imagedestroy($img);
1292
                    unlink($source_filename);
1293
                    return false;
1294
                }
668 ariadna 1295
 
546 ariadna 1296
                imageCopyResampled($new_image, $img, 0, 0, 0, 0, $source_width, $source_height, $source_width, $source_height);
553 stevensc 1297
                imagedestroy($img);
546 ariadna 1298
 
553 stevensc 1299
                return $this->_savePngAndCleanup($new_image, $target_filename, $source_filename);
546 ariadna 1300
            }
1301
 
553 stevensc 1302
            unlink($source_filename);
345 www 1303
            return false;
1304
        } catch (\Throwable $e) {
546 ariadna 1305
            error_log($e->getTraceAsString());
781 stevensc 1306
            error_log('Error al subir la imagen: ' . $e->getMessage());
553 stevensc 1307
            if (isset($img) && ($img instanceof \GdImage || is_resource($img))) {
1308
                imagedestroy($img);
1309
            }
1310
            if (isset($new_image) && ($new_image instanceof \GdImage || is_resource($new_image))) {
1311
                imagedestroy($new_image);
1312
            }
1313
            if (isset($source_filename) && file_exists($source_filename)) {
1314
                unlink($source_filename);
1315
            }
345 www 1316
            return false;
333 www 1317
        }
345 www 1318
    }
546 ariadna 1319
 
345 www 1320
    public static function extractPosterFromVideo($source_filename, $target_filename)
1321
    {
546 ariadna 1322
 
345 www 1323
        try {
1324
            $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width,duration  $source_filename";
1325
            $response   = trim(shell_exec($cmd));
546 ariadna 1326
 
345 www 1327
            $source_duration = 0;
1328
            $lines = explode("\n", $response);
1329
            foreach ($lines as $line) {
1330
                $line = trim(strtolower($line));
1331
                if (strpos($line, 'duration') !== false) {
1332
                    $values = explode('=', $line);
1333
                    $source_duration = intval(str_replace($values[1], '#', ''), 10);
1334
                }
1335
            }
546 ariadna 1336
 
1337
 
345 www 1338
            if ($source_duration == 0) {
1339
                $second_extract = '00:00:02';
1340
            } else {
1341
                if ($source_duration > 10) {
1342
                    $second_extract = '00:00:10';
1343
                } else {
1344
                    $second_extract = '00:00:02';
1345
                }
333 www 1346
            }
546 ariadna 1347
 
345 www 1348
            $cmd = "/usr/bin/ffmpeg -y -i $source_filename  -pix_fmt yuvj422p -an -ss $second_extract -f mjpeg -t 1 -r 1 -y  $target_filename";
1349
            exec($cmd);
546 ariadna 1350
 
345 www 1351
            return true;
546 ariadna 1352
        } catch (\Throwable $e) {
1353
            error_log($e->getTraceAsString());
1354
 
345 www 1355
            return false;
1356
        }
333 www 1357
    }
345 www 1358
 
1359
    /**
1360
     *
1361
     * @param string $str
1362
     * @return string
1363
     */
1364
    public function normalizeStringFilename($str = '')
1365
    {
1366
        $basename = substr($str, 0, strrpos($str, '.'));
1367
        $basename = str_replace('.', '-', $basename);
1368
 
1369
        $extension = substr($str, strrpos($str, '.'));
1370
 
1371
        $str = $basename . $extension;
1372
 
1373
        $str = strip_tags($str);
1374
        $str = preg_replace('/[\r\n\t ]+/', ' ', $str);
1375
        $str = preg_replace('/[\#\"\*\/\:\<\>\?\'\|\,]+/', ' ', $str);
1376
        $str = strtolower($str);
1377
        $str = html_entity_decode($str, ENT_QUOTES, "utf-8");
1378
        $str = htmlentities($str, ENT_QUOTES, "utf-8");
1379
        $str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
1380
        $str = str_replace(' ', '-', $str);
1381
        $str = rawurlencode($str);
1382
        $str = str_replace('%', '-', $str);
1383
        $str = str_replace([
1384
            '-----',
1385
            '----',
1386
            '---',
1387
            '--'
1388
        ], '-', $str);
1389
        return trim(strtolower($str));
1390
    }
546 ariadna 1391
}