Proyectos de Subversion LeadersLinked - Services

Rev

Rev 762 | Rev 767 | 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
    {
763 stevensc 912
        $storage_url = 'https://' . $_SERVER['SERVER_NAME'] . '/' . $url;
761 stevensc 913
        return $storage_url;
914
 
915
        /*
345 www 916
        $code = hash('sha256', $url);
546 ariadna 917
 
345 www 918
        $storageFileMapper = \LeadersLinked\Mapper\StorageFileMapper::getInstance($this->adapter);
919
        $storageFile = $storageFileMapper->fetchOneByCode($code);
920
        if (! $storageFile) {
921
            $storageFile = new \LeadersLinked\Model\StorageFile();
922
            $storageFile->code = $code;
923
            $storageFile->path = $url;
924
            $storageFile->salt = \LeadersLinked\Library\Functions::generatePassword(8);
925
 
546 ariadna 926
 
927
 
345 www 928
            $storageFileMapper->insert($storageFile);
334 www 929
        }
345 www 930
 
546 ariadna 931
 
345 www 932
        $url = 'https://' . $_SERVER['SERVER_NAME'] . '/storage/' . $code;
668 ariadna 933
 
345 www 934
        return $url;
761 stevensc 935
        */
345 www 936
    }
290 www 937
 
345 www 938
    /**
939
     *
940
     * @return @return void|string
941
     */
942
    public function getFilenamePNG()
943
    {
944
        if ($this->currentFile) {
945
            $filename = $this->normalizeStringFilename($this->currentFile['name']);
946
            $path_parts = pathinfo($filename);
947
            return $path_parts['filename'] . '.png';
948
        } else {
949
            return;
950
        }
951
    }
333 www 952
 
345 www 953
    /**
954
     *
955
     * @return @return void|string
956
     */
957
    public function getTmpFilename()
958
    {
959
        if ($this->currentFile) {
960
            return $this->currentFile['tmp_name'];
961
        } else {
962
            return;
963
        }
283 www 964
    }
345 www 965
 
283 www 966
    /**
967
     *
345 www 968
     * @return @return void|string
283 www 969
     */
345 www 970
    public function getFilename()
283 www 971
    {
345 www 972
        if ($this->currentFile) {
973
            return $this->normalizeStringFilename($this->currentFile['name']);
283 www 974
        } else {
345 www 975
            return;
283 www 976
        }
345 www 977
    }
334 www 978
 
345 www 979
    /**
980
     *
981
     * @return void|string
982
     */
983
    public function getFileType()
984
    {
985
        if ($this->currentFile) {
986
            $tmp_name = $this->currentFile['tmp_name'];
987
            $mime_type = mime_content_type($tmp_name);
988
            if ($mime_type == 'image/jpg' || $mime_type == 'image/jpeg' || $mime_type == 'image/png') {
989
                return self::FILE_TYPE_IMAGE;
990
            } else if ($mime_type == 'video/quicktime' || $mime_type == 'video/webm' || $mime_type == 'video/mpeg' || $mime_type == 'video/mpg' || $mime_type == 'video/mp4') {
991
                return self::FILE_TYPE_VIDEO;
992
            } else if ($mime_type == 'application/pdf') {
993
                return self::FILE_TYPE_DOCUMENT;
994
            } else {
995
                return;
996
            }
997
        } else {
998
            return;
334 www 999
        }
283 www 1000
    }
345 www 1001
 
1002
    /**
1003
     *
1004
     * @return void|string
1005
     */
1006
    public function getExtension()
1007
    {
1008
        if ($this->currentFile) {
1009
            $path_parts = pathinfo($this->currentFile['name']);
1010
            $ext = $path_parts['extension'];
1011
            return $ext;
1012
        } else {
1013
            return;
1014
        }
1015
    }
546 ariadna 1016
 
283 www 1017
    /**
553 stevensc 1018
     * Creates a new GD image resource with transparency settings for PNG.
1019
     * @param int $width The width of the new image.
1020
     * @param int $height The height of the new image.
1021
     * @return \GdImage|false The image resource or false on failure.
1022
     */
1023
    private function _createImageCanvas($width, $height)
1024
    {
1025
        $new_image = imageCreateTrueColor($width, $height);
1026
        if ($new_image === false) {
1027
            error_log("_createImageCanvas: imageCreateTrueColor failed for dimensions {$width}x{$height}.");
1028
            return false;
1029
        }
1030
        imageAlphaBlending($new_image, False);
1031
        imageSaveAlpha($new_image, True);
1032
        $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
1033
        if ($transparent === false) {
1034
            error_log("_createImageCanvas: imageColorAllocateAlpha failed.");
1035
            imagedestroy($new_image);
1036
            return false;
1037
        }
1038
        imagefill($new_image, 0, 0, $transparent);
1039
        return $new_image;
1040
    }
1041
 
1042
    /**
1043
     * Saves a GD image resource as PNG, unlinks source, and cleans up image resource.
1044
     * @param \GdImage $imageResource The GD image resource to save.
1045
     * @param string $targetFilename The path to save the PNG file to.
1046
     * @param string $sourceFilenameToUnlink The path to the source file to unlink.
1047
     * @return bool True on success, false on failure.
1048
     */
1049
    private function _savePngAndCleanup($imageResource, $targetFilename, $sourceFilenameToUnlink)
1050
    {
1051
        $save_success = imagepng($imageResource, $targetFilename);
1052
        imagedestroy($imageResource); // Clean up the image resource
1053
 
1054
        if ($save_success) {
1055
            if ($sourceFilenameToUnlink && file_exists($sourceFilenameToUnlink)) {
1056
                unlink($sourceFilenameToUnlink);
1057
            }
1058
            return true;
1059
        } else {
1060
            error_log("_savePngAndCleanup: imagepng failed to save image to {$targetFilename}.");
1061
            return false;
1062
        }
1063
    }
1064
 
1065
    /**
283 www 1066
     *
345 www 1067
     * @param string $source
1068
     * @param string $target_filename
1069
     * @param number $target_width
1070
     * @param number $target_height
1071
     * @return boolean
283 www 1072
     */
345 www 1073
    public function uploadImageResize($source_filename, $target_filename, $target_width, $target_height)
283 www 1074
    {
345 www 1075
        try {
1076
            $data = file_get_contents($source_filename);
1077
            $img = imagecreatefromstring($data);
546 ariadna 1078
 
1079
            if ($img) {
345 www 1080
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1081
 
345 www 1082
                $width_ratio = $target_width / $source_width;
1083
                $height_ratio = $target_height / $source_height;
546 ariadna 1084
 
1085
                if ($width_ratio > $height_ratio) {
345 www 1086
                    $resized_width = $target_width;
1087
                    $resized_height = $source_height * $width_ratio;
1088
                } else {
1089
                    $resized_height = $target_height;
1090
                    $resized_width = $source_width * $height_ratio;
1091
                }
546 ariadna 1092
 
345 www 1093
                $resized_width = round($resized_width);
1094
                $resized_height = round($resized_height);
546 ariadna 1095
 
553 stevensc 1096
                $new_image = $this->_createImageCanvas($target_width, $target_height);
1097
                if ($new_image === false) {
1098
                    imagedestroy($img);
1099
                    unlink($source_filename);
1100
                    return false;
1101
                }
1102
 
546 ariadna 1103
                imageCopyResampled($new_image, $img, 0, 0, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
553 stevensc 1104
                imagedestroy($img); // Source image resource can be destroyed after resampling
546 ariadna 1105
 
553 stevensc 1106
                return $this->_savePngAndCleanup($new_image, $target_filename, $source_filename);
345 www 1107
            }
546 ariadna 1108
 
553 stevensc 1109
            unlink($source_filename);
345 www 1110
            return false;
1111
        } catch (\Throwable $e) {
546 ariadna 1112
            error_log($e->getTraceAsString());
553 stevensc 1113
            if (isset($img) && ($img instanceof \GdImage || is_resource($img))) {
1114
                imagedestroy($img);
1115
            }
1116
            if (isset($new_image) && ($new_image instanceof \GdImage || is_resource($new_image))) {
1117
                imagedestroy($new_image);
668 ariadna 1118
            }
553 stevensc 1119
            if (isset($source_filename) && file_exists($source_filename)) {
668 ariadna 1120
                unlink($source_filename);
553 stevensc 1121
            }
345 www 1122
            return false;
1123
        }
546 ariadna 1124
    }
345 www 1125
 
546 ariadna 1126
 
333 www 1127
    /**
345 www 1128
     *
1129
     * @param string $source
1130
     * @param string $target_filename
1131
     * @param number $target_width
1132
     * @param number $target_height
1133
 
1134
     * @return boolean
334 www 1135
     */
345 www 1136
    public function uploadImageCrop($source_filename, $target_filename, $target_width, $target_height)
1137
    {
1138
        try {
1139
            $data = file_get_contents($source_filename);
1140
            $img = imagecreatefromstring($data);
546 ariadna 1141
 
1142
            if ($img) {
345 www 1143
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1144
 
345 www 1145
                $width_ratio = $target_width / $source_width;
1146
                $height_ratio = $target_height / $source_height;
546 ariadna 1147
                if ($width_ratio > $height_ratio) {
345 www 1148
                    $resized_width = $target_width;
1149
                    $resized_height = $source_height * $width_ratio;
1150
                } else {
1151
                    $resized_height = $target_height;
1152
                    $resized_width = $source_width * $height_ratio;
1153
                }
546 ariadna 1154
 
345 www 1155
                $resized_width = round($resized_width);
1156
                $resized_height = round($resized_height);
546 ariadna 1157
 
345 www 1158
                $offset_width = round(($target_width - $resized_width) / 2);
1159
                $offset_height = round(($target_height - $resized_height) / 2);
546 ariadna 1160
 
553 stevensc 1161
                $new_image = $this->_createImageCanvas($target_width, $target_height);
1162
                if ($new_image === false) {
1163
                    imagedestroy($img);
1164
                    unlink($source_filename);
1165
                    return false;
1166
                }
546 ariadna 1167
 
1168
                imageCopyResampled($new_image, $img, $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
553 stevensc 1169
                imagedestroy($img);
546 ariadna 1170
 
553 stevensc 1171
                return $this->_savePngAndCleanup($new_image, $target_filename, $source_filename);
546 ariadna 1172
            }
1173
 
553 stevensc 1174
            unlink($source_filename);
345 www 1175
            return false;
546 ariadna 1176
        } catch (\Throwable $e) {
1177
            error_log($e->getTraceAsString());
553 stevensc 1178
            if (isset($img) && ($img instanceof \GdImage || is_resource($img))) {
1179
                imagedestroy($img);
1180
            }
1181
            if (isset($new_image) && ($new_image instanceof \GdImage || is_resource($new_image))) {
1182
                imagedestroy($new_image);
1183
            }
1184
            if (isset($source_filename) && file_exists($source_filename)) {
1185
                unlink($source_filename);
1186
            }
345 www 1187
            return false;
1188
        }
334 www 1189
    }
546 ariadna 1190
 
334 www 1191
    /**
345 www 1192
     *
1193
     * @param string $source
1194
     * @param string $target_filename
1195
     * @return boolean
333 www 1196
     */
345 www 1197
    public function uploadImageWithOutChangeSize($source_filename, $target_filename)
333 www 1198
    {
345 www 1199
        try {
1200
            $data = file_get_contents($source_filename);
1201
            $img = imagecreatefromstring($data);
546 ariadna 1202
 
1203
            if ($img) {
345 www 1204
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1205
 
553 stevensc 1206
                $new_image = $this->_createImageCanvas($source_width, $source_height);
1207
                if ($new_image === false) {
1208
                    imagedestroy($img);
1209
                    unlink($source_filename);
1210
                    return false;
1211
                }
668 ariadna 1212
 
546 ariadna 1213
                imageCopyResampled($new_image, $img, 0, 0, 0, 0, $source_width, $source_height, $source_width, $source_height);
553 stevensc 1214
                imagedestroy($img);
546 ariadna 1215
 
553 stevensc 1216
                return $this->_savePngAndCleanup($new_image, $target_filename, $source_filename);
546 ariadna 1217
            }
1218
 
553 stevensc 1219
            unlink($source_filename);
345 www 1220
            return false;
1221
        } catch (\Throwable $e) {
546 ariadna 1222
            error_log($e->getTraceAsString());
553 stevensc 1223
            if (isset($img) && ($img instanceof \GdImage || is_resource($img))) {
1224
                imagedestroy($img);
1225
            }
1226
            if (isset($new_image) && ($new_image instanceof \GdImage || is_resource($new_image))) {
1227
                imagedestroy($new_image);
1228
            }
1229
            if (isset($source_filename) && file_exists($source_filename)) {
1230
                unlink($source_filename);
1231
            }
345 www 1232
            return false;
333 www 1233
        }
345 www 1234
    }
546 ariadna 1235
 
345 www 1236
    public static function extractPosterFromVideo($source_filename, $target_filename)
1237
    {
546 ariadna 1238
 
345 www 1239
        try {
1240
            $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width,duration  $source_filename";
1241
            $response   = trim(shell_exec($cmd));
546 ariadna 1242
 
345 www 1243
            $source_duration = 0;
1244
            $lines = explode("\n", $response);
1245
            foreach ($lines as $line) {
1246
                $line = trim(strtolower($line));
1247
                if (strpos($line, 'duration') !== false) {
1248
                    $values = explode('=', $line);
1249
                    $source_duration = intval(str_replace($values[1], '#', ''), 10);
1250
                }
1251
            }
546 ariadna 1252
 
1253
 
345 www 1254
            if ($source_duration == 0) {
1255
                $second_extract = '00:00:02';
1256
            } else {
1257
                if ($source_duration > 10) {
1258
                    $second_extract = '00:00:10';
1259
                } else {
1260
                    $second_extract = '00:00:02';
1261
                }
333 www 1262
            }
546 ariadna 1263
 
345 www 1264
            $cmd = "/usr/bin/ffmpeg -y -i $source_filename  -pix_fmt yuvj422p -an -ss $second_extract -f mjpeg -t 1 -r 1 -y  $target_filename";
1265
            exec($cmd);
546 ariadna 1266
 
345 www 1267
            return true;
546 ariadna 1268
        } catch (\Throwable $e) {
1269
            error_log($e->getTraceAsString());
1270
 
345 www 1271
            return false;
1272
        }
333 www 1273
    }
345 www 1274
 
1275
    /**
1276
     *
1277
     * @param string $str
1278
     * @return string
1279
     */
1280
    public function normalizeStringFilename($str = '')
1281
    {
1282
        $basename = substr($str, 0, strrpos($str, '.'));
1283
        $basename = str_replace('.', '-', $basename);
1284
 
1285
        $extension = substr($str, strrpos($str, '.'));
1286
 
1287
        $str = $basename . $extension;
1288
 
1289
        $str = strip_tags($str);
1290
        $str = preg_replace('/[\r\n\t ]+/', ' ', $str);
1291
        $str = preg_replace('/[\#\"\*\/\:\<\>\?\'\|\,]+/', ' ', $str);
1292
        $str = strtolower($str);
1293
        $str = html_entity_decode($str, ENT_QUOTES, "utf-8");
1294
        $str = htmlentities($str, ENT_QUOTES, "utf-8");
1295
        $str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
1296
        $str = str_replace(' ', '-', $str);
1297
        $str = rawurlencode($str);
1298
        $str = str_replace('%', '-', $str);
1299
        $str = str_replace([
1300
            '-----',
1301
            '----',
1302
            '---',
1303
            '--'
1304
        ], '-', $str);
1305
        return trim(strtolower($str));
1306
    }
546 ariadna 1307
}