Proyectos de Subversion LeadersLinked - Backend

Rev

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

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