Proyectos de Subversion LeadersLinked - Services

Rev

Rev 821 | Rev 823 | 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
 
822 stevensc 510
        $storageDir = $this->config['leaderslinked.storage.path'];
511
        $storageFileFullPath = $storageDir . DIRECTORY_SEPARATOR . $path;
512
        if (file_exists($storageFileFullPath)) {
513
            return $this->getPresignedUrl($storageFileFullPath);
514
        }
515
 
345 www 516
        $remoto = $this->getFullPathImageDefault();
517
        return $this->getPresignedUrl($remoto);
283 www 518
    }
334 www 519
 
520
    /**
521
     *
522
     * @param string $path
523
     * @param string $code,
524
     * @param string $filename
525
     * @return string
526
     */
345 www 527
    public function getGenericFile($path, $code, $filename)
334 www 528
    {
345 www 529
        $remoto = $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;
546 ariadna 530
 
345 www 531
        if (file_exists($remoto)) {
546 ariadna 532
            if ($code == 'd8e6c5de-52a6-4c28-bdce-4ba2823ba841') {
533
 
345 www 534
                error_log('getGenericFile = ' . $remoto);
535
            }
546 ariadna 536
 
537
 
333 www 538
            return $this->getPresignedUrl($remoto);
539
        }
760 stevensc 540
 
541
        $remoto = $path . DIRECTORY_SEPARATOR . $filename;
542
 
543
        if (file_exists($remoto)) {
544
            return $this->getPresignedUrl($remoto);
545
        }
546
 
547
        $remoto = $this->getFullPathImageDefault();
548
        return $this->getPresignedUrl($remoto);
333 www 549
    }
546 ariadna 550
 
345 www 551
    public function getPathDefault()
333 www 552
    {
345 www 553
        return $this->getPathByType(self::TYPE_DEFAULT);
333 www 554
    }
546 ariadna 555
 
345 www 556
    public function getPathChat()
320 www 557
    {
345 www 558
        return $this->getPathByType(self::TYPE_CHAT);
320 www 559
    }
546 ariadna 560
 
283 www 561
    public function getPathGroup()
562
    {
345 www 563
        return $this->getPathByType(self::TYPE_GROUP);
283 www 564
    }
546 ariadna 565
 
283 www 566
    public function getPathUser()
567
    {
345 www 568
        return $this->getPathByType(self::TYPE_USER);
283 www 569
    }
546 ariadna 570
 
283 www 571
    public function getPathImage()
572
    {
345 www 573
        return $this->getPathByType(self::TYPE_IMAGE);
283 www 574
    }
546 ariadna 575
 
283 www 576
    public function getPathJob()
577
    {
345 www 578
        return $this->getPathByType(self::TYPE_JOB);
283 www 579
    }
546 ariadna 580
 
283 www 581
    public function getPathCompany()
582
    {
345 www 583
        return $this->getPathByType(self::TYPE_COMPANY);
283 www 584
    }
546 ariadna 585
 
586
 
283 www 587
    public function getPathFeed()
588
    {
345 www 589
        return $this->getPathByType(self::TYPE_FEED);
283 www 590
    }
546 ariadna 591
 
345 www 592
    public function getPathMedia()
295 www 593
    {
345 www 594
        return $this->getPathByType(self::TYPE_MEDIA);
295 www 595
    }
546 ariadna 596
 
283 www 597
    public function getPathPost()
598
    {
345 www 599
        return $this->getPathByType(self::TYPE_POST);
283 www 600
    }
546 ariadna 601
 
602
 
283 www 603
    public function getPathMicrolearningTopic()
604
    {
345 www 605
        return $this->getPathByType(self::TYPE_MICROLEARNING_TOPIC);
283 www 606
    }
546 ariadna 607
 
283 www 608
    public function getPathMicrolearningCapsule()
609
    {
345 www 610
        return $this->getPathByType(self::TYPE_MICROLEARNING_CAPSULE);
283 www 611
    }
546 ariadna 612
 
283 www 613
    public function getPathMicrolearningSlide()
614
    {
345 www 615
        return $this->getPathByType(self::TYPE_MICROLEARNING_SLIDE);
283 www 616
    }
546 ariadna 617
 
283 www 618
    public function getPathJobDescription()
619
    {
345 www 620
        return $this->getPathByType(self::TYPE_JOB_DESCRIPTION);
283 www 621
    }
546 ariadna 622
 
295 www 623
    public function getPathSelfEvaluation()
283 www 624
    {
345 www 625
        return $this->getPathByType(self::TYPE_SELF_EVALUATION);
283 www 626
    }
546 ariadna 627
 
345 www 628
    public function getPathPerformanceEvaluation()
629
    {
630
        return $this->getPathByType(self::TYPE_PERFORMANCE_EVALUATION);
631
    }
546 ariadna 632
 
633
 
283 www 634
    public function getPathRecruitmentSelection()
635
    {
345 www 636
        return $this->getPathByType(self::TYPE_RECRUITMENT_SELECTION);
283 www 637
    }
546 ariadna 638
 
639
 
345 www 640
    public function getPathPlanningObjectivesAndGoals()
283 www 641
    {
345 www 642
        return $this->getPathByType(self::TYPE_PLANNING_OBJECTIVES_AND_GOALS);
283 www 643
    }
546 ariadna 644
 
345 www 645
    public function getPathMessage()
283 www 646
    {
345 www 647
        return $this->getPathByType(self::TYPE_MESSAGE);
283 www 648
    }
546 ariadna 649
 
650
 
283 www 651
    public function getPathSurvey()
652
    {
345 www 653
        return $this->getPathByType(self::TYPE_SURVEY);
283 www 654
    }
546 ariadna 655
 
656
 
283 www 657
    public function getPathNetwork()
658
    {
345 www 659
        return $this->getPathByType(self::TYPE_NETWORK);
283 www 660
    }
546 ariadna 661
 
662
 
345 www 663
    public function getPathDailyPulse()
283 www 664
    {
345 www 665
        return $this->getPathByType(self::TYPE_DAILY_PULSE);
283 www 666
    }
345 www 667
 
546 ariadna 668
 
345 www 669
    public function getPathEngagementReward()
283 www 670
    {
345 www 671
        return $this->getPathByType(self::TYPE_ENGAGEMENT_REWARD);
283 www 672
    }
546 ariadna 673
 
283 www 674
    public function getPathKnowledgeArea()
675
    {
345 www 676
        return $this->getPathByType(self::TYPE_KNOWLEDGE_AREA);
283 www 677
    }
546 ariadna 678
 
283 www 679
    public function getPathMyCoach()
680
    {
345 www 681
        return $this->getPathByType(self::TYPE_MY_COACH);
283 www 682
    }
546 ariadna 683
 
345 www 684
    public function getPathHabitEmoji()
283 www 685
    {
345 www 686
        return $this->getPathByType(self::TYPE_HABIT_EMOJI);
283 www 687
    }
345 www 688
 
689
    public function getPathHabitContent()
283 www 690
    {
345 www 691
        return $this->getPathByType(self::TYPE_HABIT_CONTENT);
692
    }
693
 
694
 
695
    public function getPathByType($type)
696
    {
697
        switch ($type) {
698
            case self::TYPE_DEFAULT:
699
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'default';
700
                break;
701
 
702
            case self::TYPE_CHAT:
703
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'chat';
704
                break;
705
 
706
            case self::TYPE_GROUP:
707
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'group';
708
                break;
709
 
710
            case self::TYPE_USER:
711
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'user';
712
                break;
713
 
714
            case self::TYPE_IMAGE:
715
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'image';
716
                break;
717
 
718
            case self::TYPE_JOB:
719
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'job';
720
                break;
721
 
722
            case self::TYPE_COMPANY:
723
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'company';
724
                break;
725
 
726
            case self::TYPE_FEED:
727
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'feed';
728
                break;
729
 
730
            case self::TYPE_MEDIA:
731
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'media';
732
                break;
733
 
734
            case self::TYPE_POST:
735
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'post';
736
                break;
737
 
738
            case self::TYPE_MICROLEARNING_TOPIC:
739
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'topic';
740
                break;
741
 
742
            case self::TYPE_MICROLEARNING_CAPSULE:
743
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'capsule';
744
                break;
745
 
746
            case self::TYPE_MICROLEARNING_SLIDE:
747
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'slide';
748
                break;
749
 
750
            case self::TYPE_JOB_DESCRIPTION:
751
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'job-description';
752
                break;
753
 
754
            case self::TYPE_SELF_EVALUATION:
755
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'self-evaluation';
756
                break;
757
 
758
            case self::TYPE_PERFORMANCE_EVALUATION:
759
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'performance-evaluation';
760
                break;
761
 
762
            case self::TYPE_RECRUITMENT_SELECTION:
763
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'recruitment-selection';
764
                break;
765
 
766
            case self::TYPE_PLANNING_OBJECTIVES_AND_GOALS:
767
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'planning-objectives-and-goals';
768
                break;
769
 
770
            case self::TYPE_MESSAGE:
771
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'message';
772
                break;
773
 
774
            case self::TYPE_SURVEY:
775
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'survey';
776
                break;
777
 
778
            case self::TYPE_NETWORK:
779
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'network';
780
                break;
781
 
782
            case self::TYPE_DAILY_PULSE:
783
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'daily-pulse';
784
                break;
785
 
786
            case self::TYPE_ENGAGEMENT_REWARD:
787
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'engagement-reward';
788
                break;
789
 
790
            case self::TYPE_KNOWLEDGE_AREA:
791
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'knowledge-area';
792
                break;
793
 
794
            case self::TYPE_MY_COACH:
795
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'my-coach';
796
                break;
797
 
798
            case self::TYPE_HABIT_EMOJI:
799
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'habit' . DIRECTORY_SEPARATOR . 'emoji';
800
                break;
801
 
802
            case self::TYPE_HABIT_CONTENT:
803
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'habit' . DIRECTORY_SEPARATOR . 'content';
804
                break;
805
 
806
            default:
807
                $path = $this->storagePath;
808
                break;
334 www 809
        }
345 www 810
 
811
        if (! file_exists($path)) {
812
            mkdir($path, 0775, true);
813
        }
814
 
815
        return $path;
283 www 816
    }
345 www 817
 
283 www 818
    /**
819
     *
820
     * @param string $path
821
     * @param string $code
822
     * @param string $filename
345 www 823
     * @return string
283 www 824
     */
825
    public function deleteFile($path, $code, $filename)
826
    {
345 www 827
        $remoto = $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;
553 stevensc 828
        if (file_exists($remoto)) {
829
            return unlink($remoto);
283 www 830
        } else {
831
            return true;
832
        }
833
    }
345 www 834
 
295 www 835
    /**
836
     *
549 stevensc 837
     * @param string $type
295 www 838
     * @param string $code
839
     * @return boolean
840
     */
549 stevensc 841
    public function deleteDirectory($type, $code)
295 www 842
    {
549 stevensc 843
        $path = $this->getPathByType($type);
844
        $remoto = $path . DIRECTORY_SEPARATOR . $code;
345 www 845
        if (file_exists($remoto)) {
334 www 846
            $this->deleteDirectoryRecursive($remoto);
847
            return true;
295 www 848
        } else {
849
            return true;
850
        }
851
    }
345 www 852
 
334 www 853
    /**
854
     *
855
     * @param string $dir
856
     */
345 www 857
    private function deleteDirectoryRecursive($dir)
858
    {
334 www 859
        if (is_dir($dir)) {
860
            $objects = scandir($dir);
861
            foreach ($objects as $object) {
345 www 862
                if ($object != '.' && $object != '..') {
863
                    $s = $dir . DIRECTORY_SEPARATOR . $object;
864
                    if (is_dir($s) && ! is_link($s)) {
549 stevensc 865
                        $this->deleteDirectoryRecursive($s);
345 www 866
                    } else {
553 stevensc 867
                        unlink($s);
334 www 868
                    }
869
                }
870
            }
553 stevensc 871
            rmdir($dir);
334 www 872
        }
873
    }
283 www 874
 
875
    /**
876
     *
877
     * @param string $path
878
     * @param string $code
290 www 879
     * @param string $local_filename
283 www 880
     * @return boolean
881
     */
345 www 882
    public function putFile($source_file, $target_file)
283 www 883
    {
553 stevensc 884
        return rename($source_file, $target_file);
345 www 885
    }
886
 
887
    /**
888
     *
889
     * @param string $tempfile
890
     * @param string $path
891
     * @param string $code
892
     * @param string $filename
893
     * @return boolean
894
     */
895
    public function moveUploadedFile($source_file, $target_file)
896
    {
553 stevensc 897
        return move_uploaded_file($source_file, $target_file);
345 www 898
    }
899
 
900
    /**
901
     *
346 www 902
     * @param string $tempfile
903
     * @param string $path
904
     * @param string $code
905
     * @param string $filename
906
     * @return boolean
907
     */
908
    public function copyFile($source_file, $target_file)
909
    {
553 stevensc 910
        return copy($source_file, $target_file);
346 www 911
    }
546 ariadna 912
 
913
 
346 www 914
    /**
915
     *
345 www 916
     * @param string $url
917
     * @return string
918
     */
919
    public function getPresignedUrl($url)
920
    {
921
        $code = hash('sha256', $url);
546 ariadna 922
 
345 www 923
        $storageFileMapper = \LeadersLinked\Mapper\StorageFileMapper::getInstance($this->adapter);
924
        $storageFile = $storageFileMapper->fetchOneByCode($code);
925
        if (! $storageFile) {
926
            $storageFile = new \LeadersLinked\Model\StorageFile();
927
            $storageFile->code = $code;
928
            $storageFile->path = $url;
929
            $storageFile->salt = \LeadersLinked\Library\Functions::generatePassword(8);
930
 
546 ariadna 931
 
932
 
345 www 933
            $storageFileMapper->insert($storageFile);
334 www 934
        }
345 www 935
 
546 ariadna 936
 
345 www 937
        $url = 'https://' . $_SERVER['SERVER_NAME'] . '/storage/' . $code;
668 ariadna 938
 
345 www 939
        return $url;
940
    }
290 www 941
 
345 www 942
    /**
943
     *
944
     * @return @return void|string
945
     */
946
    public function getFilenamePNG()
947
    {
948
        if ($this->currentFile) {
949
            $filename = $this->normalizeStringFilename($this->currentFile['name']);
950
            $path_parts = pathinfo($filename);
951
            return $path_parts['filename'] . '.png';
952
        } else {
953
            return;
954
        }
955
    }
333 www 956
 
345 www 957
    /**
958
     *
959
     * @return @return void|string
960
     */
961
    public function getTmpFilename()
962
    {
963
        if ($this->currentFile) {
964
            return $this->currentFile['tmp_name'];
965
        } else {
966
            return;
967
        }
283 www 968
    }
345 www 969
 
283 www 970
    /**
971
     *
345 www 972
     * @return @return void|string
283 www 973
     */
345 www 974
    public function getFilename()
283 www 975
    {
345 www 976
        if ($this->currentFile) {
977
            return $this->normalizeStringFilename($this->currentFile['name']);
283 www 978
        } else {
345 www 979
            return;
283 www 980
        }
345 www 981
    }
334 www 982
 
345 www 983
    /**
984
     *
985
     * @return void|string
986
     */
987
    public function getFileType()
988
    {
989
        if ($this->currentFile) {
990
            $tmp_name = $this->currentFile['tmp_name'];
991
            $mime_type = mime_content_type($tmp_name);
992
            if ($mime_type == 'image/jpg' || $mime_type == 'image/jpeg' || $mime_type == 'image/png') {
993
                return self::FILE_TYPE_IMAGE;
994
            } else if ($mime_type == 'video/quicktime' || $mime_type == 'video/webm' || $mime_type == 'video/mpeg' || $mime_type == 'video/mpg' || $mime_type == 'video/mp4') {
995
                return self::FILE_TYPE_VIDEO;
996
            } else if ($mime_type == 'application/pdf') {
997
                return self::FILE_TYPE_DOCUMENT;
998
            } else {
999
                return;
1000
            }
1001
        } else {
1002
            return;
334 www 1003
        }
283 www 1004
    }
345 www 1005
 
1006
    /**
1007
     *
1008
     * @return void|string
1009
     */
1010
    public function getExtension()
1011
    {
1012
        if ($this->currentFile) {
1013
            $path_parts = pathinfo($this->currentFile['name']);
1014
            $ext = $path_parts['extension'];
1015
            return $ext;
1016
        } else {
1017
            return;
1018
        }
1019
    }
546 ariadna 1020
 
283 www 1021
    /**
553 stevensc 1022
     * Creates a new GD image resource with transparency settings for PNG.
1023
     * @param int $width The width of the new image.
1024
     * @param int $height The height of the new image.
1025
     * @return \GdImage|false The image resource or false on failure.
1026
     */
1027
    private function _createImageCanvas($width, $height)
1028
    {
1029
        $new_image = imageCreateTrueColor($width, $height);
1030
        if ($new_image === false) {
1031
            error_log("_createImageCanvas: imageCreateTrueColor failed for dimensions {$width}x{$height}.");
1032
            return false;
1033
        }
1034
        imageAlphaBlending($new_image, False);
1035
        imageSaveAlpha($new_image, True);
1036
        $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
1037
        if ($transparent === false) {
1038
            error_log("_createImageCanvas: imageColorAllocateAlpha failed.");
1039
            imagedestroy($new_image);
1040
            return false;
1041
        }
1042
        imagefill($new_image, 0, 0, $transparent);
1043
        return $new_image;
1044
    }
1045
 
1046
    /**
1047
     * Saves a GD image resource as PNG, unlinks source, and cleans up image resource.
1048
     * @param \GdImage $imageResource The GD image resource to save.
1049
     * @param string $targetFilename The path to save the PNG file to.
1050
     * @param string $sourceFilenameToUnlink The path to the source file to unlink.
1051
     * @return bool True on success, false on failure.
1052
     */
1053
    private function _savePngAndCleanup($imageResource, $targetFilename, $sourceFilenameToUnlink)
1054
    {
820 stevensc 1055
        $storageDir = $this->config['leaderslinked.storage.path'];
1056
        $storageFileFullPath = $storageDir . DIRECTORY_SEPARATOR . $targetFilename;
1057
 
1058
        $save_success = imagepng($imageResource, $storageFileFullPath);
553 stevensc 1059
        imagedestroy($imageResource); // Clean up the image resource
1060
 
1061
        if ($save_success) {
1062
            if ($sourceFilenameToUnlink && file_exists($sourceFilenameToUnlink)) {
1063
                unlink($sourceFilenameToUnlink);
1064
            }
1065
            return true;
1066
        } else {
820 stevensc 1067
            error_log("_savePngAndCleanup: imagepng failed to save image to {$storageFileFullPath}.");
553 stevensc 1068
            return false;
1069
        }
1070
    }
1071
 
1072
    /**
784 stevensc 1073
     * Redimensiona una imagen manteniendo su proporción original.
1074
     * La imagen resultante tendrá las dimensiones que mejor se ajusten
1075
     * dentro de los límites especificados sin recortar contenido.
283 www 1076
     *
784 stevensc 1077
     * @param string $source_filename Ruta del archivo de imagen fuente
1078
     * @param string $target_filename Ruta donde guardar la imagen redimensionada
1079
     * @param int $target_width Ancho máximo deseado
1080
     * @param int $target_height Alto máximo deseado
1081
     * @return boolean True si se redimensionó correctamente, false en caso de error
283 www 1082
     */
345 www 1083
    public function uploadImageResize($source_filename, $target_filename, $target_width, $target_height)
283 www 1084
    {
345 www 1085
        try {
1086
            $data = file_get_contents($source_filename);
1087
            $img = imagecreatefromstring($data);
546 ariadna 1088
 
1089
            if ($img) {
345 www 1090
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1091
 
345 www 1092
                $width_ratio = $target_width / $source_width;
1093
                $height_ratio = $target_height / $source_height;
546 ariadna 1094
 
1095
                if ($width_ratio > $height_ratio) {
345 www 1096
                    $resized_width = $target_width;
1097
                    $resized_height = $source_height * $width_ratio;
1098
                } else {
1099
                    $resized_height = $target_height;
1100
                    $resized_width = $source_width * $height_ratio;
1101
                }
546 ariadna 1102
 
345 www 1103
                $resized_width = round($resized_width);
1104
                $resized_height = round($resized_height);
546 ariadna 1105
 
784 stevensc 1106
                $new_image = $this->_createImageCanvas($resized_width, $resized_height);
553 stevensc 1107
                if ($new_image === false) {
1108
                    imagedestroy($img);
1109
                    unlink($source_filename);
1110
                    return false;
1111
                }
1112
 
546 ariadna 1113
                imageCopyResampled($new_image, $img, 0, 0, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
553 stevensc 1114
                imagedestroy($img); // Source image resource can be destroyed after resampling
546 ariadna 1115
 
553 stevensc 1116
                return $this->_savePngAndCleanup($new_image, $target_filename, $source_filename);
345 www 1117
            }
546 ariadna 1118
 
553 stevensc 1119
            unlink($source_filename);
345 www 1120
            return false;
1121
        } catch (\Throwable $e) {
546 ariadna 1122
            error_log($e->getTraceAsString());
553 stevensc 1123
            if (isset($img) && ($img instanceof \GdImage || is_resource($img))) {
1124
                imagedestroy($img);
1125
            }
1126
            if (isset($new_image) && ($new_image instanceof \GdImage || is_resource($new_image))) {
1127
                imagedestroy($new_image);
668 ariadna 1128
            }
553 stevensc 1129
            if (isset($source_filename) && file_exists($source_filename)) {
668 ariadna 1130
                unlink($source_filename);
553 stevensc 1131
            }
345 www 1132
            return false;
1133
        }
546 ariadna 1134
    }
345 www 1135
 
546 ariadna 1136
 
333 www 1137
    /**
345 www 1138
     *
1139
     * @param string $source
1140
     * @param string $target_filename
1141
     * @param number $target_width
1142
     * @param number $target_height
1143
 
1144
     * @return boolean
334 www 1145
     */
345 www 1146
    public function uploadImageCrop($source_filename, $target_filename, $target_width, $target_height)
1147
    {
1148
        try {
1149
            $data = file_get_contents($source_filename);
1150
            $img = imagecreatefromstring($data);
546 ariadna 1151
 
1152
            if ($img) {
345 www 1153
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1154
 
345 www 1155
                $width_ratio = $target_width / $source_width;
1156
                $height_ratio = $target_height / $source_height;
546 ariadna 1157
                if ($width_ratio > $height_ratio) {
345 www 1158
                    $resized_width = $target_width;
1159
                    $resized_height = $source_height * $width_ratio;
1160
                } else {
1161
                    $resized_height = $target_height;
1162
                    $resized_width = $source_width * $height_ratio;
1163
                }
546 ariadna 1164
 
345 www 1165
                $resized_width = round($resized_width);
1166
                $resized_height = round($resized_height);
546 ariadna 1167
 
345 www 1168
                $offset_width = round(($target_width - $resized_width) / 2);
1169
                $offset_height = round(($target_height - $resized_height) / 2);
546 ariadna 1170
 
553 stevensc 1171
                $new_image = $this->_createImageCanvas($target_width, $target_height);
1172
                if ($new_image === false) {
1173
                    imagedestroy($img);
1174
                    unlink($source_filename);
1175
                    return false;
1176
                }
546 ariadna 1177
 
1178
                imageCopyResampled($new_image, $img, $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
553 stevensc 1179
                imagedestroy($img);
546 ariadna 1180
 
553 stevensc 1181
                return $this->_savePngAndCleanup($new_image, $target_filename, $source_filename);
546 ariadna 1182
            }
1183
 
553 stevensc 1184
            unlink($source_filename);
345 www 1185
            return false;
546 ariadna 1186
        } catch (\Throwable $e) {
1187
            error_log($e->getTraceAsString());
553 stevensc 1188
            if (isset($img) && ($img instanceof \GdImage || is_resource($img))) {
1189
                imagedestroy($img);
1190
            }
1191
            if (isset($new_image) && ($new_image instanceof \GdImage || is_resource($new_image))) {
1192
                imagedestroy($new_image);
1193
            }
1194
            if (isset($source_filename) && file_exists($source_filename)) {
1195
                unlink($source_filename);
1196
            }
345 www 1197
            return false;
1198
        }
334 www 1199
    }
546 ariadna 1200
 
334 www 1201
    /**
345 www 1202
     *
1203
     * @param string $source
1204
     * @param string $target_filename
1205
     * @return boolean
333 www 1206
     */
345 www 1207
    public function uploadImageWithOutChangeSize($source_filename, $target_filename)
333 www 1208
    {
345 www 1209
        try {
1210
            $data = file_get_contents($source_filename);
1211
            $img = imagecreatefromstring($data);
546 ariadna 1212
 
1213
            if ($img) {
345 www 1214
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1215
 
553 stevensc 1216
                $new_image = $this->_createImageCanvas($source_width, $source_height);
1217
                if ($new_image === false) {
1218
                    imagedestroy($img);
1219
                    unlink($source_filename);
1220
                    return false;
1221
                }
668 ariadna 1222
 
546 ariadna 1223
                imageCopyResampled($new_image, $img, 0, 0, 0, 0, $source_width, $source_height, $source_width, $source_height);
553 stevensc 1224
                imagedestroy($img);
546 ariadna 1225
 
553 stevensc 1226
                return $this->_savePngAndCleanup($new_image, $target_filename, $source_filename);
546 ariadna 1227
            }
1228
 
553 stevensc 1229
            unlink($source_filename);
345 www 1230
            return false;
1231
        } catch (\Throwable $e) {
546 ariadna 1232
            error_log($e->getTraceAsString());
781 stevensc 1233
            error_log('Error al subir la imagen: ' . $e->getMessage());
553 stevensc 1234
            if (isset($img) && ($img instanceof \GdImage || is_resource($img))) {
1235
                imagedestroy($img);
1236
            }
1237
            if (isset($new_image) && ($new_image instanceof \GdImage || is_resource($new_image))) {
1238
                imagedestroy($new_image);
1239
            }
1240
            if (isset($source_filename) && file_exists($source_filename)) {
1241
                unlink($source_filename);
1242
            }
345 www 1243
            return false;
333 www 1244
        }
345 www 1245
    }
546 ariadna 1246
 
345 www 1247
    public static function extractPosterFromVideo($source_filename, $target_filename)
1248
    {
546 ariadna 1249
 
345 www 1250
        try {
1251
            $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width,duration  $source_filename";
1252
            $response   = trim(shell_exec($cmd));
546 ariadna 1253
 
345 www 1254
            $source_duration = 0;
1255
            $lines = explode("\n", $response);
1256
            foreach ($lines as $line) {
1257
                $line = trim(strtolower($line));
1258
                if (strpos($line, 'duration') !== false) {
1259
                    $values = explode('=', $line);
1260
                    $source_duration = intval(str_replace($values[1], '#', ''), 10);
1261
                }
1262
            }
546 ariadna 1263
 
1264
 
345 www 1265
            if ($source_duration == 0) {
1266
                $second_extract = '00:00:02';
1267
            } else {
1268
                if ($source_duration > 10) {
1269
                    $second_extract = '00:00:10';
1270
                } else {
1271
                    $second_extract = '00:00:02';
1272
                }
333 www 1273
            }
546 ariadna 1274
 
345 www 1275
            $cmd = "/usr/bin/ffmpeg -y -i $source_filename  -pix_fmt yuvj422p -an -ss $second_extract -f mjpeg -t 1 -r 1 -y  $target_filename";
1276
            exec($cmd);
546 ariadna 1277
 
345 www 1278
            return true;
546 ariadna 1279
        } catch (\Throwable $e) {
1280
            error_log($e->getTraceAsString());
1281
 
345 www 1282
            return false;
1283
        }
333 www 1284
    }
345 www 1285
 
1286
    /**
1287
     *
1288
     * @param string $str
1289
     * @return string
1290
     */
1291
    public function normalizeStringFilename($str = '')
1292
    {
1293
        $basename = substr($str, 0, strrpos($str, '.'));
1294
        $basename = str_replace('.', '-', $basename);
1295
 
1296
        $extension = substr($str, strrpos($str, '.'));
1297
 
1298
        $str = $basename . $extension;
1299
 
1300
        $str = strip_tags($str);
1301
        $str = preg_replace('/[\r\n\t ]+/', ' ', $str);
1302
        $str = preg_replace('/[\#\"\*\/\:\<\>\?\'\|\,]+/', ' ', $str);
1303
        $str = strtolower($str);
1304
        $str = html_entity_decode($str, ENT_QUOTES, "utf-8");
1305
        $str = htmlentities($str, ENT_QUOTES, "utf-8");
1306
        $str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
1307
        $str = str_replace(' ', '-', $str);
1308
        $str = rawurlencode($str);
1309
        $str = str_replace('%', '-', $str);
1310
        $str = str_replace([
1311
            '-----',
1312
            '----',
1313
            '---',
1314
            '--'
1315
        ], '-', $str);
1316
        return trim(strtolower($str));
1317
    }
546 ariadna 1318
}