Proyectos de Subversion LeadersLinked - Services

Rev

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