Proyectos de Subversion LeadersLinked - Services

Rev

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