Proyectos de Subversion LeadersLinked - Services

Rev

Rev 346 | Rev 549 | 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)) {
129
            mkdir($this->tempPath, 0775);
130
        }
131
 
132
        $this->tempPath = 'data' . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'tmp';
133
 
134
        if (! file_exists($this->tempPath)) {
135
            mkdir($this->tempPath, 0775);
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)) {
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) {
546 ariadna 490
            $remoto = $this->composePathToFilename(self::TYPE_GROUP, $group->uuid, $group->image);
345 www 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;
545
 
546 ariadna 546
 
547
 
548
 
549
 
345 www 550
        if (file_exists($remoto)) {
546 ariadna 551
 
552
            if ($code == 'd8e6c5de-52a6-4c28-bdce-4ba2823ba841') {
553
 
345 www 554
                error_log('getGenericImage = ' . $remoto);
555
            }
546 ariadna 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;
546 ariadna 574
 
575
 
576
 
345 www 577
        if (file_exists($remoto)) {
546 ariadna 578
            if ($code == 'd8e6c5de-52a6-4c28-bdce-4ba2823ba841') {
579
 
345 www 580
                error_log('getGenericFile = ' . $remoto);
581
            }
546 ariadna 582
 
583
 
333 www 584
            return $this->getPresignedUrl($remoto);
585
        }
345 www 586
        return;
333 www 587
    }
546 ariadna 588
 
345 www 589
    public function getPathDefault()
333 www 590
    {
345 www 591
        return $this->getPathByType(self::TYPE_DEFAULT);
333 www 592
    }
546 ariadna 593
 
345 www 594
    public function getPathChat()
320 www 595
    {
345 www 596
        return $this->getPathByType(self::TYPE_CHAT);
320 www 597
    }
546 ariadna 598
 
283 www 599
    public function getPathGroup()
600
    {
345 www 601
        return $this->getPathByType(self::TYPE_GROUP);
283 www 602
    }
546 ariadna 603
 
283 www 604
    public function getPathUser()
605
    {
345 www 606
        return $this->getPathByType(self::TYPE_USER);
283 www 607
    }
546 ariadna 608
 
283 www 609
    public function getPathImage()
610
    {
345 www 611
        return $this->getPathByType(self::TYPE_IMAGE);
283 www 612
    }
546 ariadna 613
 
283 www 614
    public function getPathJob()
615
    {
345 www 616
        return $this->getPathByType(self::TYPE_JOB);
283 www 617
    }
546 ariadna 618
 
283 www 619
    public function getPathCompany()
620
    {
345 www 621
        return $this->getPathByType(self::TYPE_COMPANY);
283 www 622
    }
546 ariadna 623
 
624
 
283 www 625
    public function getPathFeed()
626
    {
345 www 627
        return $this->getPathByType(self::TYPE_FEED);
283 www 628
    }
546 ariadna 629
 
345 www 630
    public function getPathMedia()
295 www 631
    {
345 www 632
        return $this->getPathByType(self::TYPE_MEDIA);
295 www 633
    }
546 ariadna 634
 
283 www 635
    public function getPathPost()
636
    {
345 www 637
        return $this->getPathByType(self::TYPE_POST);
283 www 638
    }
546 ariadna 639
 
640
 
283 www 641
    public function getPathMicrolearningTopic()
642
    {
345 www 643
        return $this->getPathByType(self::TYPE_MICROLEARNING_TOPIC);
283 www 644
    }
546 ariadna 645
 
283 www 646
    public function getPathMicrolearningCapsule()
647
    {
345 www 648
        return $this->getPathByType(self::TYPE_MICROLEARNING_CAPSULE);
283 www 649
    }
546 ariadna 650
 
283 www 651
    public function getPathMicrolearningSlide()
652
    {
345 www 653
        return $this->getPathByType(self::TYPE_MICROLEARNING_SLIDE);
283 www 654
    }
546 ariadna 655
 
283 www 656
    public function getPathJobDescription()
657
    {
345 www 658
        return $this->getPathByType(self::TYPE_JOB_DESCRIPTION);
283 www 659
    }
546 ariadna 660
 
295 www 661
    public function getPathSelfEvaluation()
283 www 662
    {
345 www 663
        return $this->getPathByType(self::TYPE_SELF_EVALUATION);
283 www 664
    }
546 ariadna 665
 
345 www 666
    public function getPathPerformanceEvaluation()
667
    {
668
        return $this->getPathByType(self::TYPE_PERFORMANCE_EVALUATION);
669
    }
546 ariadna 670
 
671
 
283 www 672
    public function getPathRecruitmentSelection()
673
    {
345 www 674
        return $this->getPathByType(self::TYPE_RECRUITMENT_SELECTION);
283 www 675
    }
546 ariadna 676
 
677
 
345 www 678
    public function getPathPlanningObjectivesAndGoals()
283 www 679
    {
345 www 680
        return $this->getPathByType(self::TYPE_PLANNING_OBJECTIVES_AND_GOALS);
283 www 681
    }
546 ariadna 682
 
345 www 683
    public function getPathMessage()
283 www 684
    {
345 www 685
        return $this->getPathByType(self::TYPE_MESSAGE);
283 www 686
    }
546 ariadna 687
 
688
 
283 www 689
    public function getPathSurvey()
690
    {
345 www 691
        return $this->getPathByType(self::TYPE_SURVEY);
283 www 692
    }
546 ariadna 693
 
694
 
283 www 695
    public function getPathNetwork()
696
    {
345 www 697
        return $this->getPathByType(self::TYPE_NETWORK);
283 www 698
    }
546 ariadna 699
 
700
 
345 www 701
    public function getPathDailyPulse()
283 www 702
    {
345 www 703
        return $this->getPathByType(self::TYPE_DAILY_PULSE);
283 www 704
    }
345 www 705
 
546 ariadna 706
 
345 www 707
    public function getPathEngagementReward()
283 www 708
    {
345 www 709
        return $this->getPathByType(self::TYPE_ENGAGEMENT_REWARD);
283 www 710
    }
546 ariadna 711
 
283 www 712
    public function getPathKnowledgeArea()
713
    {
345 www 714
        return $this->getPathByType(self::TYPE_KNOWLEDGE_AREA);
283 www 715
    }
546 ariadna 716
 
283 www 717
    public function getPathMyCoach()
718
    {
345 www 719
        return $this->getPathByType(self::TYPE_MY_COACH);
283 www 720
    }
546 ariadna 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;
546 ariadna 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
    }
546 ariadna 949
 
950
 
346 www 951
    /**
952
     *
345 www 953
     * @param string $url
954
     * @return string
955
     */
956
    public function getPresignedUrl($url)
957
    {
546 ariadna 958
 
959
 
960
        if (strpos($url, 'd8e6c5de-52a6-4c28-bdce-4ba2823ba841') !== false) {
345 www 961
            $show = true;
283 www 962
        } else {
345 www 963
            $show = false;
546 ariadna 964
        }
965
 
966
 
967
        if ($show) {
345 www 968
            error_log('getPresignedUrl : ' . $url);
283 www 969
        }
546 ariadna 970
 
345 www 971
        $code = hash('sha256', $url);
546 ariadna 972
 
973
        if ($show) {
345 www 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
 
546 ariadna 985
 
986
 
345 www 987
            $storageFileMapper->insert($storageFile);
334 www 988
        }
345 www 989
 
546 ariadna 990
 
345 www 991
        $url = 'https://' . $_SERVER['SERVER_NAME'] . '/storage/' . $code;
546 ariadna 992
        if ($show) {
345 www 993
            error_log('URL : ' .  $url);
994
        }
546 ariadna 995
 
345 www 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
    }
546 ariadna 1077
 
283 www 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 {
546 ariadna 1090
 
345 www 1091
            $data = file_get_contents($source_filename);
1092
            $img = imagecreatefromstring($data);
546 ariadna 1093
 
1094
 
1095
            if ($img) {
345 www 1096
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1097
 
1098
 
345 www 1099
                $width_ratio = $target_width / $source_width;
1100
                $height_ratio = $target_height / $source_height;
546 ariadna 1101
 
1102
                if ($width_ratio > $height_ratio) {
345 www 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
                }
546 ariadna 1109
 
1110
 
1111
 
345 www 1112
                $resized_width = round($resized_width);
1113
                $resized_height = round($resized_height);
546 ariadna 1114
 
345 www 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);
546 ariadna 1120
                imageCopyResampled($new_image, $img, 0, 0, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
1121
 
1122
 
345 www 1123
                imagepng($new_image, $target_filename);
546 ariadna 1124
 
345 www 1125
                @unlink($source_filename);
1126
                return true;
1127
            }
546 ariadna 1128
 
345 www 1129
            @unlink($source_filename);
1130
            return false;
1131
        } catch (\Throwable $e) {
546 ariadna 1132
            error_log($e->getTraceAsString());
1133
 
345 www 1134
            @unlink($source_filename);
1135
            return false;
1136
        }
546 ariadna 1137
    }
345 www 1138
 
546 ariadna 1139
 
333 www 1140
    /**
345 www 1141
     *
1142
     * @param string $source
1143
     * @param string $target_filename
1144
     * @param number $target_width
1145
     * @param number $target_height
1146
 
1147
     * @return boolean
334 www 1148
     */
345 www 1149
    public function uploadImageCrop($source_filename, $target_filename, $target_width, $target_height)
1150
    {
1151
 
1152
        try {
546 ariadna 1153
 
345 www 1154
            $data = file_get_contents($source_filename);
1155
            $img = imagecreatefromstring($data);
546 ariadna 1156
 
1157
 
1158
            if ($img) {
345 www 1159
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1160
 
345 www 1161
                $width_ratio = $target_width / $source_width;
1162
                $height_ratio = $target_height / $source_height;
546 ariadna 1163
                if ($width_ratio > $height_ratio) {
345 www 1164
                    $resized_width = $target_width;
1165
                    $resized_height = $source_height * $width_ratio;
1166
                } else {
1167
                    $resized_height = $target_height;
1168
                    $resized_width = $source_width * $height_ratio;
1169
                }
546 ariadna 1170
 
1171
 
1172
 
345 www 1173
                $resized_width = round($resized_width);
1174
                $resized_height = round($resized_height);
546 ariadna 1175
 
345 www 1176
                $offset_width = round(($target_width - $resized_width) / 2);
1177
                $offset_height = round(($target_height - $resized_height) / 2);
546 ariadna 1178
 
1179
 
345 www 1180
                $new_image = imageCreateTrueColor($target_width, $target_height);
1181
                imageAlphaBlending($new_image, False);
1182
                imageSaveAlpha($new_image, True);
1183
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
1184
                imagefill($new_image, 0, 0, $transparent);
546 ariadna 1185
                imageCopyResampled($new_image, $img, $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
1186
 
345 www 1187
                imagepng($new_image, $target_filename);
546 ariadna 1188
 
345 www 1189
                @unlink($source_filename);
1190
                return true;
546 ariadna 1191
            }
1192
 
345 www 1193
            @unlink($source_filename);
1194
            return false;
546 ariadna 1195
        } catch (\Throwable $e) {
1196
            error_log($e->getTraceAsString());
1197
 
345 www 1198
            @unlink($source_filename);
1199
            return false;
1200
        }
334 www 1201
    }
546 ariadna 1202
 
334 www 1203
    /**
345 www 1204
     *
1205
     * @param string $source
1206
     * @param string $target_filename
1207
     * @return boolean
333 www 1208
     */
345 www 1209
    public function uploadImageWithOutChangeSize($source_filename, $target_filename)
333 www 1210
    {
1211
 
345 www 1212
        try {
1213
            $data = file_get_contents($source_filename);
546 ariadna 1214
 
1215
 
345 www 1216
            $img = imagecreatefromstring($data);
546 ariadna 1217
 
1218
            if ($img) {
345 www 1219
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1220
 
345 www 1221
                $new_image = imageCreateTrueColor($source_width, $source_height);
1222
                imageAlphaBlending($new_image, False);
1223
                imageSaveAlpha($new_image, True);
1224
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
1225
                imagefill($new_image, 0, 0, $transparent);
546 ariadna 1226
                imageCopyResampled($new_image, $img, 0, 0, 0, 0, $source_width, $source_height, $source_width, $source_height);
1227
 
1228
 
345 www 1229
                imagepng($new_image, $target_filename);
546 ariadna 1230
 
345 www 1231
                @unlink($source_filename);
1232
                return true;
546 ariadna 1233
            }
1234
 
345 www 1235
            @unlink($source_filename);
1236
            return false;
1237
        } catch (\Throwable $e) {
546 ariadna 1238
            error_log($e->getTraceAsString());
1239
 
345 www 1240
            @unlink($source_filename);
1241
            return false;
333 www 1242
        }
345 www 1243
    }
546 ariadna 1244
 
345 www 1245
    public static function extractPosterFromVideo($source_filename, $target_filename)
1246
    {
546 ariadna 1247
 
345 www 1248
        try {
1249
            $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width,duration  $source_filename";
1250
            $response   = trim(shell_exec($cmd));
546 ariadna 1251
 
345 www 1252
            $source_duration = 0;
1253
            $lines = explode("\n", $response);
1254
            foreach ($lines as $line) {
1255
                $line = trim(strtolower($line));
1256
                if (strpos($line, 'duration') !== false) {
1257
                    $values = explode('=', $line);
1258
                    $source_duration = intval(str_replace($values[1], '#', ''), 10);
1259
                }
1260
            }
546 ariadna 1261
 
1262
 
345 www 1263
            if ($source_duration == 0) {
1264
                $second_extract = '00:00:02';
1265
            } else {
1266
                if ($source_duration > 10) {
1267
                    $second_extract = '00:00:10';
1268
                } else {
1269
                    $second_extract = '00:00:02';
1270
                }
333 www 1271
            }
546 ariadna 1272
 
345 www 1273
            $cmd = "/usr/bin/ffmpeg -y -i $source_filename  -pix_fmt yuvj422p -an -ss $second_extract -f mjpeg -t 1 -r 1 -y  $target_filename";
1274
            exec($cmd);
546 ariadna 1275
 
345 www 1276
            return true;
546 ariadna 1277
        } catch (\Throwable $e) {
1278
            error_log($e->getTraceAsString());
1279
 
345 www 1280
            return false;
1281
        }
333 www 1282
    }
345 www 1283
 
1284
    /**
1285
     *
1286
     * @param string $str
1287
     * @return string
1288
     */
1289
    public function normalizeStringFilename($str = '')
1290
    {
1291
        $basename = substr($str, 0, strrpos($str, '.'));
1292
        $basename = str_replace('.', '-', $basename);
1293
 
1294
        $extension = substr($str, strrpos($str, '.'));
1295
 
1296
        $str = $basename . $extension;
1297
 
1298
        $str = strip_tags($str);
1299
        $str = preg_replace('/[\r\n\t ]+/', ' ', $str);
1300
        $str = preg_replace('/[\#\"\*\/\:\<\>\?\'\|\,]+/', ' ', $str);
1301
        $str = strtolower($str);
1302
        $str = html_entity_decode($str, ENT_QUOTES, "utf-8");
1303
        $str = htmlentities($str, ENT_QUOTES, "utf-8");
1304
        $str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
1305
        $str = str_replace(' ', '-', $str);
1306
        $str = rawurlencode($str);
1307
        $str = str_replace('%', '-', $str);
1308
        $str = str_replace([
1309
            '-----',
1310
            '----',
1311
            '---',
1312
            '--'
1313
        ], '-', $str);
1314
        return trim(strtolower($str));
1315
    }
546 ariadna 1316
}