Proyectos de Subversion LeadersLinked - Services

Rev

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