Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

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