Proyectos de Subversion LeadersLinked - Services

Rev

Rev 549 | Rev 553 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
283 www 1
<?php
546 ariadna 2
 
283 www 3
namespace LeadersLinked\Library;
4
 
5
use LeadersLinked\Model\User;
6
use LeadersLinked\Model\UserProfile;
7
use LeadersLinked\Model\Company;
8
use LeadersLinked\Model\Group;
9
 
345 www 10
class Storage
283 www 11
{
345 www 12
 
13
    const FILE_TYPE_IMAGE = 'image';
14
 
15
    const FILE_TYPE_VIDEO = 'video';
16
 
17
    const FILE_TYPE_DOCUMENT = 'document';
18
 
19
    const TYPE_DEFAULT = 'default';
20
 
21
    const TYPE_CHAT = 'chat';
22
 
23
    const TYPE_GROUP = 'group';
24
 
25
    const TYPE_USER = 'user';
26
 
27
    const TYPE_IMAGE = 'image';
28
 
29
    const TYPE_JOB = 'job';
30
 
31
    const TYPE_MEDIA = 'media';
32
 
33
    const TYPE_COMPANY = 'company';
34
 
35
    const TYPE_FEED = 'feed';
36
 
37
    const TYPE_POST = 'post';
38
 
39
    const TYPE_MICROLEARNING_TOPIC = 'topic';
40
 
41
    const TYPE_MICROLEARNING_CAPSULE = 'capsule';
42
 
43
    const TYPE_MICROLEARNING_SLIDE = 'slide';
44
 
45
    const TYPE_JOB_DESCRIPTION = 'jobdesc';
46
 
47
    const TYPE_SELF_EVALUATION = 'selfval';
48
 
49
    const TYPE_PERFORMANCE_EVALUATION = 'perfeva';
50
 
51
    const TYPE_RECRUITMENT_SELECTION = 'recrsel';
52
 
53
    const TYPE_PLANNING_OBJECTIVES_AND_GOALS = 'plannig';
54
 
55
    const TYPE_MESSAGE = 'message';
56
 
57
    const TYPE_SURVEY = 'survey';
58
 
59
    const TYPE_NETWORK = 'network';
60
 
61
    const TYPE_DAILY_PULSE = 'dailpuls';
62
 
63
    const TYPE_ENGAGEMENT_REWARD = 'engarewr';
64
 
65
    const TYPE_KNOWLEDGE_AREA = 'knowarea';
66
 
67
    const TYPE_MY_COACH = 'mycoach';
68
 
69
    const TYPE_HABIT_EMOJI = 'habit-emoji';
70
 
71
    const TYPE_HABIT_CONTENT = 'habit-content';
72
 
283 www 73
    /**
345 www 74
     *
75
     * @var\LeadersLinked\Library\Storage
283 www 76
     */
77
    private static $_instance;
345 www 78
 
283 www 79
    /**
345 www 80
     *
283 www 81
     * @var array
82
     */
83
    private $config;
345 www 84
 
283 www 85
    /**
345 www 86
     *
87
     * @var \Laminas\Db\Adapter\AdapterInterface
283 www 88
     */
333 www 89
    private $adapter;
345 www 90
 
91
    /**
92
     *
93
     * @var string
94
     */
95
    private $tempPath;
96
 
97
    /**
98
     *
99
     * @var string
100
     */
101
    private $storagePath;
102
 
103
    /**
104
     *
105
     * @var array
106
     */
107
    private $currentFile;
546 ariadna 108
 
109
 
345 www 110
    /**
111
     *
112
     * @var array
113
     */
114
    private $files;
333 www 115
 
283 www 116
    /**
345 www 117
     *
283 www 118
     * @param array $config
333 www 119
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
283 www 120
     */
345 www 121
    private function __construct($config, $adapter)
283 www 122
    {
345 www 123
        $this->config = $config;
124
        $this->adapter = $adapter;
125
        $this->currentFile = [];
126
 
127
        $this->storagePath = 'data' . DIRECTORY_SEPARATOR . 'storage';
128
        if (! file_exists($this->storagePath)) {
549 stevensc 129
            mkdir($this->storagePath, 0775, true);
345 www 130
        }
131
 
132
        $this->tempPath = 'data' . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'tmp';
133
 
134
        if (! file_exists($this->tempPath)) {
549 stevensc 135
            mkdir($this->tempPath, 0775, true);
345 www 136
        }
283 www 137
    }
345 www 138
 
283 www 139
    /**
345 www 140
     *
283 www 141
     * @param array $config
333 www 142
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
283 www 143
     * @return \LeadersLinked\Library\Storage
144
     */
333 www 145
    public static function getInstance($config, $adapter)
283 www 146
    {
345 www 147
        if (self::$_instance == null) {
333 www 148
            self::$_instance = new Storage($config, $adapter);
283 www 149
        }
345 www 150
 
283 www 151
        return self::$_instance;
152
    }
345 www 153
 
154
    /**
155
     *
156
     * @return string
157
     */
158
    public function getFullPathImageDefault()
159
    {
160
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image.jpg';
161
    }
162
 
163
    /**
164
     *
165
     * @return string
166
     */
167
    public function getFullPathImageUserDefault()
168
    {
169
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-user.jpg';
170
    }
171
 
172
    /**
173
     *
174
     * @return string
175
     */
176
    public function getFullPathImageUserPofileDefault()
177
    {
178
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-user-profile.png';
179
    }
180
 
181
    /**
182
     *
183
     * @return string
184
     */
185
    public function getFullPathImageUserCoverDefault()
186
    {
187
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-cover.jpg';
188
    }
189
 
190
    /**
191
     *
192
     * @return string
193
     */
194
    public function getFullPathImageCompanyDefault()
195
    {
196
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-company.jpg';
197
    }
198
 
199
    /**
200
     *
201
     * @return string
202
     */
203
    public function getFullPathImageCompanyCoverDefault()
204
    {
205
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-cover.jpg';
206
    }
207
 
208
    /**
209
     *
210
     * @return string
211
     */
212
    public function getFullPathImageGroupDefault()
213
    {
214
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-group.jpg';
215
    }
216
 
217
    /**
218
     *
219
     * @return string
220
     */
221
    public function getFullPathImageGroupCoverDefault()
222
    {
223
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-cover.jpg';
224
    }
225
 
226
    /**
227
     *
228
     * @return string
229
     */
230
    public function getFullPathImageCompanyHeaderPdfDefault()
231
    {
232
        return $this->storagePath . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'company-header.png';
233
    }
234
 
235
    /**
236
     *
237
     * @return string
238
     */
239
    public function getFullPathImageCompanyFooterPdfDefault()
240
    {
241
        return $this->storagePath . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'company-footer.png';
242
    }
243
 
244
    /**
245
     *
246
     * @return string
247
     */
248
    public function getStoagePath()
249
    {
250
        return $this->storagePath;
251
    }
252
 
253
    /**
254
     *
255
     * @return string
256
     */
257
    public function getTempPath()
258
    {
259
        return $this->tempPath;
260
    }
261
 
262
    /**
263
     *
264
     * @return array
265
     */
266
    public function getCurrentFile()
267
    {
268
        return $this->currentFile;
269
    }
270
 
271
    /**
272
     *
273
     * @param array $currentFile
274
     */
275
    public function setCurrentFile($currentFile)
276
    {
277
        $this->currentFile = $currentFile;
278
    }
279
 
280
    /**
281
     *
282
     * @param array $filename
283
     */
284
    public function setCurrentFilename($filename)
285
    {
546 ariadna 286
        if (empty($this->files)) {
345 www 287
            if (isset($_FILES[$filename]) && empty($_FILES[$filename]['error'])) {
288
                $this->currentFile = $_FILES[$filename];
289
            } else {
290
                $this->currentFile = [];
291
            }
292
        } else {
293
            if (isset($this->files[$filename]) && empty($this->files[$filename]['error'])) {
294
                $this->currentFile = $this->files[$filename];
295
            } else {
296
                $this->currentFile = [];
297
            }
298
        }
546 ariadna 299
 
345 www 300
        return !empty($this->currentFile);
301
    }
546 ariadna 302
 
283 www 303
    /**
304
     *
345 www 305
     * @return boolean
306
     */
307
    public function hasFile()
308
    {
309
        return !empty($this->currentFile);
310
    }
546 ariadna 311
 
345 www 312
    /**
313
     *
314
     * @param array $files
315
     */
316
    public function setFiles($files)
317
    {
318
        $this->files = $files;
319
    }
320
 
321
    /**
322
     *
323
     * @param string $type
324
     * @param string $code
325
     * @param string $filename
326
     * @return string
327
     */
328
    public function composePathToFilename($type, $code, $filename)
329
    {
330
        $path = $this->getPathByType($type);
331
        $path = $path . DIRECTORY_SEPARATOR . $code;
332
        if (! file_exists($path)) {
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
        if (file_exists($remoto)) {
333 www 547
            return $this->getPresignedUrl($remoto);
283 www 548
        }
345 www 549
 
550
        $remoto = $this->getFullPathImageDefault();
551
        return $this->getPresignedUrl($remoto);
283 www 552
    }
334 www 553
 
554
    /**
555
     *
556
     * @param string $path
557
     * @param string $code,
558
     * @param string $filename
559
     * @return string
560
     */
345 www 561
    public function getGenericFile($path, $code, $filename)
334 www 562
    {
345 www 563
        $remoto = $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;
546 ariadna 564
 
565
 
566
 
345 www 567
        if (file_exists($remoto)) {
546 ariadna 568
            if ($code == 'd8e6c5de-52a6-4c28-bdce-4ba2823ba841') {
569
 
345 www 570
                error_log('getGenericFile = ' . $remoto);
571
            }
546 ariadna 572
 
573
 
333 www 574
            return $this->getPresignedUrl($remoto);
575
        }
345 www 576
        return;
333 www 577
    }
546 ariadna 578
 
345 www 579
    public function getPathDefault()
333 www 580
    {
345 www 581
        return $this->getPathByType(self::TYPE_DEFAULT);
333 www 582
    }
546 ariadna 583
 
345 www 584
    public function getPathChat()
320 www 585
    {
345 www 586
        return $this->getPathByType(self::TYPE_CHAT);
320 www 587
    }
546 ariadna 588
 
283 www 589
    public function getPathGroup()
590
    {
345 www 591
        return $this->getPathByType(self::TYPE_GROUP);
283 www 592
    }
546 ariadna 593
 
283 www 594
    public function getPathUser()
595
    {
345 www 596
        return $this->getPathByType(self::TYPE_USER);
283 www 597
    }
546 ariadna 598
 
283 www 599
    public function getPathImage()
600
    {
345 www 601
        return $this->getPathByType(self::TYPE_IMAGE);
283 www 602
    }
546 ariadna 603
 
283 www 604
    public function getPathJob()
605
    {
345 www 606
        return $this->getPathByType(self::TYPE_JOB);
283 www 607
    }
546 ariadna 608
 
283 www 609
    public function getPathCompany()
610
    {
345 www 611
        return $this->getPathByType(self::TYPE_COMPANY);
283 www 612
    }
546 ariadna 613
 
614
 
283 www 615
    public function getPathFeed()
616
    {
345 www 617
        return $this->getPathByType(self::TYPE_FEED);
283 www 618
    }
546 ariadna 619
 
345 www 620
    public function getPathMedia()
295 www 621
    {
345 www 622
        return $this->getPathByType(self::TYPE_MEDIA);
295 www 623
    }
546 ariadna 624
 
283 www 625
    public function getPathPost()
626
    {
345 www 627
        return $this->getPathByType(self::TYPE_POST);
283 www 628
    }
546 ariadna 629
 
630
 
283 www 631
    public function getPathMicrolearningTopic()
632
    {
345 www 633
        return $this->getPathByType(self::TYPE_MICROLEARNING_TOPIC);
283 www 634
    }
546 ariadna 635
 
283 www 636
    public function getPathMicrolearningCapsule()
637
    {
345 www 638
        return $this->getPathByType(self::TYPE_MICROLEARNING_CAPSULE);
283 www 639
    }
546 ariadna 640
 
283 www 641
    public function getPathMicrolearningSlide()
642
    {
345 www 643
        return $this->getPathByType(self::TYPE_MICROLEARNING_SLIDE);
283 www 644
    }
546 ariadna 645
 
283 www 646
    public function getPathJobDescription()
647
    {
345 www 648
        return $this->getPathByType(self::TYPE_JOB_DESCRIPTION);
283 www 649
    }
546 ariadna 650
 
295 www 651
    public function getPathSelfEvaluation()
283 www 652
    {
345 www 653
        return $this->getPathByType(self::TYPE_SELF_EVALUATION);
283 www 654
    }
546 ariadna 655
 
345 www 656
    public function getPathPerformanceEvaluation()
657
    {
658
        return $this->getPathByType(self::TYPE_PERFORMANCE_EVALUATION);
659
    }
546 ariadna 660
 
661
 
283 www 662
    public function getPathRecruitmentSelection()
663
    {
345 www 664
        return $this->getPathByType(self::TYPE_RECRUITMENT_SELECTION);
283 www 665
    }
546 ariadna 666
 
667
 
345 www 668
    public function getPathPlanningObjectivesAndGoals()
283 www 669
    {
345 www 670
        return $this->getPathByType(self::TYPE_PLANNING_OBJECTIVES_AND_GOALS);
283 www 671
    }
546 ariadna 672
 
345 www 673
    public function getPathMessage()
283 www 674
    {
345 www 675
        return $this->getPathByType(self::TYPE_MESSAGE);
283 www 676
    }
546 ariadna 677
 
678
 
283 www 679
    public function getPathSurvey()
680
    {
345 www 681
        return $this->getPathByType(self::TYPE_SURVEY);
283 www 682
    }
546 ariadna 683
 
684
 
283 www 685
    public function getPathNetwork()
686
    {
345 www 687
        return $this->getPathByType(self::TYPE_NETWORK);
283 www 688
    }
546 ariadna 689
 
690
 
345 www 691
    public function getPathDailyPulse()
283 www 692
    {
345 www 693
        return $this->getPathByType(self::TYPE_DAILY_PULSE);
283 www 694
    }
345 www 695
 
546 ariadna 696
 
345 www 697
    public function getPathEngagementReward()
283 www 698
    {
345 www 699
        return $this->getPathByType(self::TYPE_ENGAGEMENT_REWARD);
283 www 700
    }
546 ariadna 701
 
283 www 702
    public function getPathKnowledgeArea()
703
    {
345 www 704
        return $this->getPathByType(self::TYPE_KNOWLEDGE_AREA);
283 www 705
    }
546 ariadna 706
 
283 www 707
    public function getPathMyCoach()
708
    {
345 www 709
        return $this->getPathByType(self::TYPE_MY_COACH);
283 www 710
    }
546 ariadna 711
 
345 www 712
    public function getPathHabitEmoji()
283 www 713
    {
345 www 714
        return $this->getPathByType(self::TYPE_HABIT_EMOJI);
283 www 715
    }
345 www 716
 
717
    public function getPathHabitContent()
283 www 718
    {
345 www 719
        return $this->getPathByType(self::TYPE_HABIT_CONTENT);
720
    }
721
 
722
 
723
    public function getPathByType($type)
724
    {
725
        switch ($type) {
726
            case self::TYPE_DEFAULT:
727
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'default';
728
                break;
729
 
730
            case self::TYPE_CHAT:
731
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'chat';
732
                break;
733
 
734
            case self::TYPE_GROUP:
735
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'group';
736
                break;
737
 
738
            case self::TYPE_USER:
739
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'user';
740
                break;
741
 
742
            case self::TYPE_IMAGE:
743
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'image';
744
                break;
745
 
746
            case self::TYPE_JOB:
747
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'job';
748
                break;
749
 
750
            case self::TYPE_COMPANY:
751
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'company';
752
                break;
753
 
754
            case self::TYPE_FEED:
755
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'feed';
756
                break;
757
 
758
            case self::TYPE_MEDIA:
759
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'media';
760
                break;
761
 
762
            case self::TYPE_POST:
763
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'post';
764
                break;
765
 
766
            case self::TYPE_MICROLEARNING_TOPIC:
767
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'topic';
768
                break;
769
 
770
            case self::TYPE_MICROLEARNING_CAPSULE:
771
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'capsule';
772
                break;
773
 
774
            case self::TYPE_MICROLEARNING_SLIDE:
775
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'slide';
776
                break;
777
 
778
            case self::TYPE_JOB_DESCRIPTION:
779
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'job-description';
780
                break;
781
 
782
            case self::TYPE_SELF_EVALUATION:
783
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'self-evaluation';
784
                break;
785
 
786
            case self::TYPE_PERFORMANCE_EVALUATION:
787
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'performance-evaluation';
788
                break;
789
 
790
            case self::TYPE_RECRUITMENT_SELECTION:
791
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'recruitment-selection';
792
                break;
793
 
794
            case self::TYPE_PLANNING_OBJECTIVES_AND_GOALS:
795
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'planning-objectives-and-goals';
796
                break;
797
 
798
            case self::TYPE_MESSAGE:
799
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'message';
800
                break;
801
 
802
            case self::TYPE_SURVEY:
803
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'survey';
804
                break;
805
 
806
            case self::TYPE_NETWORK:
807
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'network';
808
                break;
809
 
810
            case self::TYPE_DAILY_PULSE:
811
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'daily-pulse';
812
                break;
813
 
814
            case self::TYPE_ENGAGEMENT_REWARD:
815
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'engagement-reward';
816
                break;
817
 
818
            case self::TYPE_KNOWLEDGE_AREA:
819
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'knowledge-area';
820
                break;
821
 
822
            case self::TYPE_MY_COACH:
823
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'my-coach';
824
                break;
825
 
826
            case self::TYPE_HABIT_EMOJI:
827
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'habit' . DIRECTORY_SEPARATOR . 'emoji';
828
                break;
829
 
830
            case self::TYPE_HABIT_CONTENT:
831
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'habit' . DIRECTORY_SEPARATOR . 'content';
832
                break;
833
 
834
            default:
835
                $path = $this->storagePath;
836
                break;
334 www 837
        }
345 www 838
 
839
        if (! file_exists($path)) {
840
            mkdir($path, 0775, true);
841
        }
842
 
843
        return $path;
283 www 844
    }
345 www 845
 
283 www 846
    /**
847
     *
848
     * @param string $path
849
     * @param string $code
850
     * @param string $filename
345 www 851
     * @return string
283 www 852
     */
853
    public function deleteFile($path, $code, $filename)
854
    {
345 www 855
        $remoto = $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;
546 ariadna 856
        if (file_exists($filename)) {
333 www 857
            return @unlink($remoto);
283 www 858
        } else {
859
            return true;
860
        }
861
    }
345 www 862
 
295 www 863
    /**
864
     *
549 stevensc 865
     * @param string $type
295 www 866
     * @param string $code
867
     * @return boolean
868
     */
549 stevensc 869
    public function deleteDirectory($type, $code)
295 www 870
    {
549 stevensc 871
        $path = $this->getPathByType($type);
872
        $remoto = $path . DIRECTORY_SEPARATOR . $code;
345 www 873
        if (file_exists($remoto)) {
334 www 874
            $this->deleteDirectoryRecursive($remoto);
875
            return true;
295 www 876
        } else {
877
            return true;
878
        }
879
    }
345 www 880
 
334 www 881
    /**
882
     *
883
     * @param string $dir
884
     */
345 www 885
    private function deleteDirectoryRecursive($dir)
886
    {
334 www 887
        if (is_dir($dir)) {
888
            $objects = scandir($dir);
889
            foreach ($objects as $object) {
345 www 890
                if ($object != '.' && $object != '..') {
891
                    $s = $dir . DIRECTORY_SEPARATOR . $object;
892
                    if (is_dir($s) && ! is_link($s)) {
549 stevensc 893
                        $this->deleteDirectoryRecursive($s);
345 www 894
                    } else {
895
                        @unlink($s);
334 www 896
                    }
897
                }
898
            }
899
            @rmdir($dir);
900
        }
901
    }
283 www 902
 
903
    /**
904
     *
905
     * @param string $path
906
     * @param string $code
290 www 907
     * @param string $local_filename
283 www 908
     * @return boolean
909
     */
345 www 910
    public function putFile($source_file, $target_file)
283 www 911
    {
345 www 912
        return @rename($source_file, $target_file);
913
    }
914
 
915
    /**
916
     *
917
     * @param string $tempfile
918
     * @param string $path
919
     * @param string $code
920
     * @param string $filename
921
     * @return boolean
922
     */
923
    public function moveUploadedFile($source_file, $target_file)
924
    {
346 www 925
        return @move_uploaded_file($source_file, $target_file);
345 www 926
    }
927
 
928
    /**
929
     *
346 www 930
     * @param string $tempfile
931
     * @param string $path
932
     * @param string $code
933
     * @param string $filename
934
     * @return boolean
935
     */
936
    public function copyFile($source_file, $target_file)
937
    {
938
        return @copy($source_file, $target_file);
939
    }
546 ariadna 940
 
941
 
346 www 942
    /**
943
     *
345 www 944
     * @param string $url
945
     * @return string
946
     */
947
    public function getPresignedUrl($url)
948
    {
949
        $code = hash('sha256', $url);
546 ariadna 950
 
345 www 951
        $storageFileMapper = \LeadersLinked\Mapper\StorageFileMapper::getInstance($this->adapter);
952
        $storageFile = $storageFileMapper->fetchOneByCode($code);
953
        if (! $storageFile) {
954
            $storageFile = new \LeadersLinked\Model\StorageFile();
955
            $storageFile->code = $code;
956
            $storageFile->path = $url;
957
            $storageFile->salt = \LeadersLinked\Library\Functions::generatePassword(8);
958
 
546 ariadna 959
 
960
 
345 www 961
            $storageFileMapper->insert($storageFile);
334 www 962
        }
345 www 963
 
546 ariadna 964
 
345 www 965
        $url = 'https://' . $_SERVER['SERVER_NAME'] . '/storage/' . $code;
549 stevensc 966
 
345 www 967
        return $url;
968
    }
290 www 969
 
345 www 970
    /**
971
     *
972
     * @return @return void|string
973
     */
974
    public function getFilenamePNG()
975
    {
976
        if ($this->currentFile) {
977
            $filename = $this->normalizeStringFilename($this->currentFile['name']);
978
            $path_parts = pathinfo($filename);
979
            return $path_parts['filename'] . '.png';
980
        } else {
981
            return;
982
        }
983
    }
333 www 984
 
345 www 985
    /**
986
     *
987
     * @return @return void|string
988
     */
989
    public function getTmpFilename()
990
    {
991
        if ($this->currentFile) {
992
            return $this->currentFile['tmp_name'];
993
        } else {
994
            return;
995
        }
283 www 996
    }
345 www 997
 
283 www 998
    /**
999
     *
345 www 1000
     * @return @return void|string
283 www 1001
     */
345 www 1002
    public function getFilename()
283 www 1003
    {
345 www 1004
        if ($this->currentFile) {
1005
            return $this->normalizeStringFilename($this->currentFile['name']);
283 www 1006
        } else {
345 www 1007
            return;
283 www 1008
        }
345 www 1009
    }
334 www 1010
 
345 www 1011
    /**
1012
     *
1013
     * @return void|string
1014
     */
1015
    public function getFileType()
1016
    {
1017
        if ($this->currentFile) {
1018
            $tmp_name = $this->currentFile['tmp_name'];
1019
            $mime_type = mime_content_type($tmp_name);
1020
            if ($mime_type == 'image/jpg' || $mime_type == 'image/jpeg' || $mime_type == 'image/png') {
1021
                return self::FILE_TYPE_IMAGE;
1022
            } else if ($mime_type == 'video/quicktime' || $mime_type == 'video/webm' || $mime_type == 'video/mpeg' || $mime_type == 'video/mpg' || $mime_type == 'video/mp4') {
1023
                return self::FILE_TYPE_VIDEO;
1024
            } else if ($mime_type == 'application/pdf') {
1025
                return self::FILE_TYPE_DOCUMENT;
1026
            } else {
1027
                return;
1028
            }
1029
        } else {
1030
            return;
334 www 1031
        }
283 www 1032
    }
345 www 1033
 
1034
    /**
1035
     *
1036
     * @return void|string
1037
     */
1038
    public function getExtension()
1039
    {
1040
        if ($this->currentFile) {
1041
            $path_parts = pathinfo($this->currentFile['name']);
1042
            $ext = $path_parts['extension'];
1043
            return $ext;
1044
        } else {
1045
            return;
1046
        }
1047
    }
546 ariadna 1048
 
283 www 1049
    /**
1050
     *
345 www 1051
     * @param string $source
1052
     * @param string $target_filename
1053
     * @param number $target_width
1054
     * @param number $target_height
1055
     * @return boolean
283 www 1056
     */
345 www 1057
    public function uploadImageResize($source_filename, $target_filename, $target_width, $target_height)
283 www 1058
    {
345 www 1059
 
1060
        try {
546 ariadna 1061
 
345 www 1062
            $data = file_get_contents($source_filename);
1063
            $img = imagecreatefromstring($data);
546 ariadna 1064
 
1065
 
1066
            if ($img) {
345 www 1067
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1068
 
1069
 
345 www 1070
                $width_ratio = $target_width / $source_width;
1071
                $height_ratio = $target_height / $source_height;
546 ariadna 1072
 
1073
                if ($width_ratio > $height_ratio) {
345 www 1074
                    $resized_width = $target_width;
1075
                    $resized_height = $source_height * $width_ratio;
1076
                } else {
1077
                    $resized_height = $target_height;
1078
                    $resized_width = $source_width * $height_ratio;
1079
                }
546 ariadna 1080
 
1081
 
1082
 
345 www 1083
                $resized_width = round($resized_width);
1084
                $resized_height = round($resized_height);
546 ariadna 1085
 
345 www 1086
                $new_image = imageCreateTrueColor($target_width, $target_height);
1087
                imageAlphaBlending($new_image, False);
1088
                imageSaveAlpha($new_image, True);
1089
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
1090
                imagefill($new_image, 0, 0, $transparent);
546 ariadna 1091
                imageCopyResampled($new_image, $img, 0, 0, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
1092
 
1093
 
345 www 1094
                imagepng($new_image, $target_filename);
546 ariadna 1095
 
552 stevensc 1096
                // @unlink($source_filename); // Temporarily commented out for debugging
345 www 1097
                return true;
1098
            }
546 ariadna 1099
 
552 stevensc 1100
            // @unlink($source_filename); // Temporarily commented out for debugging
345 www 1101
            return false;
1102
        } catch (\Throwable $e) {
552 stevensc 1103
            error_log('Error in uploadImageResize: ' . $e->getMessage() . ' at ' . $e->getFile() . ':' . $e->getLine()); // More detailed log
546 ariadna 1104
            error_log($e->getTraceAsString());
1105
 
552 stevensc 1106
            // @unlink($source_filename); // Temporarily commented out for debugging
345 www 1107
            return false;
1108
        }
546 ariadna 1109
    }
345 www 1110
 
546 ariadna 1111
 
333 www 1112
    /**
345 www 1113
     *
1114
     * @param string $source
1115
     * @param string $target_filename
1116
     * @param number $target_width
1117
     * @param number $target_height
1118
 
1119
     * @return boolean
334 www 1120
     */
345 www 1121
    public function uploadImageCrop($source_filename, $target_filename, $target_width, $target_height)
1122
    {
1123
 
1124
        try {
546 ariadna 1125
 
345 www 1126
            $data = file_get_contents($source_filename);
1127
            $img = imagecreatefromstring($data);
546 ariadna 1128
 
1129
 
1130
            if ($img) {
345 www 1131
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1132
 
345 www 1133
                $width_ratio = $target_width / $source_width;
1134
                $height_ratio = $target_height / $source_height;
546 ariadna 1135
                if ($width_ratio > $height_ratio) {
345 www 1136
                    $resized_width = $target_width;
1137
                    $resized_height = $source_height * $width_ratio;
1138
                } else {
1139
                    $resized_height = $target_height;
1140
                    $resized_width = $source_width * $height_ratio;
1141
                }
546 ariadna 1142
 
1143
 
1144
 
345 www 1145
                $resized_width = round($resized_width);
1146
                $resized_height = round($resized_height);
546 ariadna 1147
 
345 www 1148
                $offset_width = round(($target_width - $resized_width) / 2);
1149
                $offset_height = round(($target_height - $resized_height) / 2);
546 ariadna 1150
 
1151
 
345 www 1152
                $new_image = imageCreateTrueColor($target_width, $target_height);
1153
                imageAlphaBlending($new_image, False);
1154
                imageSaveAlpha($new_image, True);
1155
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
1156
                imagefill($new_image, 0, 0, $transparent);
546 ariadna 1157
                imageCopyResampled($new_image, $img, $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
1158
 
345 www 1159
                imagepng($new_image, $target_filename);
546 ariadna 1160
 
345 www 1161
                @unlink($source_filename);
1162
                return true;
546 ariadna 1163
            }
1164
 
345 www 1165
            @unlink($source_filename);
1166
            return false;
546 ariadna 1167
        } catch (\Throwable $e) {
1168
            error_log($e->getTraceAsString());
1169
 
345 www 1170
            @unlink($source_filename);
1171
            return false;
1172
        }
334 www 1173
    }
546 ariadna 1174
 
334 www 1175
    /**
345 www 1176
     *
1177
     * @param string $source
1178
     * @param string $target_filename
1179
     * @return boolean
333 www 1180
     */
345 www 1181
    public function uploadImageWithOutChangeSize($source_filename, $target_filename)
333 www 1182
    {
1183
 
345 www 1184
        try {
1185
            $data = file_get_contents($source_filename);
546 ariadna 1186
 
1187
 
345 www 1188
            $img = imagecreatefromstring($data);
546 ariadna 1189
 
1190
            if ($img) {
345 www 1191
                list($source_width, $source_height) = getimagesize($source_filename);
546 ariadna 1192
 
345 www 1193
                $new_image = imageCreateTrueColor($source_width, $source_height);
1194
                imageAlphaBlending($new_image, False);
1195
                imageSaveAlpha($new_image, True);
1196
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
1197
                imagefill($new_image, 0, 0, $transparent);
546 ariadna 1198
                imageCopyResampled($new_image, $img, 0, 0, 0, 0, $source_width, $source_height, $source_width, $source_height);
1199
 
1200
 
345 www 1201
                imagepng($new_image, $target_filename);
546 ariadna 1202
 
345 www 1203
                @unlink($source_filename);
1204
                return true;
546 ariadna 1205
            }
1206
 
345 www 1207
            @unlink($source_filename);
1208
            return false;
1209
        } catch (\Throwable $e) {
546 ariadna 1210
            error_log($e->getTraceAsString());
1211
 
345 www 1212
            @unlink($source_filename);
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
}