Proyectos de Subversion LeadersLinked - Services

Rev

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