Proyectos de Subversion LeadersLinked - Services

Rev

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