Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev Autor Línea Nro. Línea
283 www 1
<?php
2
namespace LeadersLinked\Library;
3
 
4
use LeadersLinked\Model\User;
5
use LeadersLinked\Model\UserProfile;
6
use LeadersLinked\Model\Company;
7
use LeadersLinked\Model\Group;
8
 
9
class Storage
10
{
11
    /**
12
     *
13
     * @var\LeadersLinked\Library\Storage
14
     */
15
    private static $_instance;
16
 
17
    /**
18
     *
19
     * @var array
20
     */
21
    private $config;
22
 
23
 
24
    /**
25
     *
26
     * @var S3Files
27
     */
28
    private $s3Files;
29
 
30
    /**
31
     *
32
     * @param array $config
33
     */
34
    private function __construct($config)
35
    {
36
        $this->config = $config;
37
        $this->s3Files = S3Files::getInstance($this->config);
38
    }
39
 
40
    /**
41
     *
42
     * @param array $config
43
     * @return \LeadersLinked\Library\Storage
44
     */
45
    public static function getInstance($config)
46
    {
47
        if(self::$_instance == null) {
48
            self::$_instance = new Storage($config);
49
        }
50
 
51
        return self::$_instance;
52
    }
53
 
54
    /**
55
     *
56
     * @param User $user
57
     * @return string
58
     */
59
    public function getUserImage($user)
60
    {
61
        if($user->image) {
62
            $remoto = $this->getPathUser() . '/' . $user->uuid . '/' .  $user->image;
63
            if($this->s3Files->objectExist($remoto)) {
287 www 64
                return $this->s3Files->getPresignedUrl($remoto);
65
 
283 www 66
            }
67
        }
68
 
69
        $remoto = $this->config['leaderslinked.images_default.user_image'];
70
        return $this->s3Files->getPresignedUrl($remoto);
71
    }
72
 
73
 
74
    /**
75
     *
76
     * @param User $user
77
     * @return string
78
     */
79
    public function getUserImageForCodeAndFilename($code, $filename)
80
    {
81
        if($filename) {
82
            $remoto = $this->getPathUser() . '/' . $code . '/' .  $filename;
83
            if($this->s3Files->objectExist($remoto)) {
287 www 84
                return $this->s3Files->getPresignedUrl($remoto);
85
 
283 www 86
            }
87
        }
88
 
89
        $remoto = $this->config['leaderslinked.images_default.user_image'];
287 www 90
        return $this->s3Files->getPresignedUrl($remoto);
91
 
283 www 92
    }
93
 
94
    /**
95
     *
96
     * @param User $user
97
     * @param UserProfile $userProfile
98
     * @return string
99
     */
100
    public function getUserProfileImage($user, $userProfile)
101
    {
102
        if($userProfile->image) {
103
            $remoto = $this->getPathUser() . '/' . $user->uuid . '/' .  $userProfile->image;
104
            if($this->s3Files->objectExist($remoto)) {
287 www 105
                return $this->s3Files->getPresignedUrl($remoto);
106
 
283 www 107
            }
108
        }
109
 
110
        $remoto = $this->config['leaderslinked.images_default.user_profile'];
287 www 111
        return $this->s3Files->getPresignedUrl($remoto);
112
 
283 www 113
    }
114
 
115
    /**
116
     *
117
     * @param User $user
118
     * @param UserProfile $userProfile
119
     * @return string
120
     */
121
    public function getUserProfileCover($user, $userProfile)
122
    {
123
        if($userProfile->cover) {
124
            $remoto = $this->getPathUser() . '/' . $user->uuid . '/' .  $userProfile->cover;
125
            if($this->s3Files->objectExist($remoto)) {
287 www 126
                return $this->s3Files->getPresignedUrl($remoto);
127
 
283 www 128
            }
129
        }
130
 
131
        $remoto = $this->config['leaderslinked.images_default.user_cover'];
287 www 132
        return $this->s3Files->getPresignedUrl($remoto);
133
 
283 www 134
    }
135
 
136
    /**
137
     *
138
     * @param Company $company
139
     * @return string
140
     */
141
    public function getCompanyImage($company)
142
    {
143
        if($company->image) {
144
            $remoto = $this->getPathCompany() . '/' . $company->uuid . '/' .  $company->image;
145
            if($this->s3Files->objectExist($remoto)) {
287 www 146
                return $this->s3Files->getPresignedUrl($remoto);
147
 
283 www 148
            }
149
        }
150
 
151
        $remoto = $this->config['leaderslinked.images_default.company_profile'];
287 www 152
        return $this->s3Files->getPresignedUrl($remoto);
153
 
283 www 154
    }
155
 
156
    /**
157
     *
158
     * @param string $code
159
     * @param string $filename
160
     * @return string
161
     */
162
    public function getCompanyImageForCodeAndFilename($code, $filename)
163
    {
164
        if($filename) {
165
            $remoto = $this->getPathCompany() . '/' . $code . '/' .  $filename;
166
            if($this->s3Files->objectExist($remoto)) {
287 www 167
                return $this->s3Files->getPresignedUrl($remoto);
168
 
283 www 169
            }
170
        }
171
 
172
        $remoto = $this->config['leaderslinked.images_default.company_profile'];
287 www 173
        return $this->s3Files->getPresignedUrl($remoto);
174
 
283 www 175
    }
176
 
177
    /**
178
     *
179
     * @param Company $company
180
     * @return string
181
     */
182
    public function getCompanyCover($company)
183
    {
184
        if($company->cover) {
185
            $remoto = $this->getPathCompany() . '/' . $company->uuid . '/' .  $company->cover;
186
            if($this->s3Files->objectExist($remoto)) {
287 www 187
                return $this->s3Files->getPresignedUrl($remoto);
188
 
283 www 189
            }
190
        }
191
 
192
        $remoto = $this->config['leaderslinked.images_default.company_cover'];
287 www 193
        return $this->s3Files->getPresignedUrl($remoto);
194
 
283 www 195
    }
196
 
197
    /**
198
     *
199
     * @param Group $group
200
     * @return string
201
     */
202
    public function getGroupImage($group)
203
    {
204
        if($group->image) {
205
            $remoto = $this->getPathGroup() . '/' . $group->uuid . '/' .  $group->image;
206
            if($this->s3Files->objectExist($remoto)) {
287 www 207
                return $this->s3Files->getPresignedUrl($remoto);
208
 
283 www 209
            }
210
        }
211
 
212
        $remoto = $this->config['leaderslinked.images_default.group_profile'];
287 www 213
        return $this->s3Files->getPresignedUrl($remoto);
214
 
283 www 215
    }
216
 
217
    /**
218
     *
219
     * @param string $code
220
     * @param string $filename
221
     * @return string
222
     */
223
    public function getGroupImageForCodeAndFilename($code, $filename)
224
    {
225
        if($filename) {
226
            $remoto = $this->getPathGroup() . '/' . $code . '/' .  $filename;
227
            if($this->s3Files->objectExist($remoto)) {
287 www 228
                return $this->s3Files->getPresignedUrl($remoto);
229
 
283 www 230
            }
231
        }
232
 
233
        $remoto = $this->config['leaderslinked.images_default.group_profile'];
287 www 234
        return $this->s3Files->getPresignedUrl($remoto);
235
 
283 www 236
    }
237
 
238
    /**
239
     *
240
     * @param Group $group
241
     * @return string
242
     */
243
    public function getGroupCover($group)
244
    {
245
        if($group->cover) {
246
            $remoto = $this->getPathGroup() . '/' . $group->uuid . '/' .  $group->cover;
247
            if($this->s3Files->objectExist($remoto)) {
287 www 248
                return $this->s3Files->getPresignedUrl($remoto);
249
 
283 www 250
            }
251
        }
252
 
253
        $remoto = $this->config['leaderslinked.images_default.group_cover'];
287 www 254
        return $this->s3Files->getPresignedUrl($remoto);
255
 
283 www 256
    }
257
 
258
    /**
259
     *
260
     * @param Group $group
261
     * @return string
262
     */
263
    public function getGenericImage($path, $code, $filename)
264
    {
265
        $remoto = $path . '/' . $code. '/' .  $filename;
331 www 266
 
267
        echo $remoto;
268
 
283 www 269
        if($this->s3Files->objectExist($remoto)) {
287 www 270
            return $this->s3Files->getPresignedUrl($remoto);
271
 
283 www 272
        } else {
273
            $remoto = $this->config['leaderslinked.images_default.no_image'];
287 www 274
            return $this->s3Files->getPresignedUrl($remoto);
275
 
283 www 276
        }
277
    }
278
 
279
    /**
280
     *
298 www 281
     * @param string $path
282
     * @param string $code,
283
     * @param string $filename
284
     * @param boolean $checkExists
283 www 285
     * @return string
286
     */
298 www 287
    public function getGenericFile($path, $code, $filename, $checkExists = true)
283 www 288
    {
289
        $remoto = $path . '/' . $code. '/' .  $filename;
298 www 290
 
291
        if($checkExists) {
292
            if($this->s3Files->objectExist($remoto)) {
293
                return $this->s3Files->getPresignedUrl($remoto);
294
 
295
            } else {
296
                return;
297
            }
298
        }
299
        return $this->s3Files->getPresignedUrl($remoto);
283 www 300
    }
301
 
320 www 302
    /**
303
     *
304
     * @param string $path
305
     * @param string $code
306
     * @param string $filename
307
     * @return string
308
     */
309
    public function delete($path, $code, $filename)
310
    {
311
        $remoto = $path . '/' . $code. '/' .  $filename;
312
        try {
313
            $this->s3Files->deleteObject($remoto);
314
            return true;
315
        } catch (\Exception $exception) {
316
            //echo "No se pudo borrar el archivo : $remoto (" . $exception->getMessage() . ")";
317
            return false;
318
        }
319
 
320
    }
321
 
295 www 322
 
323
 
302 www 324
    /**
325
     *
326
     * @return string
327
     */
328
    public function getPathMedia()
329
    {
330
        return $this->config['leaderslinked.minio.fullpath_media'];
331
    }
295 www 332
 
333
 
283 www 334
    /**
324 www 335
     *
336
     * @return string
337
     */
338
    public function getPathHabitEmoji()
339
    {
340
        return $this->config['leaderslinked.minio.fullpath_habit_emoji'];
341
    }
342
 
343
    /**
344
     *
345
     * @return string
346
     */
347
    public function getPathHabitContent()
348
    {
349
        return $this->config['leaderslinked.minio.fullpath_habit_content'];
350
    }
351
 
352
 
353
    /**
283 www 354
     *
355
     * @return string
356
     */
357
    public function getPathGroup()
358
    {
359
        return $this->config['leaderslinked.minio.fullpath_group'];
360
    }
361
 
362
    /**
363
     *
364
     * @return string
365
     */
366
    public function getPathUser()
367
    {
368
        return $this->config['leaderslinked.minio.fullpath_user'];
369
    }
370
 
371
    /**
372
     *
373
     * @return string
374
     */
375
    public function getPathImage()
376
    {
377
        return $this->config['leaderslinked.minio.fullpath_image'];
378
    }
379
 
380
    /**
381
     *
382
     * @return string
383
     */
384
    public function getPathJob()
385
    {
386
        return $this->config['leaderslinked.minio.fullpath_job'];
387
    }
388
 
389
    /**
390
     *
391
     * @return string
392
     */
393
    public function getPathCompany()
394
    {
395
        return $this->config['leaderslinked.minio.fullpath_company'];
396
    }
397
 
398
    /**
399
     *
400
     * @return string
401
     */
402
    public function getPathFeed()
403
    {
404
        return $this->config['leaderslinked.minio.fullpath_feed'];
405
    }
406
 
295 www 407
 
408
 
283 www 409
    /**
410
     *
411
     * @return string
412
     */
295 www 413
    public function getPathMessage()
414
    {
415
        return $this->config['leaderslinked.minio.fullpath_message'];
416
    }
417
 
418
    /**
419
     *
420
     * @return string
421
     */
283 www 422
    public function getPathPost()
423
    {
424
        return $this->config['leaderslinked.minio.fullpath_post'];
425
    }
426
 
427
    /**
428
     *
429
     * @return string
430
     */
431
    public function getPathMicrolearningTopic()
432
    {
433
        return $this->config['leaderslinked.minio.fullpath_microlearning_topic'];
434
    }
435
 
436
    /**
437
     *
438
     * @return string
439
     */
440
    public function getPathMicrolearningCapsule()
441
    {
442
        return $this->config['leaderslinked.minio.fullpath_microlearning_capsule'];
443
    }
444
 
445
    /**
446
     *
447
     * @return string
448
     */
449
    public function getPathMicrolearningSlide()
450
    {
451
        return $this->config['leaderslinked.minio.fullpath_microlearning_slide'];
452
    }
453
 
454
    /**
455
     *
456
     * @return string
457
     */
458
    public function getPathJobDescription()
459
    {
460
        return $this->config['leaderslinked.minio.fullpath_job_description'];
461
    }
462
 
463
    /**
464
     *
465
     * @return string
466
     */
295 www 467
    public function getPathSelfEvaluation()
283 www 468
    {
469
        return $this->config['leaderslinked.minio.fullpath_self_evaluation'];
470
    }
471
 
472
    /**
473
     *
474
     * @return string
475
     */
476
    public function getPathRecruitmentSelection()
477
    {
478
        return $this->config['leaderslinked.minio.fullpath_recruitment_selection'];
479
    }
480
 
481
    /**
482
     *
483
     * @return string
484
     */
485
    public function getPathPerformanceEvaluation()
486
    {
487
        return $this->config['leaderslinked.minio.fullpath_performance_evaluation'];
488
    }
489
 
490
    /**
491
     *
492
     * @return string
493
     */
494
    public function getPathPlanningObjectivesAnGoals()
495
    {
496
        return $this->config['leaderslinked.minio.fullpath_planning_objectives_and_goals'];
497
    }
498
 
499
    /**
500
     *
501
     * @return string
502
     */
503
    public function getPathSurvey()
504
    {
505
        return $this->config['leaderslinked.minio.fullpath_survey'];
506
    }
507
 
508
    /**
509
     *
510
     * @return string
511
     */
512
    public function getPathNetwork()
513
    {
514
        return $this->config['leaderslinked.minio.fullpath_network'];
515
    }
516
 
517
    /**
518
     *
519
     * @return string
520
     */
521
    public function getPathEngagementReward()
522
    {
523
        return $this->config['leaderslinked.minio.fullpath_engagement_reward'];
524
    }
525
 
526
    /**
527
     *
528
     * @return string
529
     */
530
    public function getPathDailyPulse()
531
    {
532
        return $this->config['leaderslinked.minio.fullpath_daily_pulse'];
533
    }
534
 
535
    /**
536
     *
537
     * @return string
538
     */
539
    public function getPathKnowledgeArea()
540
    {
541
        return $this->config['leaderslinked.minio.fullpath_knowledge_area'];
542
    }
543
 
544
    /**
545
     *
546
     * @return string
547
     */
548
    public function getPathMyCoach()
549
    {
550
        return $this->config['leaderslinked.minio.fullpath_my_coach'];
551
    }
552
 
553
    /**
554
     *
555
     * @param String $filename
556
     * return boolean
557
     */
558
    public function objectExist($filename)
559
    {
285 www 560
        return $this->s3Files->objectExist($filename);
283 www 561
    }
562
 
563
    /**
564
     *
302 www 565
     * @param string $remote
566
     * @param string $local
283 www 567
     * @return boolean
568
     */
302 www 569
    public function putObject($remote, $local)
283 www 570
    {
302 www 571
        return $this->s3Files->putObject($remote, $local);
283 www 572
    }
573
 
574
 
575
    /**
576
     *
577
     * @param string $filename
578
 
579
     * @return boolean
580
     */
581
    public function deleteObject($filename)
582
    {
285 www 583
        return $this->s3Files->deleteObject($filename);
283 www 584
    }
585
 
586
 
587
    /**
588
     *
589
     * @param string $path
590
     * @param string $code
591
     * @param string $filename
592
     * @return boolean
593
     */
594
    public function deleteFile($path, $code, $filename)
595
    {
596
        if($code) {
597
            $remoto = $path . '/' . $code. '/' .  $filename;
598
        } else {
599
            $remoto = $path . '/' .  $filename;
600
        }
601
        if($this->s3Files->objectExist($remoto)) {
602
            return $this->s3Files->deleteObject($remoto);
603
        } else {
604
            return true;
605
        }
606
    }
295 www 607
 
608
    /**
609
     *
610
     * @param string $path
611
     * @param string $code
612
     * @return boolean
613
     */
614
    public function deleteDirectory($path, $code)
615
    {
616
        $remoto = $path . '/' . $code;
617
        if($this->s3Files->objectExist($remoto)) {
618
            return $this->s3Files->deleteObject($remoto);
619
        } else {
620
            return true;
621
        }
622
    }
623
 
283 www 624
 
625
 
626
    /**
627
     *
628
     * @param string $path
629
     * @param string $code
290 www 630
     * @param string $local_filename
283 www 631
     * @return boolean
632
     */
290 www 633
    public function putFile($path, $code, $local_filename)
283 www 634
    {
635
        if($code) {
290 www 636
            $remote = $path . '/' . $code . '/' . basename($local_filename);
283 www 637
        } else {
290 www 638
            $remote = $path. '/' . basename($local_filename);
283 www 639
        }
640
 
290 www 641
 
642
        $result = $this->s3Files->putObject($remote, $local_filename);
643
        if(file_exists($local_filename)) {
644
            @unlink($local_filename);
645
        }
283 www 646
 
647
        return $result;
648
    }
649
 
650
 
651
    /**
652
     *
653
     * @param string $path
654
     * @param string $code
655
     * @param string $tempfile
656
     * @param string $filename
657
     * @return boolean
658
     */
659
    public function moveAndPutFile($path, $code, $tempfile, $filename)
660
    {
661
        if($code) {
662
            $filepath = $path . '/' . $code;
663
        } else {
664
            $filepath = $path;
665
        }
666
 
667
        $filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename;
668
 
669
        move_uploaded_file($tempfile, $filename);
670
 
285 www 671
        $result = $this->s3Files->putObject($filename, $filepath);
283 www 672
        @unlink($filename);
673
 
674
        return $result;
675
    }
676
 
677
    /**
678
     *
679
     * @return string
680
     */
681
    public function getTempPath()
682
    {
683
        return 'data' . DIRECTORY_SEPARATOR . 'storage';
684
    }
685
 
686
 
687
 
688
 
689
 
690
 
691
}