Proyectos de Subversion LeadersLinked - Services

Rev

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