Proyectos de Subversion LeadersLinked - Services

Rev

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