Proyectos de Subversion LeadersLinked - Services

Rev

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